MySQL 二级索引会不会自动补齐主键

开篇:一直对Mysql 二级索引是否自动加入主键问题有怀疑,今天又时间就5.5和5.6分析了一把:

mysql> select version();
+------------+
| version()  |
+------------+
| 5.6.16-log |
mysql> create table t9(
    -> id int not null ,
    -> a int ,
    -> b int,
    -> c int,
    -> primary key(id),
    -> key ab_idx(a,b)
    -> )engine=innodb;
Query OK, 0 rows affected (0.00 sec)
show variables like ‘%optimizer_swit%‘;
....  use_index_extensions=off
mysql> alter table t9 drop primary key ;
Query OK, 16 rows affected (0.01 sec)
Records: 16  Duplicates: 0  Warnings: 0

mysql> alter table t9 add primary key(id,id2);
mysql> desc select * from t9 where a=2 and b=1 order by id;
+----+-------------+-------+------+---------------+--------+---------+-------------+------+-------------+
| id | select_type | table | type | possible_keys | key    | key_len | ref         | rows | Extra       |
+----+-------------+-------+------+---------------+--------+---------+-------------+------+-------------+
|  1 | SIMPLE      | t9    | ref  | ab_idx        | ab_idx | 10      | const,const |    3 | Using where |
+----+-------------+-------+------+---------------+--------+---------+-------------+------+-------------+
1 row in set (0.00 sec)

mysql> desc select * from t9 where a=2 and b=1 order by id,id2;
+----+-------------+-------+------+---------------+--------+---------+-------------+------+-------------+
| id | select_type | table | type | possible_keys | key    | key_len | ref         | rows | Extra       |
+----+-------------+-------+------+---------------+--------+---------+-------------+------+-------------+
|  1 | SIMPLE      | t9    | ref  | ab_idx        | ab_idx | 10      | const,const |    3 | Using where |
+----+-------------+-------+------+---------------+--------+---------+-------------+------+-------------+
* 发现是会自动补齐
mysql> select version(); 
+------------+
| version()  |
+------------+
| 5.5.36-log |
mysql> CREATE TABLE t01 (
    ->   a char(32) not null,
    ->   b char(32) not null,
    ->   c char(32) not null,
    ->   d char(32) not null,
    ->   PRIMARY KEY (a,b),
    ->    KEY idx2 (d,b)
    -> ) Engine=InnoDB;
mysql> explain select * from t01 where d=‘w‘ and b=‘g‘ order by a;                                                                   
+----+-------------+-------+------+---------------+------+---------+-------------+------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref         | rows | Extra       |
+----+-------------+-------+------+---------------+------+---------+-------------+------+-------------+
|  1 | SIMPLE      | t01   | ref  | idx2          | idx2 | 192     | const,const |    3 | Using where |
+----+-------------+-------+------+---------------+------+---------+-------------+------+-------------+
1 row in set (0.00 sec)

mysql> explain select * from t01 where d=‘w‘ and b=‘g‘ order by a,b;
+----+-------------+-------+------+---------------+------+---------+-------------+------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref         | rows | Extra       |
+----+-------------+-------+------+---------------+------+---------+-------------+------+-------------+
|  1 | SIMPLE      | t01   | ref  | idx2          | idx2 | 192     | const,const |    3 | Using where |
+----+-------------+-------+------+---------------+------+---------+-------------+------+-------------+

MySQL 5.6.16 源码 storage/innobase/dict/dict0dict.cc

/*******************************************************************//**
Builds the internal dictionary cache representation for a clustered
index, containing also system fields not defined by the user.
@return	own: the internal representation of the clustered index */
static
dict_index_t*
dict_index_build_internal_clust(
/*============================*/
	const dict_table_t*	table,	/*!< in: table */
	dict_index_t*		index)	/*!< in: user representation of
					a clustered index */
{
	dict_index_t*	new_index;
	dict_field_t*	field;
	ulint		trx_id_pos;
	ulint		i;
	ibool*		indexed;

	ut_ad(table && index);
	ut_ad(dict_index_is_clust(index));
	ut_ad(mutex_own(&(dict_sys->mutex)));
	ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);

	/* Create a new index object with certainly enough fields */
	new_index = dict_mem_index_create(table->name,
					  index->name, table->space,
					  index->type,
					  index->n_fields + table->n_cols);
	/* Remember the table columns already contained in new_index */
	indexed = static_cast<ibool*>(
		mem_zalloc(table->n_cols * sizeof *indexed));

	/* Mark the table columns already contained in new_index */
	for (i = 0; i < new_index->n_def; i++) {

		field = dict_index_get_nth_field(new_index, i);

		/* If there is only a prefix of the column in the index
		field, do not mark the column as contained in the index */

		if (field->prefix_len == 0) {

			indexed[field->col->ind] = TRUE;
		}
	}

