mysql - R3.8xlarge RDS 数据库需要几天时间才能运行查询

标签 mysql sql amazon-web-services rds

我目前有一个表,其中包含大约 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/

相关文章:

php - 在 url 中获取产品名称而不是 id php

sql - 不同的问题!

amazon-web-services - 如何将 aws_elastic_beanstalk_environment 设置传递给 Terraform 模块

amazon-web-services - 当 AWS 安全组在入站或出站规则中被指定为源/目标时,它是否只看到私有(private)地址?

php - 带有从表 : layout issues 打开的表的 Bootstrap 模式

mysql - 使用 Knex 动态迁移和播种数据库?

php - 需要 INSERT ...SELECT 语句,但我还想手动设置一些字段

.net - 如何在 SQL Server 2012 的 SQLCLR 程序集中使用 TimeZoneInfo

mysql - SQL : splitting a row in two rows

python - “gcc”在 AWS Elastic Beanstalk 上构建 pandas 时失败