java - Optimize the sql query, too slow even on small data -
basically trying total sum of count words matched each url. have sql query:
select w.url, w.word, w.count, ( select sum(w2.count) wordcounts w2 w2.url = w.url , w2.word in ('search', 'more') ) totalcount wordcounts w w.word in ('search', 'more') i using query kind of result:
url | word | count | total count http://haacked.com/ | more | 61 | 62 http://haacked.com/ | search | 1 | 62 http://feeds.haacked.com/haacked | more | 58 | 59 http://feeds.haacked.com/haacked | search | 1 | 59 http://www.asp.net/privacy | more | 7 | 13 http://www.asp.net/privacy | search | 6 | 13 my original table structure
id | url | word | count but problem is, small query taking time. 7+ seconds run above query on few thousand rows. how can optimize query?
i got syntax site giving error.
select id, url, word, count, sum(count) over(partition url) count_sum wordcounts word in ('search', 'more') order url error code 1064, sql state 42000: have error in sql syntax; check manual corresponds mysql server version right syntax use near '(partition url) count_sum wordcounts word in ('search', 'more')' @ line 2 line 1, column 1 execution finished after 0 s, 1 error(s) occurred.
pre-aggregate:
select w.url, w.word, w.`count`, w3.totalcount wordcounts w join ( select w2.url, sum(w2.`count`) totalcount wordcounts w2 w2.word in ('search', 'more') group w2.url) w3 on w3.url = w.url w.word in ('search', 'more')
Comments
Post a Comment