Why are these two queries with the only difference being the campaign_id (a foreign key to another table) getting different performance and different EXPLAIN results?
Query 1 - Avg time: 0.21s
SELECT tx_time, campaign_id, tx_amount, tx_status FROM tx WHERE
campaign_id=6963 ORDER BY tx_time DESC LIMIT 2500;
Query 2 - Avg time: 0.29s
SELECT tx_time, campaign_id, tx_amount, tx_status FROM tx WHERE
campaign_id=6946 ORDER BY tx_time DESC LIMIT 2500;
Query 1 vs Query 2 EXPLAIN:
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
UPDATE: After adding (tx_id,tx_time,campaign_id) and (tx_id,tx_time) indexes and running ANALYZE, Query 1 has improved to 0.15s but Query 2 has slowed to 13s. Updated EXPLAINs:
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
Table tx:
CREATE TABLE
tx
(tx_id
bigint(20) unsigned NOT NULL AUTO_INCREMENT,tx_time
timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,campaign_id
int(10) unsigned NOT NULL,tx_amount
decimal(12,5) unsigned 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) DEFAULT NULL,
PRIMARY KEY (tx_id
),
KEYtx_campaign_id
(campaign_id
),
KEYtx_time
(tx_time
) USING BTREE,
KEYtx_amount
(tx_amount
) USING BTREE,
KEYtx_time_campaign_id
(tx_id
,tx_time
,campaign_id
) USING BTREE,
KEYtx_id_time
(tx_id
,tx_time
) USING BTREE,
CONSTRAINTcampaign_idcampaign_id
FOREIGN KEY (campaign_id
) REFERENCEScampaign
(campaign_id
) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=10855433 DEFAULT CHARSET=utf8
SELECT * FROM table1 USE INDEX (col1_index,col2_index)
(see documentation). – CᴏʀʏEXPLAINs
show two tables (actually a 'self-join'). Why the discrepancy?? – Rick James