MySQL复制到tmp表导致挂起

时间:2015-12-02 10:21:38

标签: mysql

我有一个简单的'查询:

SELECT searchterm, count(searchterm) as intNumHits
FROM site_searches 
WHERE searchdate >= 20151202 AND searchdate <= 20151202 
GROUP BY searchterm 
ORDER BY intNumHits DESC 
LIMIT 50;

site_searches是&gt; 750万行。

searchterm&amp; searchdate已编入索引。

根据此处的其他答案, max_heap_table_size tmp_table_size 会受到质疑。我的设置是:

  

max_heap_table_size:16777216

     

tmp_table_size:264241152

但是,当我运行查询时,它似乎挂起&#34;复制到tmp表&#34;。

如果我删除了count(searchterm)和GROUP BY选项,则查询会运行,但仍需要19秒!

有人有任何建议吗?

编辑:

在查询上运行EXPLAIN会产生以下结果:

id = 1
select_type = SIMPLE
table = site_searches
type = index
possible_keys = searchdate
key = searchterm
key_len = 767
ref = NULL
rows = 7431801
Extra = Using where; Using temporary; Using filesort

由于

1 个答案:

答案 0 :(得分:2)

查看searchdate数据类型是否为int?如果它的字符,然后使用引号ex。 '20151202'

    SELECT searchterm, count(searchterm) as intNumHits
    FROM site_searches 
    WHERE searchdate >= '20151202' AND searchdate <= '20151202' 
    GROUP BY searchterm 
    ORDER BY intNumHits DESC 
    LIMIT 50;