我的 query 是这样的:SELECT * FROM topics ORDER BY updated_at DESC LIMIT 15
现在已经对 updated_at 字段加了索引,但是今天分别在 MySQL 5.5 和 PostgreSQL 9.1 上面测试时发现,PostgreSQL 会用到索引,而 MySQL 却不会,这个怎么回事呢?
MySQL:
mysql> explain select * from topics order by updated_at desc limit 15;
+----+-------------+--------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+----+-------------+--------+------+---------------+------+---------+------+------+----------------+
|  1 | SIMPLE      | topics | ALL  | NULL          | NULL | NULL    | NULL |  991 | Using filesort |
+----+-------------+--------+------+---------------+------+---------+------+------+----------------+
1 row in set (0.00 sec)
PostgreSQL:
development=# explain analyze select * from topics  order by updated_at DESC limit 15;                                                                                                    QUERY PLAN                                                                        
----------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.00..0.57 rows=15 width=52) (actual time=0.049..0.059 rows=15 loops=1)
   ->  Index Scan Backward using index_topics_on_updated_at on topics  (cost=0.00..377.30 rows=10003 width=52) (actual time=0.048..0.053 rows=15 loops=1)
 Total runtime: 0.096 ms
(3 rows)