首页 > 其他分享 >复合索引 multiple-column index Composite Indexes

复合索引 multiple-column index Composite Indexes

时间:2023-09-16 14:24:30浏览次数:38  
标签:index multiple name column col2 SELECT first

MySQL :: MySQL 8.0 Reference Manual :: 8.3.6 Multiple-Column Indexes https://dev.mysql.com/doc/refman/8.0/en/multiple-column-indexes.html

 MySQL :: MySQL 8.0 Reference Manual :: 8.2.1.3 Index Merge Optimization https://dev.mysql.com/doc/refman/8.0/en/index-merge-optimization.html

 

8.3.6 Multiple-Column Indexes

MySQL can create composite indexes (that is, indexes on multiple columns). An index may consist of up to 16 columns. For certain data types, you can index a prefix of the column (see Section 8.3.5, “Column Indexes”).

MySQL can use multiple-column indexes for queries that test all the columns in the index, or queries that test just the first column, the first two columns, the first three columns, and so on. If you specify the columns in the right order in the index definition, a single composite index can speed up several kinds of queries on the same table.

A multiple-column index can be considered a sorted array, the rows of which contain values that are created by concatenating the values of the indexed columns.

Note

As an alternative to a composite index, you can introduce a column that is “hashed” based on information from other columns. If this column is short, reasonably unique, and indexed, it might be faster than a “wide” index on many columns. In MySQL, it is very easy to use this extra column:

SELECT * FROM tbl_name
  WHERE hash_col=MD5(CONCAT(val1,val2))
  AND col1=val1 AND col2=val2;

Suppose that a table has the following specification:

CREATE TABLE test (
    id         INT NOT NULL,
    last_name  CHAR(30) NOT NULL,
    first_name CHAR(30) NOT NULL,
    PRIMARY KEY (id),
    INDEX name (last_name,first_name)
);

The name index is an index over the last_name and first_name columns. The index can be used for lookups in queries that specify values in a known range for combinations of last_name and first_name values. It can also be used for queries that specify just a last_name value because that column is a leftmost prefix of the index (as described later in this section). Therefore, the name index is used for lookups in the following queries:

SELECT * FROM test WHERE last_name='Jones';

SELECT * FROM test
  WHERE last_name='Jones' AND first_name='John';

SELECT * FROM test
  WHERE last_name='Jones'
  AND (first_name='John' OR first_name='Jon');

SELECT * FROM test
  WHERE last_name='Jones'
  AND first_name >='M' AND first_name < 'N';

However, the name index is not used for lookups in the following queries:

SELECT * FROM test WHERE first_name='John';

SELECT * FROM test
  WHERE last_name='Jones' OR first_name='John';

Suppose that you issue the following SELECT statement:

SELECT * FROM tbl_name
  WHERE col1=val1 AND col2=val2;

If a multiple-column index exists on col1 and col2, the appropriate rows can be fetched directly. If separate single-column indexes exist on col1 and col2, the optimizer attempts to use the Index Merge optimization (see Section 8.2.1.3, “Index Merge Optimization”), or attempts to find the most restrictive index by deciding which index excludes more rows and using that index to fetch the rows.

If the table has a multiple-column index, any leftmost prefix of the index can be used by the optimizer to look up rows. For example, if you have a three-column index on (col1, col2, col3), you have indexed search capabilities on (col1)(col1, col2), and (col1, col2, col3).

MySQL cannot use the index to perform lookups if the columns do not form a leftmost prefix of the index. Suppose that you have the SELECT statements shown here:

SELECT * FROM tbl_name WHERE col1=val1;
SELECT * FROM tbl_name WHERE col1=val1 AND col2=val2;

SELECT * FROM tbl_name WHERE col2=val2;
SELECT * FROM tbl_name WHERE col2=val2 AND col3=val3;

If an index exists on (col1, col2, col3), only the first two queries use the index. The third and fourth queries do involve indexed columns, but do not use an index to perform lookups because (col2) and (col2, col3) are not leftmost prefixes of (col1, col2, col3).

 

 

 

