【发布时间】:2015-04-22 19:13:48
【问题描述】:
我有一张很大的桌子,下面的代码需要 990 秒。去完成。 bdate 和 itype 已编入索引。我还需要优化/更改什么?
SELECT s, count(*) as total
FROM `mt_ex_15`
WHERE bdate > '2014-10-01' and bdate < '2014-11-01'
and itype = '3'
group by s
order by total desc
编辑:这是EXPLAIN
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE mt_ex_15 ref itype,bdate,s itype 2 const 44157686 Using where; Using temporary; Using filesort
编辑:我认为我需要优化我的 DB 或 my.cnf,因为即使是以下查询也需要 40 秒。
SELECT count(*) as total
FROM `mt_ex_15`
WHERE bdate > '2015-02-01' and bdate < '2015-03-01'
这里是解释:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE mt_ex_15 range bdate bdate 3 NULL 4494019 Using where; Using index
【问题讨论】:
-
表定义会很好。编辑:因为您的查询最好由
index(bdate,itype,s)提供。您的日期是日期还是日期时间? -
你有
s的索引吗?s有多少个不同的值?由于您正在对计算值进行排序,因此如果s有大量不同的值,则排序可能仍需要一些时间。 -
s 有 8 个不同的值,是的,它已编入索引
-
考虑在 (itype, s, bdate) 上建立一个多列索引。假设 InnoDB,如果针对和索引的简单 COUNT 查询需要很长时间,则需要增加 InnoDB 缓冲池的大小。
标签: mysql database performance indexing