select tc.id,tc.uid,tc.Typeid_new,tc.imgurl,tc.subject,tc.content,tc.Expire,tc.sendtime,tc.BrandName,uc.lv,uc.county from
 tradeCN tc left join UCHOME uc on tc.uid = uc.uid left join 
UCHOME2 ua on uc.uid = ua.uid  where ua.Freeze=1 and tc.isok= 1 order by tc.id desc
有三个表的查询。这个UChome和UChome2的数据量有80W,tradeCN 有20W 数据,这样的查询好慢啊。。谁帮忙优化下啊

解决方案 »

  1.   

    left join 应该改成inner join
      

  2.   

    然后就是增加必要索引
    UCHOME2(Freeze)
    tradeCN (isok)
    以及各表的uid应该有索引
      

  3.   

    再不行就是修改写法,用临时表分步
    不过你数据量20w*80W*80W,有索引应该不必分步的了
      

  4.   

    left join 比inner join效率高吗?
      

  5.   

    1.不要用left Join 如果资料不匹配,会找不到很多资料,改成join
    2.建立表的主键索引。3.把数据量最小的表放在最后面,数据量最大的表放在最前边。
      

  6.   

    这应该不是效率不效率的问题吧。 两个得到的结果都不同。
    要看楼主要得到什么数据就用什么join了。。
    不过最好的办法应该是建索引吧。 在那些有用到与其他表做关联的字段上做索引。。建议楼主可以用“显示估计执行计划”去看下这条语句的执行效率
      

  7.   

    tc.isok= 1 放在ON位置优于where
    order by tc.id desc 若没必要就不会加了
      

  8.   

    他有
    where ua.Freeze=1实际结果left  join 就与inner join相同了,一般inner join效率高些
      

  9.   

    select tc.id,tc.uid,tc.Typeid_new,tc.imgurl,tc.subject,tc.content,tc.Expire,tc.sendtime,tc.BrandName,
    (select uc.lv from UCHOME uc,UCHOME2 ua where tc.uid = uc.uid and uc.uid = ua.uid and ua.Freeze=1) as lv,
    (select uc.county from UCHOME uc,UCHOME2 ua where tc.uid = uc.uid and uc.uid = ua.uid and ua.Freeze=1) as county
    from  tradeCN tc 
    where  tc.isok= 1 
    order by tc.id desc这样试试