select *
  from tgw_weibo A
 where (exists (select 1
                  from tgw_follow_infor
                 where platformid = 1
                   and userid = 10001
                   and LENGTH(trim(sinauid)) >= 1
                   and sinauid = A.sina_uid) or exists
        (select 1
           from tgw_follow_infor
          where platformid = 1
            and userid = 10001
            and hostid != 0
            and hostid = A.userid))
 order by id desc;id 是主键 ,现在用explain后得到
请问怎么优化tgw_weibo ,sina_uid和userid的索引都建了

解决方案 »

  1.   

    将EXISTS修改成INNER JOIN,去掉order by id desc;速度如何
      

  2.   

    把or改成union allselect *
      from tgw_weibo A
     where (exists (select 1
      from tgw_follow_infor
      where platformid = 1
      and userid = 10001
      and LENGTH(trim(sinauid)) >= 1
      and sinauid = A.sina_uid)) 
    union all
    select *
      from tgw_weibo A
    where 
     exists
      (select 1
      from tgw_follow_infor
      where platformid = 1
      and userid = 10001
      and hostid != 0
      and hostid = A.userid)
     order by id desc;另外sql优化不是万能的
      

  3.   

    我在200万条的tgw_weibo表里,用union all的方法花了20多秒 
      

  4.   

    按照你的执行计划已经用到了索引而且没有全表查询但是sql语句的执行顺序是从后往前,把能过滤掉最多纪录的条件放后面,所以查询是将条件
    sinauid = A.sina_uid和hostid = A.userid放到前面,用下面的这个语句试试,看看有没有快一点select *
      from tgw_weibo A
     where (exists (select 1
      from tgw_follow_infor
      where sinauid = A.sina_uid
        and LENGTH(trim(sinauid)) >= 1
        and platformid = 1
        and userid = 10001
    ) or exists
      (select 1
      from tgw_follow_infor
      where  hostid = A.userid
           and platformid = 1
           and hostid != 0
           and userid = 10001
    ))
     order by id desc;
      

  5.   

    explain select *
       from tgw_weibo A
      where (exists (select 1
       from tgw_follow_infor
       where platformid = 1
       and userid = 10001
       and LENGTH(trim(sinauid)) >= 1
       and sinauid = A.sina_uid)) explain select *
       from tgw_weibo A
    where  
      exists
       (select 1
       from tgw_follow_infor
       where platformid = 1
       and userid = 10001
       and hostid != 0
       and hostid = A.userid)贴结果
      

  6.   

    你这个表tgw_weibo符合的数据有多少条
      

  7.   

    orselect a.* from tgw_weibo A inner join tgw_follow_infor b
    b.sinauid = A.sina_uid
       where platformid = 1
       and userid = 10001
       and LENGTH(trim(sinauid)) >= 1
        
    explain select a.* from tgw_weibo A inner join tgw_follow_infor b
    b.sinauid = A.sina_uid
       where platformid = 1
       and userid = 10001
       and LENGTH(trim(sinauid)) >= 1
      

  8.   

    改成联查 + union 字段 sinauid 加 函数索引 
     
      

  9.   

    SELECT *
    FROM tgw_weibo a
      INNER JOIN tgw_follow_infor b
    WHERE (b.platformid = 1 AND b.userid = 10001) AND (
    (LENGTH(TRIM(b.sinauid)) >= 1 AND b.sinauid = a.sina_uid)
    OR
            (b.hostid != 0 AND b.hostid = a.userid)
    )
    ORDER BY id DESC
      

  10.   

    贴出你的show index from tgw_follow_infor