繁体   English   中英

Mysql查询(order by)很慢

[英]Mysql query (order by) very slow

当我从另一个表使用 ORDER BY 时,它的工作速度非常慢。 如何进行查询

SELECT u.*, (SELECT COUNT(id) FROM operations o WHERE o.userId=u.userId and o.status=1) as countOperations FROM users u ORDER BY countOperations

您的查询格式不正确。 试试下面的查询:

select col1, col2, ..., coln, count(id) countOperations, SUM(o.sumRUB) sumRUB
from users u
inner join operations o 
  on o.userId=u.userId and o.status=1
group by col1, col2, ..., coln
ORDER BY countOperations

您可能还需要operations(userId, status)索引operations(userId, status)和可能(您可能不需要) users(col1, col2, ..., coln)索引users(col1, col2, ..., coln)

暂无
暂无

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

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