[英]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.