mysql,explain執行計畫組合索引測試

2021-09-27 01:32:25 字數 1852 閱讀 8912

create table `users` (

`id` int(11) not null,

`name` varchar(255) default null,

`age` int(11) default null,

`manager_id` int(11) default null,

primary key (`id`),

key `idex_name_age_managerid` (`name`,`age`,`manager_id`) using btree

) engine=innodb default charset=utf8;

-- ----------------------------

-- records of users

-- ----------------------------

insert into `users` values ('205', null, '24', '102');

insert into `users` values ('206', null, '25', '102');

insert into `users` values ('101', 'jack', '35', null);

insert into `users` values ('102', 'tom', '30', '101');

insert into `users` values ('201', 'xiaoli', '23', '102');

insert into `users` values ('203', 'xiaoliu', '22', '102');

1)explain select * from `users` where name = 'tom' and age = 30 and manager_id = 101;

2)explain select * from `users` where name = 'tom';

3)explain select * from `users` where age = 30;

4)explain select * from `users` where manager_id = 101;

5)explain select * from `users` where name = 'tom' and age = 30;

6)explain select * from `users` where name = 'tom' and manager_id = 101;

7)explain select * from `users` where age = 30 and manager_id = 101;

8)explain select * from `users` where age = 30 and name = 'tom';

9)explain select * from `users` where manager_id= 101 and name = 'tom';

1、由以上執行計畫可以看出,第3、4、7個執行計畫沒有命中索引,所以,只要是以含有 name條件的sql語句都命中了索引(遵循左字首原則);

2、第8、9個sql雖然沒有以name開頭,但是也命中了索引,說明 mysql的查詢優化器會幫你優化成索引可以識別的形式。

3、根據執行計畫分析,命中索引的sql第6、9個查詢效率是比較低的,filtered只有16.67,所以:

組合索引 idex_name_age_managerid (`name`,`age`,`manager_id`)   sql 執行為了提高執行效率,where 條件字段順序應該遵循以下組合

name

name,age

name,age,manager_id

Mysql explain 執行計畫

使用方法,在select語句前加上explain就可以了 如 explain select from test1 explain列的解釋 table 顯示這一行的資料是關於哪張表的 type 這是重要的列,顯示連線使用了何種型別。從最好到最差的連線型別為const eq reg ref range ...

MySql Explain執行計畫

explain執行計畫 例 explain select from user idselect type table partitions type possible keys keykey len refrows filtered extra id 查詢序列號,表示查詢中執行select子句或操作...

MySQL Explain 執行計畫

執行計畫就是sql的執行查詢的順序,以及如何使用索引查詢,返回的結果集行數,可以根據執行計畫結果結合業務對現有sql進行優化 explain 查詢結果有10列,分別表示的含義是 是乙個有序的編號,是查詢的順序號,有幾個select就顯示幾行,id的順序是按照select出現的順序增長的,id列的值越...