我有如下表:爲什麼如果我查詢更多列,MYSQL不會使用相同查詢的索引?
create table stuff (
id mediumint unsigned not null auto_increment primary key,
title varchar(150) not null,
link varchar(250) not null,
time timestamp default current_timestamp not null,
content varchar(1500)
);
如果我解釋查詢
select id from stuff order by id;
然後它說,使用他們的主鍵訂購結果的指標。但有了這個查詢:
select id,title from stuff order by id;
EXPLAIN說沒有可能的鍵,它訴諸於filesort。
這是爲什麼?不是某個行的數據一起存儲在數據庫中嗎?如果它只能在查詢id時使用索引來排序結果,那麼爲什麼在查詢中添加其他列會產生影響?主鍵已經確定了該行,所以我認爲它應該在第二種情況下使用主鍵進行排序。
你能解釋一下爲什麼這是不是這樣的?
我不知道答案,但在這裏是如何工作的.. http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html – DhruvPathak 2011-03-22 06:07:09
沒有的情況下,列出索引無法使用的地方似乎適用於此。 – Tom 2011-03-22 06:35:48
如果您想了解更多有關這些內容的內容而不會深入瞭解,可能會對開發人員感興趣[SQL索引教程(涵蓋MySQL)](http://use-the-index-luke.com/)。您的特定問題在第一章 - [索引解析](http://use-the-index-luke.com/de/sql/anatomy)中得到解答。 – 2011-03-22 07:02:16