某梦的系统,数据23.8万,调用最新文章:
SELECT arc.*,tp.typedir,tp.typename,tp.corank,tp.isdefault,tp.defaultname,tp.namerule,
        tp.namerule2,tp.ispart,tp.moresite,tp.siteurl,tp.sitepath        
        FROM `dede_archives` arc LEFT JOIN `dede_arctype` tp on arc.typeid=tp.id        
         WHERE  arc.typeid IN (6) And  arc.arcrank > -1    ORDER BY arc.sortrank desc  LIMIT 0,10;
去掉order by 执行很快,加上5~7秒之间。
表索引:
操作 键名 类型 唯一 Packed 字段 基数 整理 Null Comment
编辑  删除  PRIMARY BTREE 是 否 id 238281 A
编辑  删除  channel BTREE 否 否 channel 1 A
编辑  删除  ismake BTREE 否 否 ismake 3 A
编辑  删除  sortrank BTREE 否 否 sortrank 238281 A
编辑  删除  typeid BTREE 否 否 typeid 105 A
EXPLAIN结果:
id  select_type  table  type  possible_keys  key  key_len  ref  rows  Extra
1  SIMPLE  arc  index  typeid  sortrank  4  NULL  215  Using where
1  SIMPLE  tp  const  PRIMARY  PRIMARY  2  const  1   
尝试组合索引:typeid、arcrank、sortrank,执行时间没变化。尝试不同组合索引无果。

解决方案 »

  1.   

    调用最新文章不是应该ORDER BY arc.id desc吗
      

  2.   

    换成ORDER BY arc.id desc,结果是一样的,还是5~7秒
      

  3.   

    看解释, 执行速度应该不会慢啊...
    不要用in, 尝试对 arc.typeid, arc.arcrank  建联合索引,  tp.id 建索引
    如果还是解释使用索引 arc.sortrank , 建议强制使用上面的联合索引试试
      

  4.   

    强制索引有效,但不是使用arc.typeid, arc.arcrank ,而是使用 arc.typeid, arc.id 组合,使用order by id排序。并删除了sortrank索引,测试基本<0.01S,但原因不明。
      

  5.   

    create index xxx on dede_archives (typeid,arcrank);
    create index xxx on dede_archives (typeid,sortrank);
    create index xxx on dede_arctype (id);