2

基本上我试图从每个 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.
4

3 回答 3

3

预聚合:

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')
于 2013-05-10T13:13:04.770 回答
1

使用 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
于 2013-05-10T13:15:27.863 回答
1

您最初的查询在 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 不支持这些。

于 2013-05-10T13:21:00.237 回答