查询正在进行的全表扫描

时间:2013-04-23 08:16:26

标签: mysql performance

我的查询未使用任何索引。查询执行全表扫描。我该怎么做才能避免这种情况?

explain select * from 
timed_delivery_messages 
where start_time <= '06:39' 
and end_time > '06:39' 
and mode='Active' 
and rotation_weight like '%,45,%' 
and substr(day_of_week, 2, 1) = 'T' 
limit 1;

解释计划

+----+-------------+-------------------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table                   | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------------------------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | timed_delivery_messages | ALL  | NULL          | NULL | NULL    | NULL |   22 | Using where |
+----+-------------+-------------------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

表:

mysql> show create table timed_delivery_messages\G
*************************** 1. row ***************************
       Table: timed_delivery_messages
Create Table: CREATE TABLE `timed_delivery_messages` (
  `row_create` datetime DEFAULT NULL,
  `row_mod` datetime DEFAULT NULL,
  `rule_id` int(11) NOT NULL,
  `start_time` time DEFAULT NULL,
  `end_time` time DEFAULT NULL,
  `day_of_week` varchar(7) DEFAULT NULL,
  `rotation_weight` varchar(50) DEFAULT NULL,
  `mode` varchar(10) DEFAULT 'active',
  `long_message` varchar(256) DEFAULT NULL,
  `short_message` varchar(256) DEFAULT NULL,
  PRIMARY KEY (`rule_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

1 个答案:

答案 0 :(得分:0)

您必须使用列创建一个组索引 start_timeend_timemode

并且还尝试将day_of_week包含在此索引中。也许它会加速你的系统