Привет. Я пытаюсь оптимизировать этот запрос.
Если в таймфрейме много транзакций, выполнение может занять до 10 секунд в моей локальной среде.
Я попытался создать индекс для столбца create_at, но это не решает проблему, если в таблице много строк (в моей таблице всего 4 млн строк).
Кто-то может порекомендовать несколько советов по оптимизации?
select
count(*) as total,
trader_id
from
(select *
from `transactions`
where `created_at` >= '2018-05-04 10:54:00'
order by `id` desc)
as `transactions`
where
`transactions`.`market_item_id` = 1
and `transactions`.`market_item_id` is not null
and `gift` = 0
group by `trader_id`;
Edit:
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE transactions NULL range transactions_market_item_id_foreign,transactions_trader_id_foreign,transactions_created_at_index transactions_created_at_index 5 NULL 107666 2.41 Using index condition; Using where; Using MRR; Using temporary; Using filesort