我的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
我很关心索引,但我不确定如何在这里使用它们。
感谢您提供任何帮助或建议。
答案 0 :(得分:-1)
accounts: INDEX(user_guid, name, account_id)
users: INDEX(hidden, account_id)
请为每张表提供SHOW CREATE TABLE
;有了这个,我们可能会有进一步的建议。