mysql - MySQL查询永远执行
问题描述
我正在尝试提取一个简单的报告。我已经准备了一个查询,但它需要永远执行。
PFB查询:-
select sc.product_name as product_name,
(select count(id) from hg_scratch_card
where product_name=sc.product_name
and (review_received_date between '2019-12-01'
and '2019-12-21') ) as RR_COUNT,
(select count(id) from hg_scratch_card
where product_name=sc.product_name
and (review_updated_date between '2019-12-01'
and '2019-12-21') ) as RU_COUNT
from hg_scratch_card sc
where sc.product_name !=''
order by RR_COUNT desc
limit 3
请建议我如何优化查询。我对所有列都有索引。
解决方案
如果您想要每行 1 行,product_name
那么您应该group by product_name
使用条件聚合:
select product_name,
sum(review_received_date between '2019-12-01' and '2019-12-21') as RR_COUNT,
sum(review_updated_date between '2019-12-01' and '2019-12-21') as RU_COUNT
from hg_scratch_card
where product_name !=''
group by product_name
order by RR_COUNT desc
limit 3
推荐阅读
- input - 使用 put 和 proc 格式在 SAS 中创建新列
- python - Seaborn阴谋冲突
- html-table - 如何在 ckeditor5 配置中向表元素添加类?
- powershell - 如何在while循环中运行命令x次 - powershell
- python - 将表格保存在 .txt 文件中
- firebase - 如何循环来自云 Firebase 集合的数据以获取用户与其他用户之间的距离
- graphql - 如何在 Micronaut GraphQL 中使用 Keycloak JWT 进行身份验证
- .htaccess - 如何保护 Shopware 中的语言商店?
- firebase - 是否可以使用 Firebase 存储将多个图像添加到 Firebase 数据库?- 颤振
- wordpress - 单击即可加载 Google 地图以符合 GDPR