Mysql
 sql >> Database >  >> RDS >> Mysql

ottimizza la query con il campo del tipo di data in mysql

Suggerirei di scrivere la query come:

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;

Questa query può sfruttare un indice su incomes(deleted_at, date, amount) :

create index idx_incomes_deleted_at_date_amount on incomes(deleted_at, date, amount)