select * from notice fid in (一个几百长度的数组)  or  tid in (同一个几百长度的数组) and ltype<>11 order by updated_at目前notice这个表里面大概有36W条记录,并且每天增长量很大,这条语句现在成为了我整个项目的瓶颈,里面用到的四个字段我全有做索引,但是运行时间居然达到5秒甚至8秒
但这个功能又不可缺少有没有办法能加快速度啊?

解决方案 »

  1.   

    可否说详细点?join不是用来连接两个表吗?
     谢谢啦,问题解决后立即给分
      

  2.   

    1.调整查询顺序
    select * from notice where ltype<>11 and fid in(数组) or tid in(数组) order by updated_at 
    估测一下 fid 和 tid 的数组,看哪个数组长度短些,放前面。
    2.采用联合
    select * from notice where ltype<>11 and fid in(数组) union select * from notice where ltype<>11 and fid in(数组) order by update_at你自己测试下,看看有不有用
      

  3.   

    直接说吧。
    ltype<> 11是FULL TABLE SCAN.
    所以你这条语句对于表NOTICE而言是FULL TABLE SCAN。没有用到任何索引!你这里面还有OR操作!如果换成UNION他会对主表多扫描一次,浪费资源!所以还是保留的好!
    建议对NOTCIE进行分表。条件为ltype 是否等于11.这样查询条件就只要查询LTYPE<>11的那张小表了。
      

  4.   

    学习,不知道join要怎么用才好
      

  5.   

    呵呵,不好意思,我忘记了索引,用or分割的条件,如果or前的语句有索引,后面的没,则不回用到。
      

  6.   

    这样的话不用ltype<>11 而是改成所有可能情况 ltype=1 or ltype =2 or ltype=3 这样会好点不?
      

  7.   

    使用临时表的可能性也不大,因为数据量增长很快,ltype=11那个表也会马上达到这么总表这么大,还是慢
      

  8.   

    select * from notice fid in (一个几百长度的数组)  or  tid in (同一个几百长度的数组) and ltype <>11 order by updated_at 一个几百长度的数组:存入临时表时select * from  (select * from notice where ltype <>11) a inner join
    on lsb b on (a.fid=b.id or a.tid=b.id) order by updated_at 要fid、tid、ltype上建立索引
      

  9.   

    IN的效率低,而且你要重复使用两次,最好存入临时表中
    在fid、tid、ltype上建立索引
      

  10.   

    将你的数组放入临时表再与工作表连接,
    select * from  (select * from notice where ltype <>11) a inner join
    on lsb b on (a.fid=b.id or a.tid=b.id) order by updated_at
    在相关字段上建立索引,试试速度吧
      

  11.   

    select * from notice fid in (一个几百长度的数组)  or  tid in (同一个几百长度的数组) and ltype <>11 order by updated_at试下:
    select * into 临时表 from notic where ltype>11 UNION ALL select * from notic where ltype <11
    select * from 临时表 where fid in (一个几百长度的数组)  or  tid in (同一个几百长度的数组)
      

  12.   

    你的方法试了一下,建立临时表用了18秒,运行SQL用了4分0.63秒
      

  13.   

    将一个几百长度的数组:存入临时表时 ?
    EXPLAIN一下SQL语句,贴结果出来看看
      

  14.   

    select * from  (select * from notice where ltype <>11) a inner join
    on lsb b on (a.fid=b.id or a.tid=b.id) order by updated_at 
    在fid、tid、ltype、LSB的ID上建立索引
      

  15.   

    给你看看这个:
    http://blog.chinaunix.net/u/29134/showart_1332349.html