电子商务网站建设教程 pdf,合肥墙面刷新,性能网站建设,湘潭网站建设选择湘潭振企网站建设一群DBA朋友聊天#xff0c;突然抛出一个某公司联合索引的面试题#xff0c;当时好多人都蒙了#xff0c;这次针对这个问题#xff0c;做了个简单的实验#xff0c;把联合索引的作用一次搞清楚 问题大概是这样的#xff0c;联合索引#xff08;a,b,c,d#xff09;下面这…一群DBA朋友聊天突然抛出一个某公司联合索引的面试题当时好多人都蒙了这次针对这个问题做了个简单的实验把联合索引的作用一次搞清楚 问题大概是这样的联合索引a,b,c,d下面这类的查询分别用到联合索引的哪部分 select * from t where a1 and b1 and d1 and c1; select * from t where a1 and b1 and c1 order by b,c; select * from t where a1 and b1 and c1 and d1 order by b; select * from t where a1 and b1 and d1 order by c; 下面建立测试表字段都用int not null并插入测试数据 CREATE TABLE NewTable (id int NOT NULL ,a int NOT NULL ,b int NOT NULL ,c int NOT NULL ,d int NOT NULL ,PRIMARY KEY (id)); 建立联合索引 ALTER TABLE tADD INDEX idx_a_b_c_d (a, b, c, d) ; 1.explain select * from t where a1 and b1 and d1 and c1\G; id: 1 select_type: SIMPLE table: t type: rangepossible_keys: idx_a_b_c_d key: idx_a_b_c_d key_len: 16 ref: NULL rows: 1 Extra: Using where; Using index key_len16说明索引走了4个字段 4*4字节联合索引用到全部字段 2.explain select * from t where a1 and b1 and c1 order by b,c\G; id: 1 select_type: SIMPLE table: t type: rangepossible_keys: idx_a_b_c_d key: idx_a_b_c_d key_len: 8 ref: NULL rows: 1 Extra: Using where; Using index key_len8 说明where过滤用了两个字段 ab, Extra没有file sort说明排序用到全部索引bc所以这个查询用到了索引的abc部分 3.explain select * from t where a1 and b1 and c1 and d1 order by b\G; id: 1 select_type: SIMPLE table: t type: refpossible_keys: idx_a_b_c_d key: idx_a_b_c_d key_len: 16 ref: const,const,const,const rows: 3 Extra: Using index key_len16 说明where过滤用了4个字段abcdExtra没出现file sort说明排序用到了索引b。 4.explain select * from t where a1 and b1 and d1 order by c\G; id: 1 select_type: SIMPLE table: t type: rangepossible_keys: idx_a_b_c_d key: idx_a_b_c_d key_len: 8 ref: NULL rows: 1 Extra: Using where; Using index; Using filesort key_len8说明where过滤用到了两个字段ab Extra出现filesort说明排序字段没用到索引 这类的查询就不一一测试了总结一下看这类执行计划应该注意的点 key_len 1key_len单位是字节比如int一个字段是4两个字段是8 如果字段可以为空那么key_len会1也就是int会显示5两个字段显示10 2key_len显示的是where条件用到索引长度order by 和group by之后的字段不显示在key_len里面 Extra 两种情况会出现file sort 1where 条件后面没有这个字段但是order by这个字段会出现file sort 2order by的字段没走索引会出现file sort 还有比较重要的是联合索引遇到范围查询会停止继续检索比如where a1 and b1 and c1 and d1那么索引只会检索到abc 当然排序会延续使用where条件后面的索引比如下面的情况 mysql explain select * from t where a1 and b1 order by b,c\G ;*************************** 1. row *************************** id: 1 select_type: SIMPLE table: t type: rangepossible_keys: idx_a_b_c_d key: idx_a_b_c_d key_len: 8 ref: NULL rows: 1 Extra: Using where; Using index 排序用到了ab但是排序用到了bcd也就是这整条查询用到了abcd全部联合索引 转载于:https://www.cnblogs.com/lv-lei/p/7055778.html