* 去掉limit呢? 因为limit是执行计划的杀手,这个我想大部分DBA知道的吧。。。
explain select `TagId`,`TagType`,`SubType`,`CommId`,`TagFlag`,`TagName`,`OrderId`,`Unum`,`IsBest`,`BrokerId`,`AddDate`,`UpdateDate`,`updatetime`,`tmpnum`,`cityid` from `xx` where `TagType` = 1 and `TagName` ='*高' order by `TagId`
+----+-------------+-----------------+------+--------------------------------------+-----------------------------+---------+-------+-------+----------------------------------
------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra
|
+----+-------------+-----------------+------+--------------------------------------+-----------------------------+---------+-------+-------+----------------------------------
------------------+
| 1 | SIMPLE | xx | ref | idx_lc_1,idx_tagName_brokerId_cityId | idx_tagName_brokerId_cityId | 768 | const | 13854 | Using index condition
e
+----+-------------+-----------------+------+--------------------------------------+-----------------------------+---------+-------+-------+----------------------------------
------------------+
惊奇的发现,执行计划再次发生了改变。。。。
idx_tagName_brokerId_cityId 为什么又冒出来了呢?
那我们再回头看看表结构:
PRIMARY KEY (`TagId`),
KEY `idx_4` (`IsBest`,`TagFlag`,`CommId`),
KEY `idxnew` (`UpdateDate`),
KEY `idx_lc_1` (`TagName`,`TagType`,`TagId`),
KEY `idx_lc_2` (`CommId`,`TagName`,`TagType`),
KEY `idx_tagName_brokerId_cityId` (`TagName`,`BrokerId`,`cityid`),
KEY `idx_lc_3` (`SubType`,`TagType`,`cityid`)
去掉干扰项后:
PRIMARY KEY (`TagId`),
`idx_lc_1` (`TagName`,`TagType`,`TagId`),
`idx_tagName_brokerId_cityId` (`TagName`,`BrokerId`,`cityid`),
执行计划竟然没有选择idx_lc_1,而是idx_tagName_brokerId_cityId,那么这个肯定是干扰索引。
所以,就更加清晰的定位到idx_tagName_brokerId_cityId索引的问题,然后开始调整这个索引,主要是第一个字段TagName的干扰,选择性的问题。
将: KEY `idx_tagName_brokerId_cityId` (`TagName`,`BrokerId`,`cityid`) => KEY `idx_tagName_brokerId_cityId` (`BrokerId`,`TagName`,`cityid`)