组合索引中,需要按照最左前缀原则使用索引字段才会有效:带头索引不能丢,中间索引不能断
# 正确示例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
推荐阅读
-
本地股|多只厦门本地股前三季度实现盈利 10股被北上资金悄悄埋伏
-
「书中烟火」到帝国萧然坍塌——司马迁说:太可惜了,秦始皇从灭六国到大一统
-
中国质量新闻网TB|这2批次移动电源还敢用吗?,辐射骚扰场强等项目不合格
-
【马德里mini|年薪高达1000万欧,巴黎接近以5500万至6000万欧买断伊卡尔迪】
-
-
车讯之家|年销100万台,出口40个国家皆获认可,又一国产品牌脱颖而出
-
旭旭宝宝|深渊材料谁最多?全职业第一人的黑旭被碾压,大坤坤不愧第一肝帝
-
孙伟勇|亿田股份过会:今年IPO获批第171家 财通证券过4单
-
-
新新爱科技|当心被封卡降额,以下4种刷卡行为的人留意了!银行已经盯上你了
-
#凯文·杜兰特#疯狂一夜!杜兰特喜讯,NBA官宣重要决定,常规赛或迎新调整
-
电影|摄影师拍《闪电侠》片场照 老爷街头帅气骑摩托
-
-
『虫子爱搞笑』居然被园方劝退了,原因是......,开心一笑:儿子第一天上幼儿园
-
电话|外卖员埋怨大学生取餐慢,遭大学生短信辱骂,外卖小哥回应事件始末真相
-
游戏攻略|《第二银河》1.7版本强化版战术视图攻略
-
青年|口袋妖怪:盘点宝可梦里玩家选择数最多的精灵!暴鲤龙无一例外!
-
为啥有些女生扎起头发很自信从容,放下头发显得很害羞
-
「御龙美玉行」你家有吗?单枚价值已近千元!,这样的一角硬币
-
風兮|东部第一的雄鹿为何被热火支配?,0比3!分锅大会