optimize query with date type field in mysql

后端 未结 1 948
太阳男子
太阳男子 2021-01-28 06:00

I currently have the following query prepared:

select sum(amount) as total 
  from incomes 
 where (YEAR(date) = \'2019\' and MONTH(date) = \'07\') 
   and incom         


        
相关标签:
1条回答
  • 2021-01-28 06:06

    I would suggest writing the query as:

    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;
    

    This query can take advantage of an index on incomes(deleted_at, date, amount):

    create index idx_incomes_deleted_at_date_amount on incomes(deleted_at, date, amount)
    
    0 讨论(0)
提交回复
热议问题