我一直在徘徊以下查询是否有更优化的解决方案:
select count(purchases.id)
from purchases right join purchase_items on purchases.item_id=purchase_items.id
where purchases.created_date >= '2017-06-01'
and purchases.created_date < '2017-07-01'
and purchases.cancellation_date is null
and purchases.pending = 0;
提前致谢!
最佳答案
使用内部联接并添加索引:
select count(*)
from purchases p join
purchase_items pi
on p.item_id = pi.id
where p.created_date >= '2017-06-01' and
p.created_date < '2017-07-01' and
p.cancellation_date is null and
p.pending = 0;
purchases(pending, cancellation_date, created_date, item_id)
和purchase_items(id)
上的索引将有助于此查询。