翻译

搜索

复制

标签:index,multiple,name,column,col2,SELECT,first
From: https://www.cnblogs.com/papering/p/17706674.html

相关文章

  • nginx 如何配置指向带有hash值的index.html文件?
    要在Nginx配置中指向带有哈希值的index.html文件,可以使用通配符(*)来匹配文件名中的哈希部分。以下是一个示例配置:location/{root/path/to/html/demo;indexindex.html;try_files$uri$uri//index.html;location~^/index\.(\w+)\.html${#使用......
  • gorm stdErr = sql: Scan error on column index 0, name "total": converting NULL
    前言使用gorm查询时,报错:stdErr=sql:Scanerroroncolumnindex0,name"total":convertingNULLtofloat64isunsupported代码如下vartotalfloat64res:=db.Model(&model.Record{}).Select("sum(amount)astotal").Where("id=?andtyp......
  • mysql中information_schema.columns字段说明
    语句如下:SELECT *FROM information_schema.COLUMNSWHERE TABLE_SCHEMA='具体某个数据库';各字段含义如下:字段含义table_schema 表所有者(对于schema的名称)table_name 表名column_name 列名ordinal_position 列标识号column_default 列的默认值is_nullable 列的为空性......
  • Bug库____org.springframework.jdbc.IncorrectResultSetColumnCountException: Incorr
    Bug:使用到了spring的jdbctemplate模板使用到以下template.queryForObject(sql,requiredType)template.queryForList(sql,elementType,args)报以下错误org.springframework.jdbc.IncorrectResultSetColumnCountException:Incorrectcolumncount:expected1,actual3检查完......
  • BUG(Spring Framework JdbcTemplate) org.springframework.jdbc.IncorrectResultSetCo
    一.SpringFramework queryForObject问题1.spring4.0之前使用使用jdbctemplate的queryForObject(Stringsql,Object[]args,RowMapper<T>rowMapper)直接放入class类型会报错org.springframework.jdbc.IncorrectResultSetColumnCountException:Incorrectcolumncount:expec......
  • Vue.js的index.html文件中引入JavaScript文件
    将js文件放在public文件夹下面在index.html文件下引入js文件在前面加<%=BASE_URL%>后面加路径,如果想将本地js文件打包之后也放在static/js文件夹下,需要在public文件夹下创建一个和打包之后文件放的位置一样的文件夹<scriptsrc="<%=BASE_URL%>./static/js/js文件名"></sc......
  • Oracle 查询当前用户下所有索引(Index)并拼接创建脚本
    原文地址:https://blog.csdn.net/zy_workjob/article/details/82011825 Oracle查询当前用户下所有索引(Index,PK)并拼接创建脚本【不包含FUNCTION-BASED基于函数的索引】SELECTT.TABLE_NAME,--表名T.INDEX_NAME,--索引名I.UNIQUENESS,--是否非空I.INDEX_......
  • python中字符串内置函数find和index
     001、find>>>str1="xyabmnabkj"##测试字符串>>>foriinenumerate(str1):...print(i)##列出每个字符的索引...(0,'x')(1,'y')(2,'a')(3,'b')(4,'m'......
  • index.html在webpack打包时动态生成index模板
    通过<%=BASE_URL%>包裹环境变量通过<%if(process.env.NODE_ENV==='production'){%><%}%>包裹条件判断<!DOCTYPEhtml><html><head><metacharset="utf-8"/><metacontent="IE=edge,chrome=1&qu......
  • 不再担心正向还是逆向查询,Vlookup函数的救星Index+Match组合!
    1职场实例我们在日常使用Excel时,遇到最多的场景便是“查找”问题,一提到“查找”,我们脑海里首先想到的肯定就是“Vlookup函数”了,但是“Vlookup函数”也有其自身的小短板,比如说遇到逆向查找的时候,使用Vlookup函数的基本语法公式时便会立马失效。那么有没有一种方法,不管正向还是逆向......