我用的是下面的sql语句,可是执行起来特别慢,执行一个功能要一分钟左右
谁能帮帮我给优化一下啊
select count(distinct(DI.LoginName)) as DealerCount
from AC_DealerBaseInfo as DI,AC_DealerSeries as DS,[360che]..[AC_CarModel_Series] as CS,[Franchiser]..[AC_ChinaCityRelation] as CC
where 
   DI.LoginName=DS.LoginName
and
   DS.CarSeriesId=CS.SeriesId
and
   DI.CityId=CC.F_Id
and CC.F_ProvinceId=@F_ProvinceId
and CS.BrandId=@BrandId
and DI.IsCharge=@IsCharge
and DI.IsEnable=@IsEnableselect count(distinct(DI.LoginName)) as DealerCount
from AC_DealerBaseInfo as DI,AC_DealerSeries as DS,[360che]..[AC_CarModel_Series] as CS,[Franchiser]..[AC_ChinaCityRelation] as CC
where DI.LoginName=DS.LoginName
and DS.CarSeriesId=CS.SeriesId
and DI.CityId=CC.F_Id
and CC.F_ProvinceId=@F_ProvinceId
and BrandId=@BrandId
select count(distinct(BP.LoginName)) as AllPass from [360Franchiser]..[AC_DealerBaseInfo] as BI,AC_DealerSeries as DS,[360che]..[AC_CarModel_Series] as CS, [360Franchiser]..[AC_CheckExtendDealerBaseInfoPic] as BP,[Franchiser]..[AC_ChinaCityRelation] as CC
where BI.LoginName=BP.LoginName and BI.CityId=CC.F_Id and BI.LoginName=DS.LoginName and DS.CarSeriesId=CS.SeriesId
 and OperateType='Pass' and BI.LoginName not in(select LoginName from [360Franchiser]..[AC_CheckExtendDealerBaseInfoPic] where OperateType <> 'Pass' or  isnull(OperateType,'')='')
and CC.F_ProvinceId=@F_ProvinceId and CS.BrandId=@BrandId and BI.IsCharge=@IsCharge and BI.IsEnable=@IsEnable

解决方案 »

  1.   

    第一.二个没什么可优化的了 连接字段加索引第三的not in利用不了索引  数据量不大的话可以改写为not exists isnull函数不要使用 用case when 代替
      

  2.   


    isnull那个使用是因为每个LoginName 在该表会有三条数据,当该LoginName对应的TYpe都是Pass的时候才通过,否则是不通过
      

  3.   

    数据量不大! 就是慢,查询两百多条数据要近一分钟。
    全国所有的省份,每个省份走一下上面的sql语句,看了一下大约每个省份要两秒钟……
      

  4.   

    distinct改用group by 试试
      

  5.   

    围观学习中...作为初学者,我感觉那个not in用的不合适,会进行表扫描的
      

  6.   

    将Not in寻找替代方案 
      

  7.   

    我把涉及到not in 的语句去掉之后,效率还是没有提高。
      

  8.   

    在WHERE 条件 字段上加索引,然后加 强制 索引 查询 
      

  9.   

    用类似 select column1,column2,..column from mytable with(nolock index(index_name)) 试一下
      

  10.   

    建议:第一:检查表上的索引,对于查询语句之后的条件,条件字段最好是在索引内。(表上的索引不要建立太多。太多也会影响查询效率。合理就好)
    第二:在多表连接用到inner join 的时候,如果tbB表数据大于tbA表的时候,语句尽可能写成select * from tbA as a inner hash join tbB as b on a.字段=b.字段对数据的检索效率有一定的提高。个人建议,仅供参考。