- 查询花费了太多时间来执行,大约 130 分钟。
- 具有索引但查询未使用索引的表。
- 表大小约为 3000 万条记录。
- 请帮助我通过更改(或),(和)条件来重写查询。
- 该查询未在正常执行中使用,因此我们尝试使用强制索引但没有运气。
询问:
SELECT customer_history.customer_id,
customer_history.row_mod,
customer_history.row_create,
customer_history.event_id,
customer_history.event_type,
customer_history.new_value,
customer_history.old_value
FROM customer_history FORCE INDEX (customer_history_n2)
WHERE customer_id >= 1
AND customer_id < 5000000
AND (customer_history.row_mod >= '2012-10-01')
OR (
customer_history.row_create >= '2012-10-01'
AND customer_history.row_create < '2012-10-13'
);
Table structure
CREATE TABLE `customer_history` (
`customer_id` int(11) NOT NULL,
`row_mod` datetime DEFAULT NULL,
`row_create` datetime DEFAULT NULL,
`event_id` int(11) DEFAULT NULL,
`event_type` varchar(45) DEFAULT NULL,
`new_value` varchar(255) DEFAULT NULL,
`old_value` varchar(255) DEFAULT NULL,
KEY `customer_id1` (`customer_id`),
KEY `new_value_n1` (`new_value`),
KEY `customer_history_n1` (`row_create`),
KEY `customer_history_n2` (`row_mod`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
解释计划:
+----+-------------+------------------+------+---------------------+------+---------+------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------------+------+---------------------+------+---------+------+----------+-------------+
| 1 | SIMPLE | customer_history | ALL | customer_history_n2 | NULL | NULL | NULL | 18490530 | Using where |
+----+-------------+------------------+------+---------------------+------+---------+------+----------+-------------+
1 row in set (0