+ -
当前位置:首页 → 问答吧 → mysql中order by索引无效

mysql中order by索引无效

时间:2011-08-02

来源:互联网

创建表:
CREATE  TABLE `doh`.`test` (
  `id` INT NOT NULL AUTO_INCREMENT ,
  `name` VARCHAR(45) NULL ,
  `age` INT NOT NULL DEFAULT 0 ,
  PRIMARY KEY (`id`) ,
  INDEX `age_index` USING BTREE (`age` DESC) )
ENGINE = InnoDB;

explain select id,name,age from test order by age desc limit 10
得到的结果
mysql> explain select id,name,age from test order by age desc limit 10;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
|  1 | SIMPLE      | test  | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using filesort |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set (0.00 sec)

还是使用filesort,并没有使用age_index这个索引,这是什么原因呢?

作者: funkworld   发布时间: 2011-08-02

你有多少数据量啊,以及过滤性如何,也即不同的值有多少个??

作者: jinguanding   发布时间: 2011-08-02

USING BTREE (`age` DESC) 是什么意识?

作者: kerlion   发布时间: 2011-08-02

数据量不多,几百条数据。
age这个字段的值分布范围也就是在20~40之间了。
难道这样子优化器分析得出的是全表扫描比使用索引来得更快?

作者: funkworld   发布时间: 2011-08-02



QUOTE:原帖由 kerlion 于 2011-8-2 14:23 发表
USING BTREE (`age` DESC) 是什么意识?

btree索引专用于and or?
mysql里也就那么几种索引,full-text,btree,rtree,hash?

作者: funkworld   发布时间: 2011-08-02