Home Database Mysql Tutorial InnoDB一定会在索引中加上主键吗

InnoDB一定会在索引中加上主键吗

Jun 07, 2016 pm 04:35 PM
dba innodb one primary key index discuss

DBA 群里在讨论一个问题,到底InnoDB会不会在索引末尾加上主键,什么时候会加? 我之前看代码记得是如果索引末尾就是主键,那么InnoDB就不再添加主键了,如果索引末尾不是主键,那么会添加主键,但是这跟测试结果不符: CREATETABLE t ( a char(32)notnullpr

DBA群里在讨论一个问题,到底InnoDB会不会在索引末尾加上主键,什么时候会加?

我之前看代码记得是如果索引末尾就是主键,那么InnoDB就不再添加主键了,如果索引末尾不是主键,那么会添加主键,但是这跟测试结果不符:

1

2

3

CREATETABLE t (

  a char(32)notnullprimarykey,

  b char(32)notnull,KEY idx1 (a,b),KEY idx2 (b,a)) Engine=InnoDB;

Copy after login

插入部分数据后可以看到idx1和idx2两个索引的大小相同。这说明idx1和idx2的内部结构是一样的,因此 不可能 是idx1在内部存为(a,b,a)。

在登博的指导下看了 dict0dict.cc:dict_index_build_internal_non_clust() 这个函数,就是构造索引的数据字典的过程,理解了这个过程就明白了,我们接下来解读下这个函数(基于5.6最近trunk):

1

2

3

2727/*******************************************************************//**2728 Builds the internal dictionary cache representation for a non-clustered2729 index, containing also system fields not defined by the user.2730 @return own: the internal representation of the non-clustered index */2731static2732 dict_index_t*2733 dict_index_build_internal_non_clust(2734/*================================*/2735const dict_table_t* table,  /*!mutex)));2748   ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);27492750/* The clustered index should be the first in the list of indexes */2751   clust_index = UT_LIST_GET_FIRST(table->indexes);27522753   ut_ad(clust_index);2754   ut_ad(dict_index_is_clust(clust_index));2755   ut_ad(!dict_index_is_univ(clust_index));27562757/* Create a new index */2758   new_index = dict_mem_index_create(2759     table->name, index->name, index->space, index->type,

2760     index->n_fields +1+ clust_index->n_uniq);27612762/* Copy other relevant data from the old index2763   struct to the new struct: it inherits the values */27642765   new_index->n_user_defined_cols = index->n_fields;27662767   new_index->id = index->id;27682769/* Copy fields from index to new_index */2770   dict_index_copy(new_index, index, table, 0, index->n_fields);27712772/* Remember the table columns already contained in new_index */2773   indexed =static_cast<ibool>(2774     mem_zalloc(table->n_cols *sizeof*indexed));27752776/* Mark the table columns already contained in new_index */2777for(i =0; i n_def; i++){27782779     field = dict_index_get_nth_field(new_index, i);27802781/* If there is only a prefix of the column in the index2782     field, do not mark the column as contained in the index */27832784if(field->prefix_len ==0){27852786       indexed[field->col->ind]= TRUE;2787}2788}27892790/* Add to new_index the columns necessary to determine the clustered2791   index entry uniquely */27922793for(i =0; i n_uniq; i++){27942795     field = dict_index_get_nth_field(clust_index, i);27962797if(!indexed[field->col->ind]){2798       dict_index_add_col(new_index, table, field->col,

2799              field->prefix_len);2800}2801}28022803   mem_free(indexed);28042805if(dict_index_is_unique(index)){2806     new_index->n_uniq = index->n_fields;2807}else{2808     new_index->n_uniq = new_index->n_def;2809}28102811/* Set the n_fields value in new_index to the actual defined2812   number of fields */28132814   new_index->n_fields = new_index->n_def;28152816   new_index->cached = TRUE;28172818return(new_index);2819}</ibool>

Copy after login

这是整个函数,读者最好可以先自己读读这个函数理解一下,然后再看分析。

好了,下面我们开始分析了,首先把 dict_table_t 这个结构体的相关成员解释一下:

1

474unsigned  n_user_defined_cols:10;475/*!

Copy after login

注释很好理解,主要是 n_uniq 表示索引中需要多少个字段来唯一标识一行数据,只对唯一索引有效;n_def 是有多少个字段用了扩展存储空间,就是索引中只存前缀; n_fields 是索引最终一共有多少字段,包括系统加的;n_user_defined_cols 是用户定义的字段数,不包括系统自动加的。

然后我们来看两段最主要的代码:

1

2772/* Remember the table columns already contained in new_index */2773   indexed =static_cast<ibool>(2774     mem_zalloc(table->n_cols *sizeof*indexed));27752776/* Mark the table columns already contained in new_index */2777for(i =0; i n_def; i++){27782779     field = dict_index_get_nth_field(new_index, i);27802781/* If there is only a prefix of the column in the index2782     field, do not mark the column as contained in the index */27832784if(field->prefix_len ==0){27852786       indexed[field->col->ind]= TRUE;2787}2788}</ibool>

Copy after login

InnoDB首先创建了一个布尔型数组,然后依次循环索引上的每一个字段,如果这个字段不是只有前缀,那么就在数组中记下它的索引号,标记这个字段在索引中出现了。因此indexed数组就存下了索引中用户定义的所有字段序号。

1

2

2790/* Add to new_index the columns necessary to determine the clustered2791   index entry uniquely */27922793for(i =0; i n_uniq; i++){27942795     field = dict_index_get_nth_field(clust_index, i);27962797if(!indexed[field->col->ind]){2798       dict_index_add_col(new_index, table, field->col,

2799              field->prefix_len);2800}2801}

Copy after login

这一段就开始循环聚集索引(主键)的每个字段,盘下indexed数组中这个字段是不是有了,如果没有,那么再调用 dict_index_add_col 把字段加到索引中。

因此只要用户定义的索引字段中包含了主键中的字段,那么这个字段就不会再被InnoDB自动加到索引中了,如果用户的索引字段中没有完全包含主键字段,InnoDB就会把剩下的主键字段加到索引末尾。

因此我们最初的例子中, idx1 和 idx2 两个索引内部大小完全一样,没有区别。

最后再补充下组合主键的例子:

1

2

3

4

5

CREATETABLE t (

  a char(32)notnull,

  b char(32)notnull,

  c char(32)notnull,

  d char(32)notnull,PRIMARYKEY(a,b)KEY idx1 (c,a),KEY idx2 (d,b)) Engine=InnoDB;

Copy after login

这个表InnoDB会自动补全主键字典,idx1 实际上内部存储为 (c,a,b),idx2 实际上内部存储为 (d,b,a)。
但是这个自动添加的字段,Server层是不知道的,所以MySQL优化器并不知道这个字段的存在,所以如果你有一个查询:

1

SELECT * FROM t WHERE d=x1 AND b=x2 ORDER BY a;

Copy after login

其实内部存储的idx2(d,b,a)可以让这个查询完全走索引,但是由于Server层不知道,所以最终MySQL优化器可能选择 idx2(d,b) 做过滤然后排序 a 字段,或者直接用PK扫描避免排序。

而如果我们定义表结构的时候就定义为 KEY idx2(d,b,a) ,那么MySQL就知道(d,b,a)三个字段索引中都有,并且InnoDB发现用户定义的索引中包含了所有的主键字段,也不会再添加了,并没有增加存储空间。

因此,由衷的建议,所有的DBA建索引的时候,都在业务要求的索引字段后面补上主键字段,这没有任何损失,但是可能给你带来意外的惊喜。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the Oracle index types? What are the Oracle index types? Nov 16, 2023 am 09:59 AM

Oracle index types include: 1. B-Tree index; 2. Bitmap index; 3. Function index; 4. Hash index; 5. Reverse key index; 6. Local index; 7. Global index; 8. Domain index ; 9. Bitmap connection index; 10. Composite index. Detailed introduction: 1. B-Tree index is a self-balancing tree data structure that can efficiently support concurrent operations. In Oracle database, B-Tree index is the most commonly used index type; 2. Bit Graph index is an index type based on bitmap algorithm and so on.

How to set the return key and home key on Redmi K70Pro? How to set the return key and home key on Redmi K70Pro? Feb 23, 2024 pm 01:40 PM

Mobile phones are now a necessity for young and middle-aged people. Of course, people of each age group have different needs for mobile phones. As one of the more popular models now, RedmiK70Pro has a very diverse range of functions and services that can meet the needs of consumers of different ages. How to set the return key and home key on Redmi K70Pro? You also need to understand it clearly. Only after you understand it can you decide whether to buy this mobile phone. Then follow the editor to take a look at the following content! How to set the return key and home key on Redmi K70Pro? To access your phone's settings menu, you can open the settings interface by pulling down the notification shade or looking for the settings icon on your home screen. In the settings interface, find and click "Button" or "Navigation Bar"

How to solve the problem that the index exceeds the array limit How to solve the problem that the index exceeds the array limit Nov 15, 2023 pm 05:22 PM

The solutions are: 1. Check whether the index value is correct: first confirm whether your index value exceeds the length range of the array. The index of the array starts from 0, so the maximum index value should be the array length minus 1; 2. Check the loop boundary conditions: If you use the index for array access in a loop, make sure the loop boundary conditions are correct; 3. Initialize the array: Before using an array, make sure that the array has been initialized correctly; 4. Use exception handling: You can use the exception handling mechanism in the program to catch errors where the index exceeds the bounds of the array, and handle it accordingly.

How to automatically associate MySQL foreign keys and primary keys? How to automatically associate MySQL foreign keys and primary keys? Mar 15, 2024 pm 12:54 PM

How to automatically associate MySQL foreign keys and primary keys? In the MySQL database, foreign keys and primary keys are very important concepts. They can help us establish relationships between different tables and ensure the integrity and consistency of the data. In actual application processes, it is often necessary to automatically associate foreign keys to the corresponding primary keys to avoid data inconsistencies. The following will introduce how to implement this function through specific code examples. First, we need to create two tables, one as the master table and the other as the slave table. Create in main table

PHP returns the string from the start position to the end position of a string in another string PHP returns the string from the start position to the end position of a string in another string Mar 21, 2024 am 10:31 AM

This article will explain in detail how PHP returns the string from the start position to the end position of a string in another string. The editor thinks it is quite practical, so I share it with you as a reference. I hope you will finish reading this article. You can gain something from this article. Use the substr() function in PHP to extract substrings from a string. The substr() function can extract characters within a specified range from a string. The syntax is as follows: substr(string,start,length) where: string: the original string from which the substring is to be extracted. start: The index of the starting position of the substring (starting from 0). length (optional): The length of the substring. If not specified, then

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

How to improve the efficiency of data grouping and data aggregation in PHP and MySQL through indexes? How to improve the efficiency of data grouping and data aggregation in PHP and MySQL through indexes? Oct 15, 2023 am 11:39 AM

How to improve the efficiency of data grouping and data aggregation in PHP and MySQL through indexes? Introduction: PHP and MySQL are currently the most widely used programming languages ​​and database management systems, and are often used to build web applications and process large amounts of data. Data grouping and data aggregation are common operations when processing large amounts of data, but if indexes are not designed and used appropriately, these operations can become very inefficient. This article will introduce how to use indexes to improve the efficiency of data grouping and data aggregation in PHP and MySQL, and improve

Database security risks caused by insufficient Oracle DBA authority Database security risks caused by insufficient Oracle DBA authority Mar 08, 2024 am 11:33 AM

Database security risks caused by insufficient OracleDBA permissions With the rapid development of the Internet, databases, as an important information storage and management tool for enterprises, carry a large amount of sensitive data. In this process, the database administrator (DBA) plays a vital role and is responsible for ensuring the normal operation of the database and the security of the data. However, due to work requirements or management policies, the DBA's authority is sometimes restricted, which may cause database security risks. This article will introduce the possible consequences of insufficient DBA authority in Oracle database

See all articles