使用group by和sum优化查询

时间:2018-04-10 21:08:24

标签: mysql query-optimization

以下查询执行8秒

SELECT `currency`, sum(amount) as amount
FROM `p_transactions`
WHERE `member_id` = 2051
AND `transaction_type` = 'debit'
AND `transaction_status` = 12
GROUP BY `currency`

但是如果删除GROUP BY currency部分,它最多短按2秒。有没有办法加快这个查询?

索引是复合索引(member_id,transaction_type,transaction_status)。

1 个答案:

答案 0 :(得分:0)

尝试将索引更改为:

(member_id, transaction_type, transaction_status, currency)