在tb_user中,关于profession的索引有:
联合索引:idx_user_pro_age_sta:
profession
,age
,status
单列索引:idx_user_pro:
profession
-- 查询走了联合索引。这是MySQL自动选择的结果。
mysql> explain select * from tb_user where profession = '软件工程'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: tb_user
partitions: NULL
type: ref
possible_keys: idx_user_pro_age_sta,idx_user_pro
key: idx_user_pro_age_sta
key_len: 36
ref: const
rows: 1
filtered: 100.00
Extra: NULL
1 row in set, 1 warning (0.00 sec)
从上面来看,MySQL帮我们选择走了联合索引(idx_user_pro_age_sta)
但是我们也可以自己指定走哪个索引
use index
: 建议MySQL使用哪一个索引完成此次查询(仅仅是建议,mysql内部还会再次进行评估)。
mysql> explain select * from tb_user use index(idx_user_pro) where profession = '软件工程'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: tb_user
partitions: NULL
type: ref
possible_keys: idx_user_pro
key: idx_user_pro
key_len: 36
ref: const
rows: 1
filtered: 100.00
Extra: NULL
1 row in set, 1 warning (0.00 sec)
ignore index
: 忽略指定的索引。
mysql> explain select * from tb_user ignore index(idx_user_pro) where profession = '软件工程'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: tb_user
partitions: NULL
type: ref
possible_keys: idx_user_pro_age_sta
key: idx_user_pro_age_sta
key_len: 36
ref: const
rows: 1
filtered: 100.00
Extra: NULL
1 row in set, 1 warning (0.00 sec)
force index
: 强制使用索引。
mysql> explain select * from tb_user force index(idx_user_pro) where profession = '软件工程'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: tb_user
partitions: NULL
type: ref
possible_keys: idx_user_pro
key: idx_user_pro
key_len: 36
ref: const
rows: 1
filtered: 100.00
Extra: NULL
1 row in set, 1 warning (0.00 sec)
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 1909773034@qq.com