简体   繁体   English

优化sql查询,即使对小数据也太慢

[英]Optimize the sql query, too slow even on small data

Basically I am trying to get total sum of count from words matched for each url. 基本上,我试图从匹配每个URL的单词中获取计数的总和。 I have this sql query: 我有这个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')

I am using this query to get this 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 is 我原来的表结构是

ID | URL  |  word  | count

But the problem is, this small query is taking too much time. 但是问题是,这个小的查询要花费太多时间。 7+ seconds to run above query on a few thousand rows. 7秒钟以上即可在数千行上运行以上查询。 How can I optimize this query? 如何优化此查询?

I got this syntax from another site but it is giving error. 我从另一个站点获得了此语法,但是它给出了错误。

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.

Pre-aggregate: 预汇总:

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')

Use a JOIN instead of a subquery: 使用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

Your originally query runs slowly in MySQL because MySQL is executing the subquery for each row of the result set. 您最初的查询在MySQL中运行缓慢,因为MySQL正在为结果集的每一行执行子查询。 You can fix this by doing the aggregation once and joining the results in: 您可以通过执行一次汇总并将结果加入以下内容来解决此问题:

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') 

Other databases support a class of functions called window functions that make this easier. 其他数据库支持一类称为窗口函数的函数,这使此操作变得更加容易。 MySQL does not support these. MySQL不支持这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM