Почему эти два запроса, с той лишь разницей, что camp_id (внешний ключ к другой таблице) имеют разную производительность и разные результаты EXPLAIN?
Запрос 1 - Среднее время: 0,21 с
SELECT tx_time, campaign_id, tx_amount, tx_status FROM tx WHERE
campaign_id=6963 ORDER BY tx_time DESC LIMIT 2500;
Запрос 2 - Среднее время: 0,29 с
SELECT tx_time, campaign_id, tx_amount, tx_status FROM tx WHERE
campaign_id=6946 ORDER BY tx_time DESC LIMIT 2500;
Запрос 1 против запроса 2 ОБЪЯСНЕНИЕ:
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE tx NULL index tx_campaign_id tx_time 4 NULL 85591 2.92 Using where
1 SIMPLE tx NULL ref tx_campaign_id tx_campaign_id 4 const 106312 100 Using index condition; Using filesort
ОБНОВЛЕНИЕ: После добавления (tx_id,Индексы tx_time, campaign_id) и (tx_id, tx_time) и запущенный ANALYZE, Query 1 улучшен до 0,15 с, но Query 2 замедлен до 13 с.Обновленные объяснения:
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE tx NULL index tx_campaign_id tx_time 4 NULL 75450 3.31 Using where
1 SIMPLE tx NULL ref tx_campaign_id tx_campaign_id 4 const 117400 100.00 Using index condition; Using filesort
Таблица tx:
CREATE TABLE tx
(
tx_id
bigint (20) без знака NOT NULL AUTO_INCREMENT,
tx_time
отметка времени NOT NULL DEFAULT CURRENT_TIMESTAMP,
campaign_id
int (10) без знака NOT NULL,
tx_amount
десятичный (12,5) без знака NOT NULL,
tx_geo
varchar (2) NOT NULL,
tx_langauge
varchar (511) NOT NULL,
tx_ua
varchar (511) NOT NULL,
tx_ip
varchar (45) NOT NULL,
tx_status
tinyint (255) ПО УМОЛЧАНИЮNULL,
ПЕРВИЧНЫЙ КЛЮЧ (tx_id
),
КЛЮЧ tx_campaign_id
(campaign_id
),
КЛЮЧ tx_time
(tx_time
) ИСПОЛЬЗУЯ BTREE,
КЛЮЧ tx_amount
(tx_amount
) ИСПОЛЬЗОВАТЬ BTREE,
КЛЮЧ tx_time_campaign_id
(tx_id
, tx_time
, campaign_id
) ИСПОЛЬЗОВАТЬ BTREE,
КЛЮЧ tx_id_time
(tx_id
, tx_time
) ИСПОЛЬЗОВАТЬ BTREE,
CONSTRAINT campaign_idcampaign_id
ИНОСТРАННЫЙ КЛЮЧ (campaign_id
) ССЫЛКИ campaign
(campaign_id
) ВКЛ УДАЛИТЬ НЕТ ДЕЙСТВИЙ ОБНОВЛЕНИЕ НЕТ ДЕЙСТВИЙ
) ДВИГАТЕЛЬ = InnoDB AUTO_INCREMENT = 10855433 CHARSET ПО УМОЛЧАНИЮ = utf8