这两天有一个任务表 t_task_list ,搜索条件下面都有,相关的索引也有,但就是用不到索引,应该是跟数据分布有关,所以对索引做了调整。
最重要的不是对索引做了调整,而是,不要以为建了索引就一定能用到,还是根据数据的分布情况来决定的,以后多注意吧,有慢查询,一定看sql,看explain。
alter table t_task_list drop key idx_bd_id_start_time_end_time;
alter table t_task_list add key idx_bd_id_start_end_time(bd_id,start_time,end_time);
//---------------------------------
Fingerprint
select * from `t_task_list` `t` where ((((object_type=?) and (status = ?)) and (start_time >= ?)) and (end_time <= ?)) and (bd_id = ?) order by deal_time desc
Last Sample on host BJ-M6-10-0-14-12 at 2018-07-23 19:32:39
More Samples
SELECT * FROM `t_task_list` `t` WHERE ((((object_type=2) AND (status = 1)) AND (start_time >= 1531670400)) AND (end_time <= 1532352974)) AND (bd_id = 211796) ORDER BY deal_time desc
Explain Plan
+----+-------------+-------+------+------------------+-----+---------+-----+---------+-----------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+------------------+-----+---------+-----+---------+-----------------------------+
| 1 | SIMPLE | t | ALL | idx_starttime_bd | | | | 9976828 | Using where; Using filesort |
+----+-------------+-------+------+------------------+-----+---------+-----+---------+-----------------------------+
/**为了公司保密,对create table做了保密处理**/
//修改之前的
CREATE TABLE `t_task_list` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`company_id` int(11) NOT NULL D,
`bd_id` int(11) NOT NULL COMMEN,
`object_type` tinyint(4) NOT NU,
`object_id` int(11) NOT NULL CO,
`object_source_id` int(11) DEFA,
`category_id` tinyint(4) NOT NU,
`org_id` int(11) NOT NULL COMME,
`start_time` int(11) NOT NULL C,
`end_time` int(11) NOT NULL COM,
`deal_time` int(11) NOT NULL DE,
`extend` varchar(4000) NOT NULL,
`op_id` int(11) NOT NULL DEFAUL,
`status` tinyint(4) NOT NULL CO,
`feed_back` tinyint(4) NOT NULL,
`is_deleted` int(11) NOT NULL D,
`c_t` int(11) NOT NULL COMMENT ,
`u_t` int(11) NOT NULL COMMENT ,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_comp_task_category` (`company_id`,`object_id`,`object_type`,`category_id`),
KEY `idx_starttime_bd` (`start_time`,`bd_id`),
KEY `idx_object_id` (`object_id`)
) ENGINE=InnoDB AUTO_INCREMENT=35301121 DEFAULT CHARSET=utf8 COMMENT='任务下发表'
//修改之后的
PRIMARY KEY (`id`),
UNIQUE KEY `idx_comp_task_category` (`company_id`,`object_id`,`object_type`,`category_id`),
KEY `idx_object_id` (`object_id`),
KEY `idx_bd_id_start_end_time` (`bd_id`,`start_time`,`end_time`)
谢绝转载,谢谢!!!
谢绝转载,谢谢!!!