Mysql查询(order by)很慢

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql查询(order by)很慢相关的知识,希望对你有一定的参考价值。

当我从另一个表中使用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)上的索引以及可能(你可能不需要)users(col1, col2, ..., coln)

以上是关于Mysql查询(order by)很慢的主要内容,如果未能解决你的问题,请参考以下文章