组合索引中,需要按照最左前缀原则使用索引字段才会有效:带头索引不能丢,中间索引不能断
# 正确示例mysql> explain select * from tuser where name = 'fdcoffee' and age = 36 and sex = 'M' G;mysql> explain select * from tuser where name = 'fdcoffee' and age = 36 G;mysql> explain select * from tuser where name = 'fdcoffee' G;# 错误示例# 缺少带头索引name,剩下的age和sex字段都无法使用索引mysql> explain select * from tuser where age = 36 and sex = 'M' G;# 同上,没有前面的name和age字段一起,sex字段无法使用到索引mysql> explain select * from tuser where sex = 'M' G;# 缺少中间索引age,只能使用到部分索引:name字段有效,但sex字段无法用到索引mysql> explain select * from tuser where name = 'fdcoffee' and sex = 'M' G;不要在索引列上进行计算操作:计算、函数、自动/手动类型转换,不然会导致索引失效而转向全表扫描
# 使用left函数对loginname长度截取,索引失效mysql> explain select * from tuser where left(loginname,4) = 'fdco' G*************************** 1. row ***************************id: 1select_type: SIMPLEtable: tuserpartitions: NULLtype: ALLpossible_keys: NULLkey: NULLkey_len: NULLref: NULLrows: 7filtered: 100.00Extra: Using where# 上面的示例可以这样优化:使用 like% 或在程序上先对loginname做处理在传入MySQL数据库查询mysql> explain select * from tuser where loginname like 'fdco%' G*************************** 1. row ***************************id: 1select_type: SIMPLEtable: tuserpartitions: NULLtype: rangepossible_keys: idx_loginnamekey: idx_loginnamekey_len: 303ref: NULLrows: 2filtered: 100.00Extra: Using index condition不能继续使用索引中范围条件(between、<、>、in等)右边的列
# 由于age使用范围操作符,后面的sex字段索引失效mysql> explain select * from tuser where name = 'fdcoffee' and age > 20 and sex = 'M' G;*************************** 1. row ***************************id: 1select_type: SIMPLEtable: tuserpartitions: NULLtype: rangepossible_keys: idx_name_age_sexkey: idx_name_age_sexkey_len: 308ref: NULLrows: 1filtered: 14.29Extra: Using index condition尽量使用覆盖索引(select 索引列),也就是查询列和索引列一致,减少使用select *
# 使用select *全表扫描mysql> explain select * from tuser G;*************************** 1. row ***************************id: 1select_type: SIMPLEtable: tuserpartitions: NULLtype: ALLpossible_keys: NULLkey: NULLkey_len: NULLref: NULLrows: 7filtered: 100.00Extra: NULL# 使用select 索引列时,扫描索引,不需要回表mysql> explain select name,age,sex from tuser G;*************************** 1. row ***************************id: 1select_type: SIMPLEtable: tuserpartitions: NULLtype: indexpossible_keys: NULLkey: idx_name_age_sexkey_len: 312ref: NULLrows: 7filtered: 100.00Extra: Using index索引字段上使用不等(!=或者<>)判断时,会导致索引失效而转向全表扫描
mysql> explain select * from tuser where name != 'fdcoffee' G;*************************** 1. row ***************************id: 1select_type: SIMPLEtable: tuserpartitions: NULLtype: ALL# 全表扫描possible_keys: idx_name_age_sexkey: NULLkey_len: NULLref: NULLrows: 7filtered: 100.00Extra: Using where索引字段上使用is null/is not null判断会走全表扫描 。允许为null的索引字段呢?看下面分析 。
# 主键idmysql> explain select * from tuser where id is not null G;*************************** 1. row ***************************id: 1select_type: SIMPLEtable: tuserpartitions: NULLtype: ALLpossible_keys: PRIMARYkey: NULLkey_len: NULLref: NULLrows: 7filtered: 85.71Extra: Using where# 由于主键不允许为空,使用is null,执行计划信息Extra列将提示Impossible WHERE信息mysql> explain select * from tuser where id is null G;*************************** 1. row ***************************id: 1select_type: SIMPLEtable: NULLpartitions: NULLtype: NULLpossible_keys: NULLkey: NULLkey_len: NULLref: NULLrows: NULLfiltered: NULLExtra: Impossible WHERE# 允许为空的索引# 使用is not null将走全表扫描mysql> explain select * from tuser where loginname is not null G;*************************** 1. row ***************************id: 1select_type: SIMPLEtable: tuserpartitions: NULLtype: ALLpossible_keys: idx_loginnamekey: NULLkey_len: NULLref: NULLrows: 7filtered: 100.00Extra: Using where# 而is null则用到了索引mysql> explain select * from tuser where loginname is null G;*************************** 1. row ***************************id: 1select_type: SIMPLEtable: tuserpartitions: NULLtype: refpossible_keys: idx_loginnamekey: idx_loginnamekey_len: 303ref: constrows: 1filtered: 100.00Extra: Using index condition
推荐阅读
-
中国吉林网|正阳街道万福社区开展国家安全教育街头宣传活动
-
-
-
「大宗交易」北京大宗交易连涨5年 自用买家独宠核心区城市更新项
-
-
阿娇晒伤口照片|甜蜜的负担!阿娇晒伤口照片,伤口针脚触目惊心
-
-
吴亦凡|吴亦凡自爆是个女儿控,我看他见到可爱的小女孩,就忍不住想下手
-
-
-
体育风云传|苏炳添基因有多强,2岁儿子跑步的父亲真传,网友:这是个孩子?
-
-
问董秘|请问2...,投资者提问:董秘好:公司披露过18/19年股权激励费用对利润的影响
-
-
高达|少年!你真幸福!一出考场就收到妈妈送的高达模型
-
遥不可及|每个架构师都在研究的康威定律,程序员进阶路上,你思考过吗?
-
荒郊野史|在韩信和萧何遇害的时候,为何选择袖手旁观?,张良那么足智多谋
-
【极品飞车视频|学会吃鸡到手软!,和平精英狙击手必备攻略】
-
菊花和什么搭配着喝最合适,菊花和枸杞起泡有什么功效
-
历史智慧库|成为一名太监,为何现在多地都有他的后代?,12岁郑和入宫