我目前有一个表,其中包含大约 1200 万行评论,其中包含以下列:
id
productid
title
price
userid
profilename
helpfulness
score
review_time
summary
text
我的查询如下:
SELECT title, productid as p, count(text) as positive,
(SELECT count(*) FROM `reviews` WHERE productid = p) as total
FROM `reviews`
WHERE text like '%my favorite book%'
GROUP BY productid
ORDER BY positive DESC;
它基本上是寻找评论文本中包含“我最喜欢的书”的所有产品ID,获取每个产品ID匹配的评论数量,然后计算每个产品ID的评论总数。
我在 AWS RDS 上的数据库中拥有此表,其类别设置为我能看到的最快的 r3.8xlarge,但它仍然需要几天的时间才能运行。
现在更奇怪的是,至少对我来说,如果我将文本搜索更改为以下内容:
SELECT title, productid as p, count(text) as positive,
(SELECT count(*) FROM `reviews` WHERE productid = p) as total
FROM `reviews`
WHERE text like '%tim ferriss%' or
text like '%timothy ferriss%' or
text like '%four hour workweek%' or
text like '%4-hour workweek%' or
text like '%four hour body%' or
text like '%4-hour body%' or
text like '%4 hour workweek%' or
text like '%4 hour body%' or
text like '%four hour chef%' or
text like '%4-hour chef%' or
text like '%4 hour chef%'
GROUP BY productid
ORDER BY positive DESC
甚至将数据库等级降低到m3.2xlarge,查询也只需不到20分钟。
我在这里遗漏了什么吗?任何建议都会有帮助,谢谢。
最佳答案
我认为使用条件聚合更容易编写您的查询:
SELECT title, productid as p,
sum(text like '%my favorite book%') as positive,
count(*) as total
FROM `reviews`
GROUP BY productid
ORDER BY positive DESC;
您的原始查询过滤掉了没有正面评价的产品。如果你真的想要这个,那么你可以添加:
HAVING positive > 0
在分组依据
之后。
关于mysql - R3.8xlarge RDS 数据库需要几天时间才能运行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25463979/