2016-03-03 55 views
0

指數我有一些推薦表mysql命令仍用文件排序

CREATE TABLE IF NOT EXISTS `testimonials` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL, 
    `description` text COLLATE utf8_unicode_ci NOT NULL, 
    `user` varchar(255) COLLATE utf8_unicode_ci NOT NULL, 
    `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 
    `status` enum('active','test','inactive','') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'active', 
    `t_order` int(11) NOT NULL, 
    PRIMARY KEY (`id`), 
    UNIQUE KEY `t_order` (`t_order`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=26 ; 

和一些簡單的任務:使人工分揀。


SELECT查詢如下:

mysql> EXPLAIN SELECT t_order, id, title, description FROM testimonials WHERE status = 'active' ORDER BY t_order DESC; 

輸出:

| id | select_type | table  | type | possible_keys | key | key_len | ref | rows | Extra      | 

| 1 | SIMPLE  | testimonials | ALL | NULL   | NULL | NULL | NULL | 23 | Using where; Using filesort | 

ORDER BY使用索引字段,但仍EXPLAIN顯示Using filesort

爲什麼無法從索引執行排序? 查詢的東西?

感謝)

回答

1

key的列顯示了所使用的索引和它`空。使用僅意味着你使用的是WHERE to restrict the rows。對於您的查詢

ALTER TABLE testimonials ADD KEY(status,t_order) 

是最好的指數,假設你有足夠的行這樣的指數纔有意義。(極少數行的表掃描比索引更快)

+1

謝謝。對我來說,這個問題不夠多行。多於1000行MySQL使用索引。 – alquist42