【发布时间】:2017-03-23 07:26:09
【问题描述】:
我有一张平桌,大约有 10mio 行,每行有 15 列。 索引设置为 column_1、column_2、column_3 和 my_time。
SELECT Date(my_time) my_time,
count(DISTINCT column_1) c_c1,
count(DISTINCT column_2) c_c2
FROM `table_name`
WHERE `column_3` in (10,11,100,50,213,756)
AND Date(my_time) > '2016-09-01'
AND Date(my_time) < '2016-09-30'
GROUP BY Date(my_time)
ORDER BY Date(my_time) ASC
结果大约需要 20-30 秒。
有谁知道,如何改进这个查询,也许是子查询? 如果是子查询,能否给我一个示例查询,如何提高性能?
谢谢!
【问题讨论】:
-
发送EXPLAIN 并将其发布在您的答案中。
-
尝试更改为 my_time BETWEEN '2016-09-01 00:00:00' AND '2016-09-30 23:59:59'
-
id: 1 select_type: SIMPLE table: table_name type: ALL possible_keys: my_time,column_3 key: NULL key_len: NULL ref: NULL rows: .... Extra: Using where;使用文件排序
标签: mysql select count subquery distinct