总结:

  1. 从源代码看出在引擎层是做了主动补齐主键到二级索引的最后面,但是server层并不知道主键补齐到后面,mysql server层不一定动能自动识别二级索引后面的主键列,强烈建议创建二级索引的时候加上主键列。
  2. 我上面的演示实例M ySQL server层是自动自动识别了二级索引后的主键列,看运气吧,目前还没遇到过不识别的。
时间: 2024-07-30 22:49:26

MySQL 二级索引会不会自动补齐主键的相关文章

Elasticsearch in java 范例:自动补齐功能(completion suggester)

ES(elasticsearch)的suggester共有四类(term suggester, phrase suggester, completion suggester, context suggester), 其中completion suggester作为搜索框中的自动补齐功能,尤为常用. 本文将用java语言实现一个简单例子来叙述如何使用completion suggester. 例子的主要功能是为股票的名称和编号建立自动补齐功能. 实现一个完整的completion suggeste

小技巧--tab键自动补齐Git命令

Git是什么,你不清楚? 好吧,那么该篇内容对你也木有帮助,请绕道而行.. 我们在使用Git命令时,可以通过tab键,自动补齐Git,特别是在切换分支时特别有用. 如下,当我们想将当前分支切换到bugfix/DEV-31999-review-dialog-dose-not-show-scroll时,再此之前,要么傻兮兮的敲打分支名,要么复制粘贴: 但是,倘若我们可以通过tab键自动填充目的分支,岂不完美,如下: 下面就是关于设置mac下Tab键对git命令起作用的方法: Install Bash

Eclipse -- 自动补齐设置和其他用法

1:自动补齐设置:最简单的修改方式是:Windows——>Preferences——>Java-->Editor-->Content Asist,在Auto activation triggers for Java后面的文本框里只有一个“.”.现在你将其改为“.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”即可.然后你再试试,会发现,现在的补全功能跟VS差不多了.你还可以在Advanced和Favorite里进行高级的设置

关闭浏览器输入框自动补齐 兼容IE,FF,Chrome等主流浏览器

这篇文章主要介绍了关闭浏览器输入框自动补齐 兼容IE,FF,Chrome等主流浏览器,需要的朋友可以参考下.希望对大家有所帮助 Firefox 和 IE 的浏览器各自实现了input历史记录的功能,可以简化输入时的麻烦,但是,有时候弹出的下拉框会挡住页面显示内容,而且在某些情况下也不需要对input框进行记录,如号码查询的input框,用户不会对同一个号码进行多次查询,就没有必要让浏览器记录. MSIE自定义了input 的扩展属性 autocomplete,置为off即可 <input typ

webstorm配置Autoprefixer,自动补齐代码

1.win+R,运行npm install postcss-cli -g 2.安装autoprefixer npm install autoprefixer -g 3.安装 npm install postcss-cli -g 4.打开webstorm--settings--找到External Tools,新增Tools 配置参数 Program:填入你的PATH,postcss.cmd所在的文件夹 我的是C:\Users\还有谁\AppData\Roaming\npm\postcss.cmd

CocoaPods 导入第三方库头文件自动补齐

使用了一段时间CocoaPods来管理Objective-c的类库,方便了不少.但是有一个小问题,当我在xcode输入import关键字的时候,没有自动联想补齐代码的功能,需要手工敲全了文件名,难以适应. 在stackoverflow上找到了解决办法: Go to the Target > ”Build Settings” tab and find the ”User Header Search Paths” setting. Set this to ”$(BUILT_PRODUCTS_DIR)

CocoaPods导入第三方库头文件自动补齐

使用了一段时间CocoaPods来管理Objective-c的类库,方便了不少.但是有一个小问题,当我在xcode输入import关键字的时候,没有自动联想补齐代码的功能,需要手工敲全了文件名,难以适应. 在stackoverflow上找到了解决办法: Go to the Target > ”Build Settings” tab and find the ”User Header Search Paths” setting. Set this to ”$(BUILT_PRODUCTS_DIR)

mac下 netbeans 8.02中文版设置代码自动补齐

netbeans自带的自动补齐快捷键是commad+\ 我想要的是在输入的时候,有自动提示,找了半天也没找到怎么搞. 因为我是用的mac系统 后来参考其他的设置,找到了设置的方法,把这个方法记录一下. 先是打开netbeans,进入偏好设置 点那个Preferences... 然后选择编辑器选项,找到代码完成,把语言设置成Java,就看到我想设置的东西了. 本来里面只有一个.  ,就是只有输入.的时候才会提示,这里我把所有的大小写英文字母全部加进了. 最后确定应用. 回到编辑器,试下功能. 已经

python tab键自动补齐命令

一.基础环境 1.角色.ip.版本.内核 serverA 10.1.10.117 3.2.0-4-amd64 7.8 python readline rlcompleter python-2.7.3 二.python tab键自动补齐命令安装 1.安装python apt-get -y install python 2.查看下目前已安装的模块 python Python 2.7.3 (default, Mar 13 2014, 11:03:55)  [GCC 4.7.2] on linux2 T