2016-03-03 56 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