2013-04-23 90 views
0

我的查询没有使用任何索引。查询执行全表扫描。我能做些什么来避免这种情况?查询执行全表扫描

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

问题在哪里? – NielsC 2013-04-23 08:18:46

+0

我敢打赌,它是'''','45',%''和'substr(day_of_week,2,1)='T''这些负责全表扫描的'rotation_weight。 – 2013-04-23 08:20:09

+4

另外我看不到'start_time','end_time'和'mode'上的索引 – 2013-04-23 08:20:47

回答

0

您必须创建列 start_time一个指标, end_timemode

也做实验,将day_of_week纳入该指数。也许它会加快你的系统