mysql info表,id 主键 自增长 数据20万左右
用order by id 特别慢
select id,title from info order by id desc limit 80100,50; |用时8秒select id from info order by id desc limit 80100,50; |用时0.05秒为什么差这么多呢? 能解释下吗如果我想title一起读出来怎么办呢。难道要分两次读才行吗。

解决方案 »

  1.   

    最大的可能是哪些字段设置有误,延长检索时间。
    id,是id的吗?
      

  2.   

    select id,title from info order by id desc limit 80100,50; |用时8秒这个MYSQL需要在索引文件中获得 50个ID,然后到数据文件去取 titleselect id from info order by id desc limit 80100,50
    而这个MYSQL仅通过访问索引文件就可以获得 ID,不需要再去访问数据文件。
      

  3.   

    这是explain结果:mysql> desc select id ,title from info order by id desc limit 90000, 50;
    +----+-------------+-------+------+---------------+------+---------+------+--------+----------------+
    | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows   | Extra          |
    +----+-------------+-------+------+---------------+------+---------+------+--------+----------------+
    |  1 | SIMPLE      | info  | ALL  | NULL          | NULL | NULL    | NULL | 227903 | Using filesort |
    +----+-------------+-------+------+---------------+------+---------+------+--------+----------------+
    1 row in set (0.00 sec)
      

  4.   

    确实没用到索引。id是主键,order by 也不行吗。实在不解啊另外如果加了where语句。再order by id就用到索引了。这是为什么呢。请赐教!!!mysql> desc select id,title from destoon_sell info where status = 3 order by id desc limit 90000, 50;
    +----+-------------+-------+------+---------------+--------+---------+-------+--------+-----------------------------+
    | id | select_type | table | type | possible_keys | key    | key_len | ref   | rows   | Extra                       |
    +----+-------------+-------+------+---------------+--------+---------+-------+--------+-----------------------------+
    |  1 | SIMPLE      | info  | ref  | status        | status | 1       | const | 198341 | Using where; Using filesort |
    +----+-------------+-------+------+---------------+--------+---------+-------+--------+-----------------------------+
    1 row in set (0.11 sec)
      

  5.   

    试试:
    select id,title from info a 
    inner join 
    (select id from info order by id desc limit 80100,50) b
    on a.id=b.id
      

  6.   

    非常感谢版主大人的回复!select id,title from info where status = 3 order by id desc limit 90000, 50;
    这条语句,explain看是用到索引了。为什么执行SQL的时候还是很慢。也要10多秒!去掉 order by id desc,要快点,0。7秒。还是慢难道这种情况只能 先查出ID组,然用得到的ID组去多次取ID所在行的数据吗。
      

  7.   


    你要看到用到索引是status的索引而不是order by字段的索引,不一样,所以最后还是用到了filesort来排序,所以速度慢
      

  8.   

    给id和title建立一个联合索引试试