如何提升分組速度
<p>我有一個擁有超過3B行的大表格,我的查詢現在需要3天才能執行。 </p>
<p>這是我的詢問:</p>
<pre class="brush:php;toolbar:false;">insert into frequencywords (word, frequency, filename)
select word, count(*) as frequency, max(filename)
from allwordstemp
group by word</pre>
<p>基本上,我的查詢是從allwordstemp表格按照word分組,並且我想知道當frequency = 1時的<strong>filename</strong>,這就是為什麼我添加了max(filename),因為filename需要包含在聚合函數中,例如max。如果frequency > 1,我也不需要filename的值。
這兩個表格都在word和filename上有2個索引。 </p>
<p>allwordstemp表格(filename是id檔):</p>
<pre class="brush:php;toolbar:false;">CREATE TABLE `allwordstemp` (
`word` varchar(45) DEFAULT NULL,
`filename` int(11) DEFAULT NULL,
KEY `idx_allwordstemp_word` (`word`),
KEY `idx_allwordstemp_filename` (`filename`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;</pre>
<p>frequencywords表格:</p>
<pre class="brush:php;toolbar:false;">CREATE TABLE `frequencywords` (
`word` varchar(45) DEFAULT NULL,
`frequency` int(11) DEFAULT NULL,
`filename` int(11) DEFAULT NULL,
KEY `idx_frequencywords_word` (`word`),
KEY `idx_frequencywords_frequency` (`frequency`),
KEY `idx_frequencywords_filename` (`filename`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;</pre>
<p>解釋選擇:</p>
<pre class="brush:php;toolbar:false;"> ---- ------------- -------------- -- ---------- ------- --------------- ------------------ ----- --------- ------ ------------ ---------- -------- -----
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
---- ------------- -------------- ------------ ------- --------------- ----------------------- --------- --- --- ------------ ---------- -------------
| 1 | SIMPLE | allwordstemp | NULL | index | NULL | idx_allwordstemp_word | 138 | NULL | 3487864881 | 100.00 | Using index |
---- ------------- -------------- ------------ ------- --------------- ----------------------- --------- --- --- ------------ ---------- ------------- </pre>
<p>我該如何讓我的查詢更快? </p>