谁来帮忙看看这些问题啊!!!!!!!!!!

qiudong_5210 2011-08-16 04:44:39
我用的是下面的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=@IsEnable

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 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

...全文
122 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
chtzhking 2011-08-17
  • 打赏
  • 举报
回复
建议:第一:检查表上的索引,对于查询语句之后的条件,条件字段最好是在索引内。(表上的索引不要建立太多。太多也会影响查询效率。合理就好)
第二:在多表连接用到inner join 的时候,如果tbB表数据大于tbA表的时候,语句尽可能写成

select * from tbA as a inner hash join tbB as b on a.字段=b.字段

对数据的检索效率有一定的提高。

个人建议,仅供参考。

Felixzhaowenzhong 2011-08-17
  • 打赏
  • 举报
回复
用类似 select column1,column2,..column from mytable with(nolock index(index_name)) 试一下
Felixzhaowenzhong 2011-08-17
  • 打赏
  • 举报
回复
在WHERE 条件 字段上加索引,然后加 强制 索引 查询
qiudong_5210 2011-08-16
  • 打赏
  • 举报
回复
我把涉及到not in 的语句去掉之后,效率还是没有提高。
勿勿 2011-08-16
  • 打赏
  • 举报
回复
最好是用临时表来提高速度。不然估计有点难了
sz_vcp2007 2011-08-16
  • 打赏
  • 举报
回复
将Not in寻找替代方案
qian_jinli 2011-08-16
  • 打赏
  • 举报
回复
围观学习中...作为初学者,我感觉那个not in用的不合适,会进行表扫描的
geniuswjt 2011-08-16
  • 打赏
  • 举报
回复
distinct改用group by 试试[Quote=引用 5 楼 qiudong_5210 的回复:]
数据量不大! 就是慢,查询两百多条数据要近一分钟。
全国所有的省份,每个省份走一下上面的sql语句,看了一下大约每个省份要两秒钟……
[/Quote]
qiudong_5210 2011-08-16
  • 打赏
  • 举报
回复
数据量不大! 就是慢,查询两百多条数据要近一分钟。
全国所有的省份,每个省份走一下上面的sql语句,看了一下大约每个省份要两秒钟……
快溜 2011-08-16
  • 打赏
  • 举报
回复
没什么优化的,使用变量或函数导致查询优化器选择索引失误,如果数据量大,考虑用临时表过滤一部分。
geniuswjt 2011-08-16
  • 打赏
  • 举报
回复
case when能走索引哦?[Quote=引用 1 楼 fredrickhu 的回复:]
第一.二个没什么可优化的了 连接字段加索引

第三的not in利用不了索引 数据量不大的话可以改写为not exists isnull函数不要使用 用case when 代替
[/Quote]
qiudong_5210 2011-08-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fredrickhu 的回复:]
第一.二个没什么可优化的了 连接字段加索引

第三的not in利用不了索引 数据量不大的话可以改写为not exists isnull函数不要使用 用case when 代替
[/Quote]

isnull那个使用是因为每个LoginName 在该表会有三条数据,当该LoginName对应的TYpe都是Pass的时候才通过,否则是不通过
--小F-- 2011-08-16
  • 打赏
  • 举报
回复
第一.二个没什么可优化的了 连接字段加索引

第三的not in利用不了索引 数据量不大的话可以改写为not exists isnull函数不要使用 用case when 代替

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