[英]How can I optimize this SQL query with JOINS and nested SELECT?
我的 SQL 查询需要大量时间来执行,因为交易表在某种程度上非常庞大。 我正在寻找提高此查询性能的方法。
SELECT users.user_id, users.name as user, IFNULL(SUM(IF(transactions.recipient_account = users.account_id,transactions.money,0-transactions.money)),0) as total
FROM users as users
JOIN
(SELECT account_id as system_account FROM accounts WHERE user_guid IS NULL AND name IS NULL LIMIT 1) as tmp
LEFT JOIN (transactions as transactions)
ON (users.account_id IN (transactions.sender_account,transactions.recipient_account))
WHERE (users.hidden = 0)
AND ((transactions.flags & 1) = 0
OR transactions.flags IS NULL)
GROUP BY users.user_guid
ORDER BY total DESC
LIMIT 0,5
我很喜欢索引,但我不确定如何在这里使用它们。
感谢您的任何帮助或建议。
accounts: INDEX(user_guid, name, account_id)
users: INDEX(hidden, account_id)
请为每张桌子提供SHOW CREATE TABLE
; 有了这个,我们可能会有进一步的建议。
索引食谱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.