我並不確定,但是這可能會迫使MySQL在日期時間減去日期時迭代所有行。你應該使用EXPLAIN來確保。你可以做這樣的事情解決這個問題:
(t1.dt >= "2010-06-24 00:00:00"
AND t1.dt < ("2010-06-24 00:00:00" + INTERVAL 1 DAY))
編輯:只是使用日期()試過了,似乎MySQL的仍然使用索引:
CREATE TEMPORARY TABLE t1 (
dt DATETIME,
INDEX iDt (dt)
);
INSERT INTO t1 VALUES
(NOW()),
(NOW() - INTERVAL 1 DAY),
(NOW() - INTERVAL 1 DAY),
(NOW() - INTERVAL 2 DAY);
CREATE TEMPORARY TABLE t2 (
dt DATETIME,
INDEX iDt (dt)
);
INSERT INTO t2 VALUES
(NOW()),
(NOW() - INTERVAL 1 DAY),
(NOW() - INTERVAL 1 DAY),
(NOW() - INTERVAL 2 DAY);
# t1: Using index. t2: Using where+index
EXPLAIN EXTENDED
SELECT
*
FROM t1
JOIN t2 ON DATE(t1.dt) = DATE(t2.dt)
我試過類似的:'ON t1.td BETWEEN t2.date AND t2.date + INTERVAL 1 DAY',它使它快一點,但後半部分BETWEEN沒有索引,所以它仍然使用filesort和臨時的。 – aidan 2010-06-24 10:38:44
你可以給一個解釋擴展,看看它說什麼? – simendsjo 2010-06-24 12:19:28