我需要此幫助。我有這樣的查詢:使用帶範圍類型查詢和其他標準的索引
SELECT * FROM cart WHERE
(ts_in between 1249077600 AND 1318975199);
以上查詢使用ts_in colomn(範圍類型)上的btree索引。現在我想添加另一個標準。例如:
SELECT * FROM cart WHERE
(ts_in between 1249077600 AND 1318975199) and is_removed=0;
上述查詢不使用ts_in colomn(範圍)上的btree索引。
有人可以告訴我爲什麼以及如何正確實現快速計算。
我創建了測試兩個指標:
CREATE INDEX range_idx_1 using BTREE
ON cart (is_removed, ts_in);
和
CREATE INDEX range_idx_2 using BTREE
ON cart (ts_in , is_removed);
有什麼好笑的是,當我使用這個查詢:
EXPLAIN SELECT id FROM cart WHERE
(ts_in between 1249077600 AND 1318975199) AND is_removed=0;
我收到這樣的結果:
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra
1 | SIMPLE | cart | range | range_idx_1,range_idx_2 | range_idx_1 | 6 | | 17391 | Using where; Using index
上述查詢使用索引但:
EXPLAIN SELECT * FROM cart WHERE
(ts_in between 1249077600 AND 1318975199) AND is_removed=0;
我有這樣的結果:
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra
1 | SIMPLE | cart | ref | range_idx_1,range_idx_2 | range_idx_1 | 1 | const | 77979 | Using where
這不使用索引。
當我嘗試使用INDEX或FORCE INDEX語法時,結果是相同的。在一種情況下,Mysql不使用索引。任何幫助?
請發佈每個查詢的解釋計劃。 – Hammerite
它是MyISAM還是InnoDB表?我猜這是InnoDB? –
我爲以上每個場景添加了執行計劃。看起來第二個查詢不是類型範圍,但爲什麼? –