Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

otimizar consulta com campo de tipo de data no mysql


Sugiro escrever a consulta como:
select sum(i.amount) as total
from incomes i
where i.date >= '2019-07-01' and
      i.date < '2019-08-01' and
      i.deleted_at is null;

Esta consulta pode tirar proveito de um índice em incomes(deleted_at, date, amount) :
create index idx_incomes_deleted_at_date_amount on incomes(deleted_at, date, amount)