select * from table 效率很低


select * from table 比 select * from table order by id 效率低。


mysql> EXPLAIN SELECT * FROM kn_site limit 1;
+----+-------------+---------+------+---------------+------+---------+------+-------+-------+
| id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows  | Extra |
+----+-------------+---------+------+---------------+------+---------+------+-------+-------+
|  1 | SIMPLE      | kn_site | ALL  | NULL          | NULL | NULL    | NULL | 91548 | NULL  |
+----+-------------+---------+------+---------------+------+---------+------+-------+-------+
1 row in set (0.00 sec)



mysql> EXPLAIN SELECT * FROM kn_site ORDER BY id LIMIT 1;
+----+-------------+---------+-------+---------------+---------+---------+------+------+-------+
| id | select_type | table   | type  | possible_keys | key     | key_len | ref  | rows | Extra |
+----+-------------+---------+-------+---------------+---------+---------+------+------+-------+
|  1 | SIMPLE      | kn_site | index | NULL          | PRIMARY | 4       | NULL |    1 | NULL  |
+----+-------------+---------+-------+---------------+---------+---------+------+------+-------+
1 row in set (0.00 sec)



虽说达到的效果一样,但是还是要把索引字段体现到sql语句里面的。

你可能感兴趣的:(PHP开发)