我有這個疑問:爲什麼MySQL對這個查詢使用「using_where」?
SELECT *
FROM items i
WHERE i.user_id = 1 AND i.source_id = 34
ORDER BY i.time DESC LIMIT 0, 30;
而且這種複合指數:
idx_user_src_time (user_id, source_id, time)
解釋是,MySQL確實使用該指數顯示:
# id, select_type, table, type, possible_keys, key, key_len, ref, rows, filtered, Extra
'1', 'SIMPLE', 'ui', 'ref', 'PRIMARY,idx_source_id,idx_user_src_time', 'idx_user_src_time', '8', 'const,const', '329', '100.00', 'Using where'
但在額外的列說,他需要執行一些其中。
有人可以解釋我爲什麼MySQL需要使用此查詢執行額外的東西,並沒有足夠的索引?
EDIT
如果我通過子句去除順序的,解釋保持相同(使用相同的索引),但using_where自敗。
EDIT
表看起來像:
CREATE TABLE `items` (`user_id` int(11) NOT NULL,
`item_id` int(11) NOT NULL,
`source_id` int(11) NOT NULL,
`time` varchar(32) NOT NULL DEFAULT '',
PRIMARY KEY (`user_id`,`item_id`)
KEY `idx_iid_user_src_time` (`item_id`,`user_id`,`source_id`,`time`) USING BTREE,
KEY `idx_user_time` (`user_id`,`time_order`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
因爲MySQL認爲這是執行查詢的最佳方式。桌子有多大? –
如果您正在選擇'*',那麼需要拉取可能不在索引中的表列。 – Glenn
項目表上有多少記錄?有多少條記錄的user_id是1,source_id是34? – Kickstart