mysql oder by 使用index一例

season0891發表於2012-05-31

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use lvh
Database changed
mysql> explain select * from job_queue where 1=1  order by opt_time desc ;
+----+-------------+-----------+------+---------------+------+---------+------+--------+----------------+
| id | select_type | table     | type | possible_keys | key  | key_len | ref  | rows   | Extra          |
+----+-------------+-----------+------+---------------+------+---------+------+--------+----------------+
|  1 | SIMPLE      | job_queue | ALL  | NULL          | NULL | NULL    | NULL | 454244 | Using filesort |
+----+-------------+-----------+------+---------------+------+---------+------+--------+----------------+
1 row in set (0.00 sec)

mysql> explain select * from job_queue where 1=1  order by opt_time desc limit 100,10;
+----+-------------+-----------+-------+---------------+--------------+---------+------+------+-------+
| id | select_type | table     | type  | possible_keys | key          | key_len | ref  | rows | Extra |
+----+-------------+-----------+-------+---------------+--------------+---------+------+------+-------+
|  1 | SIMPLE      | job_queue | index | NULL          | ind_opt_time | 9       | NULL |  110 |       |
+----+-------------+-----------+-------+---------------+--------------+---------+------+------+-------+
1 row in set (0.00 sec)

mysql> explain select * from job_queue  order by opt_time desc limit 100,10;
+----+-------------+-----------+-------+---------------+--------------+---------+------+------+-------+
| id | select_type | table     | type  | possible_keys | key          | key_len | ref  | rows | Extra |
+----+-------------+-----------+-------+---------------+--------------+---------+------+------+-------+
|  1 | SIMPLE      | job_queue | index | NULL          | ind_opt_time | 9       | NULL |  110 |       |
+----+-------------+-----------+-------+---------------+--------------+---------+------+------+-------+
1 row in set (0.00 sec)

mysql>

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/90618/viewspace-731553/,如需轉載,請註明出處,否則將追究法律責任。

相關文章