2011-08-26 44 views
1

+ SUM查詢我有以下架構表:標引組通過對MySQL的

CREATE TABLE `wordtrend` (
     `oid` bigint(20) NOT NULL AUTO_INCREMENT, 
     `monitorId` bigint(20) DEFAULT NULL, 
     `nGram` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
     `nGramWord` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
     `negatives` bigint(20) DEFAULT NULL, 
     `neutrals` bigint(20) DEFAULT NULL, 
     `positives` bigint(20) DEFAULT NULL, 
     `total` bigint(20) DEFAULT NULL, 
     `trendCut` datetime DEFAULT NULL, 
     PRIMARY KEY (`oid`) 
    ) ENGINE=MyISAM 
     AUTO_INCREMENT=358539 
     DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci 

是否有可能爲了有效地運行下面的查詢創建索引?

SELECT nGram 
    , nGramWord 
    , SUM(total) AS sTotal 
    , SUM(positives) 
    , SUM(negatives) 
    , SUM(neutrals) 
FROM WordTrend 
WHERE monitorId = 21751021 
    AND trendCut >= '2011-01-01 00:00:00' 
GROUP BY nGram 
     , nGramWord 
ORDER BY sTotal DESC 

我們已經嘗試了以下內容:

KEY `RollupIndex` (`monitorId`,`trendCut`) 
KEY `RollupIndex2` (`monitorId`,`nGram`,`trendCut`) 

,但我們正在「使用哪裏;使用臨時;使用文件排序」的額外列。提前致謝。

+0

查詢返回多少行?需要多少時間才能運行? –

+0

我很好奇這兩個查詢會顯示什麼:'從WordTrend'和'SELECT COUNT(DISTINCT nGram,nGramWord)FROM WordTrend'選擇COUNT(DISTINCT nGram)' –

+0

(警告:它們可能非常慢, nGram,nGramWord)' –

回答

0
  1. 您按無列索引的列進行排序以便您獲取信息使用臨時;使用filesort
  2. 使用where; - 你在哪裏使用或不使用索引來搜索where子句。
0

您使用排序 - 'ORDER BY sTotal DESC',因此sTotal字段上的索引可能會有所幫助。

+0

sTotal是SUM(total)列的名稱。我認爲你不能索引sTotal,對吧? – rreyes1979

+0

哦,你是對的,我沒有注意到它。 – Devart

0

嘗試KEY的不同組合(nGram,nGramWord,total)。

0

爲什麼不使用EXPLAIN來獲得有關性能的有用信息並優化查詢?