2013-04-23 89 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納入該指數。也許它會加快你的系統

相關問題