基本的に、各 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.