select sd.id from sortdata sd 
inner join sorttwo s2 on sd.sorttwo=s2.id 
where s2.oneid=2 order by sd.id  limit 0,8 上面的语句加了一个 desc,如下:
select sd.id from sortdata sd 
inner join sorttwo s2 on sd.sorttwo=s2.id 
where s2.oneid=2 order by sd.id desc limit 0,8 第一条速度很快,第二条速度很慢,请问一下原因?
sortdata索引sorttwo,id
sorttwo索引id,oneidEXPLAIN select sd.id from sortdata sd 
inner join sorttwo s2 on sd.sorttwo=s2.id 
where s2.oneid=2 order by sd.id  limit 0,8 ;
上面二条结果相同,如下:
query result(2 records)
id select_type table type possible_keys key key_len ref rows Extra 
1 SIMPLE sd index sorttwo PRIMARY 4 (NULL) 60903   
1 SIMPLE s2 eq_ref PRIMARY,oneid PRIMARY 4 cixi.sd.sorttwo 1 Using where 

解决方案 »

  1.   

    wwwwA  我的id是主键,应该没有按升序建立,
    怎样按升序建立索引?
      

  2.   

    wwwwA  我的id是主键,应该没有按升序建立, 
    怎样按升序建立索引?不好意思我理解错了.
    可以建立倒序索引么?
    不行的话,
    我上面速度慢的原因是什么?谢谢.
      

  3.   

    create index newid on tt (id desc)
      

  4.   

    速度慢的原因是
    第一种得到JOIN后的结果集后无需排序,直接输出8个就行了。
    而第二种则需要把所有记录按sd.id排序后再选择8条。自然就慢了。
    where s2.oneid=2 order by sd.id 
    这种情况下要看你的 s2.oneid=2 记录有多少,然后才能找到优化的方案。盲目地加个索引意义并不大。http://topic.csdn.net/u/20090526/17/639d78ec-e299-40d0-9c8e-8d5b21229405.html
    http://topic.csdn.net/u/20090520/16/a96a2e90-a935-4460-837e-e52b4557c519.html
    
      

  5.   

    s2.oneid=2 记录有多少
    记录不多,10个样子.create index newid on tt (id desc)另外我建了这个索引,EXPLAIN select sd.id from sortdata sd 
    inner join sorttwo s2 on sd.sorttwo=s2.id 
    where s2.oneid=2 order by sd.id  limit 0,8 ; 
    这条语句我怎样改?用上新的索引
      

  6.   

    elect sd.id from sortdata sd FORCE INDEX(newid)
    inner join sorttwo s2 on sd.sorttwo=s2.id 
    where s2.oneid=2 order by sd.id  limit 0,8 ;
      

  7.   

    如果这样改成如下 (需要建立索引 sorttwo(oneid), sortdata(id) )select sd.id 
    from sortdata sd inner join (select id from sorttwo where oneid=2) s2 on sd.sorttwo=s2.id 
    order by by sd.id  
    limit 0,8 ; 
      

  8.   

    select sd.id 
    from sortdata sd inner join (select id from sorttwo where oneid=2) s2 on sd.sorttwo=s2.id 
    order by by sd.id  
    limit 0,8 ; 上面语句改成:
    select sd.id,sd.title
    from sortdata sd inner join (select id from sorttwo where oneid=2) s2 on sd.sorttwo=s2.id 
    order by by sd.id  
    limit 0,8 ; 
    多加了一个字段,就慢了.换成 *  也快的.
      

  9.   

    select *  from sortdata sd 
    inner join (select id from sorttwo where oneid=7 ) s2 on sd.sorttwo=s2.id 
      order by sd.id desc limit 0,8 花了:37922msselect *  from sortdata sd 
    inner join (select id from sorttwo where oneid=2 ) s2 on sd.sorttwo=s2.id 
      order by sd.id desc limit 0,8 花了15ms
      

  10.   

    估计你的问题和这个贴子中的一样。建议看一下吧。http://topic.csdn.net/u/20090526/17/639d78ec-e299-40d0-9c8e-8d5b21229405.html 
    http://topic.csdn.net/u/20090520/16/a96a2e90-a935-4460-837e-e52b4557c519.html 如果想进一步优化,则需要以下数据。desc sortdata;
    desc sorttwo;show index from sortdata;
    show index from sorttwo;explain 
    select sd.id 
    from sortdata sd inner join (select id from sorttwo where oneid=2) s2 on sd.sorttwo=s2.id 
    order by by sd.id  
    limit 0,8 ; explain 
    select sd.id,sd.title
    from sortdata sd inner join (select id from sorttwo where oneid=2) s2 on sd.sorttwo=s2.id 
    order by by sd.id  ;explain 
    select *  from sortdata sd 
    inner join (select id from sorttwo where oneid=7 ) s2 on sd.sorttwo=s2.id 
    order by sd.id desc limit 0,8 ;
      

  11.   

    select a.id,b.subject from (select sd.id from sortdata sd 
    inner join sorttwo s2 on sd.sorttwo=s2.id
    where s2.oneid=7 order by sd.id desc limit 0,8)a inner join sortdata b on a.id=b.id我看了
    http://topic.csdn.net/u/20090526/17/639d78ec-e299-40d0-9c8e-8d5b21229405.html 
    http://topic.csdn.net/u/20090520/16/a96a2e90-a935-4460-837e-e52b4557c519.html 
    的帖子,和我的问题一下.
    我现在是这样处理的.
    1.我现在sortdata 表中有4万记录,oneid=7数据量很大,在10000-40000之间都是one=7的,我把oneid=1,2,3,4,6,8
    这些类里面也增加了8条记录进来,这样当order by desc时也能及时的找到.
    2.我先取出sd.id ,后面有inner 和order by 索引能起作用.取出8条后,再做一次inner,id索引也能用上,速度也不错.