2017-04-22 9 views
1

다음 내부 검색 작업으로 구성된 다음 MySQL 쿼리가 있습니다. 그러나Mysql Inner Select Optimization

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 

1 내부 선택 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