2017-04-22 133 views
1

我有一个以下的mysql查询,它由一些内部选择操作组成。但它是缓慢摆脱该查询输出..Mysql内部选择优化

SELECT 
      c.topicName as 'topicName', 
      c1 as 'sumOfWordsInTopic', 
      c.word, 
      c.wordCount, 
      c2/c1 AS 'wordProbability' 
      FROM weightallofwordsintopic as c 
      JOIN (
      SELECT topicName, sum(wordCount) AS 'c1' 
      FROM weightallofwordsintopic group by topicName 
      ) AS sub1 ON c.topicName = sub1.topicName 
      JOIN (
      SELECT topicName,word, wordCount AS 'c2' 
      FROM weightallofwordsintopic 

      ) AS sub2 ON c.topicName = sub2.topicName and c.word=sub2.word 

一是内部的选择需要wordCounts的总和与第二个需要wordcounts和他们的外部选择采取分工。有什么办法让它更快一点?感谢您的关注..

回答

0

首先,我不知道为什么你有第二个子查询。我认为这应该做你想做的:

SELECT wwt.topicName, t.topic_cnt as sumOfWordsInTopic, 
     wwt.word, wwt.wordCount, 
     (wwt.word/t.topic_cnt) AS wordProbability 
FROM weightallofwordsintopic as wwt JOIN 
    (SELECT topicName, sum(wordCount) AS topic_cnt 
     FROM weightallofwordsintopic 
     GROUP BY topicName 
    ) t 
    ON wwt.topicName = t.topicName; 

这会加快查询一下。

+0

非常感谢您的关注。 – mstfky