[英]Optimize the sql query, too slow even on small data
基本上,我試圖從匹配每個URL的單詞中獲取計數的總和。 我有這個SQL查詢:
select w.url, w.word, w.count, (
select sum(w2.count)
from wordcounts w2 where w2.url = w.url and w2.word in ('search', 'more')
) as totalcount
from wordcounts w
where w.word in ('search', 'more')
我正在使用此查詢來獲得這種結果:
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
我原來的表結構是
ID | URL | word | count
但是問題是,這個小的查詢要花費太多時間。 7秒鍾以上即可在數千行上運行以上查詢。 如何優化此查詢?
我從另一個站點獲得了此語法,但是它給出了錯誤。
select id, url, word, count,
sum(count) over(partition by url) as count_sum
from wordcounts where word in ('search', 'more') order by url
Error code 1064, SQL state 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(partition by url) as count_sum
from wordcounts where word in ('search', 'more')' at line 2
Line 1, column 1
Execution finished after 0 s, 1 error(s) occurred.
預匯總:
select w.url, w.word, w.`count`, w3.totalcount
from wordcounts w
join (
select w2.url, sum(w2.`count`) totalcount
from wordcounts w2
where w2.word in ('search', 'more')
group by w2.url) w3 on w3.url = w.url
where w.word in ('search', 'more')
使用JOIN而不是子查詢:
select w.url, w.word, w.count, sum(w2.count) as totalcount
from wordcounts w
left join wordcounts w2
on w2.url = w.url and w2.word in ('search', 'more')
where w.word in ('search', 'more')
group by w.url, w.word, w.count
您最初的查詢在MySQL中運行緩慢,因為MySQL正在為結果集的每一行執行子查詢。 您可以通過執行一次匯總並將結果加入以下內容來解決此問題:
select w.url, w.word, w.count, wsum.sumcount
from wordcoutns w join
(select w.url, w.word, SUM(w.count) as sumcount
from wordcounts w
where w.word in ('search', 'more')
group by w.url, w.word
) wsum
on wsum.url = w.url and wsum.word = w.word
where w.word in ('search', 'more')
其他數據庫支持一類稱為窗口函數的函數,這使此操作變得更加容易。 MySQL不支持這些。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.