MySQL 相关子查询 SUM() ORDER BY
是否有办法优化以下查询:
SELECT
t1.id,
(SELECT SUM(col1) FROM table_name_two t2 WHERE t2.name LIKE CONCAT('%',t1.name)) AS col1_count
FROM
table_name_one t1
ORDER BY
col1_count DESC
使用 ORDER BY col1_count DESC
需要很长时间。
谢谢。
Is there anyway to optimize the following query:
SELECT
t1.id,
(SELECT SUM(col1) FROM table_name_two t2 WHERE t2.name LIKE CONCAT('%',t1.name)) AS col1_count
FROM
table_name_one t1
ORDER BY
col1_count DESC
Using ORDER BY col1_count DESC
takes a long time.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需在连接的
on
子句中进行正常连接即可:这样应该方式更快 - 它基本上是一个查询而不是“n”个查询,尽管它赢了使用带有前导
%
的LIKE
运算符无法从索引中获得任何帮助Just make a normal join with your comparison in the join's
on
clause:It should be way faster this way - it's basically one query instead of "n" queries, although it won't get any help from indexes using the
LIKE
operator with a leading%