我想知道如何更快地进行此类查询。提前谢谢。
SELECT * FROM
(
SELECT * FROM table1 WHERE DATE_FORMAT(date, '%Y-%m-%d') BETWEEN 'someDate' AND 'someDate'
UNION ALL
SELECT * FROM table2 WHERE DATE_FORMAT(date, '%Y-%m-%d') BETWEEN 'someDate' AND 'someDate'
)as unitable
答案 0 :(得分:0)
首先,在没有子查询的情况下重写查询。其次,更改日期比较,这样就不会在日期列上使用任何函数(常量上的函数都可以)。第三,在日期列上添加索引:
CREATE INDEX idx_table1_date on table1(date);
CREATE INDEX idx_table2_date on table2(date);
SELECT *
ROM table1
WHERE date BETWEEN 'someDate' AND 'someDate'
UNION ALL
SELECT *
FROM table2
WHERE date BETWEEN 'someDate' AND 'someDate' ;