您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

mysql 多列索引 详细说明

2024/8/18 17:02:09发布49次查看
mysql的索引可以分为单列索引和多列索引,单列索引,请参考: 添加mysql索引的3条原则 mysql可以为多个列创建一个索引,最多可以16列,多列索引可以视为包含通过连接索引列的值而创建值的排序数组。mysql多列索引适用场合:当你在where子句中为索引的第1个列
mysql的索引可以分为单列索引和多列索引,单列索引,请参考:添加mysql索引的3条原则
mysql可以为多个列创建一个索引,最多可以16列,多列索引可以视为包含通过连接索引列的值而创建值的排序数组。mysql多列索引适用场合:当你在where子句中为索引的第1个列指定已知的参数时,查询很快,即使你没有指定其它列的值,这里的其他列是多列索引里面,指定的其他列。
一,创建测试表index_test
mysql> create table `index_test` ( -> `id` int(11) not null auto_increment, -> `user_id` int(11) not null, -> `username` varchar(20) not null, -> primary key (`id`) -> ) engine=myisam default charset=utf8 auto_increment=1 ;query ok, 0 rows affected (0.08 sec)
二,创建多列索引
1,普通多列索引
mysql> alter table index_test add index test( user_id, username );query ok, 0 rows affected (0.07 sec)records: 0 duplicates: 0 warnings: 0
2,多列唯一索引
mysql> alter table index_test add unique test( user_id, username );query ok, 0 rows affected (0.06 sec)records: 0 duplicates: 0 warnings: 0
3,多列主键索引
mysql> alter table index_test add primary key test( user_id, username );query ok, 0 rows affected (0.06 sec)records: 0 duplicates: 0 warnings: 0
我们主键一般都是id,并且是自增长的,如果有,就要先删除主键后在创建多列主键索引,不然会报错的,error 1068 (42000): multiple primary key defined;
删除索引,可以用drop index test on index_test
三,例子
1,使用多列索引的情况
例1,
mysql> explain select * from index_test where user_id=1\g;*************************** 1. row *************************** id: 1 select_type: simple table: index_test type: refpossible_keys: test key: test //使用了索引test key_len: 4 ref: const rows: 1 extra:1 row in set (0.00 sec)error:no query specified
例2,
mysql> explain select * from index_test where user_id=1 and username='tank'\g;*************************** 1. row *************************** id: 1 select_type: simple table: index_test type: refpossible_keys: test key: test key_len: 66 ref: const,const rows: 1 extra: using where1 row in set (0.00 sec)error:no query specified
例3,
mysql> explain select * from index_test where user_id=1 and (username='tank' or username='zhang')\g;*************************** 1. row *************************** id: 1 select_type: simple table: index_test type: rangepossible_keys: test key: test key_len: 66 ref: null rows: 2 extra: using where1 row in set (0.00 sec)error:no query specified
2,不使用多列索引的情况
例4,
mysql> explain select * from index_test where user_id=1 or username='tank'\g;*************************** 1. row *************************** id: 1 select_type: simple table: index_test type: allpossible_keys: test //列出了可能存在的索引 key: null //但是并没有使用这个索引 key_len: null ref: null rows: 2 extra: using where1 row in set (0.01 sec)error:no query specified
例5,
mysql> explain select * from index_test where username='tank'\g;*************************** 1. row *************************** id: 1 select_type: simple table: index_test type: allpossible_keys: null //可能存在的索引都没有列出来 key: null //也没有使用多列索引 key_len: null ref: null rows: 2 extra: using where1 row in set (0.00 sec)error:no query specified
根据上面测试,多列索引的第一列很重要,以上面例子为例,就是user_id这一列。要想多列索列起作用,第一列必须要包含在内,如果要用到or,不要与第一列并行。看例4
mysql的索引可以分为单列索引和多列索引,单列索引,请参考:添加mysql索引的3条原则mysql可以为多个列创建一个索引,最多可以16列,多列索引可以视为包含通过连接索引列的值而创建值的排序数组。mysql多列索引适用场合:当你在where子句中为索引的第1个列指定已知的参数时,查询很快,即使你没有指定其它列的值,这里的其他列是多列索引里面,指定的其他列。一,创建测试表index_testmysql> create table `index_test` ( -> `id` int(11) not null auto_increment, -> `user_id` int(11) not null, -> `username` varchar(20) not null, -> primary key (`id`) -> ) engine=myisam default charset=utf8 [...]
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product