解决方法:
1,优化sql语句。
2,将sql语句利用字符串构造,对于从几百个以上的数据中取出几个值来说,可以直接将值取出。
即:当使用exists或in时,若真正的数据占表中的比例小于10%,可以全部取出,构造sql语句处理。
DECLARE @Query VARCHAR(500)
SET @Query = ”
SET @Query = ‘
SELECT ISNULL(COUNT(1),0) as SalesCount, ISNULL(sum(b.Cash), 0) as Amount
FROM bdOrder a(NOLOCK)
JOIN bdAchievement b(NOLOCK)
ON a.Orderno = b.Orderno
WHERE OrderDate >=”’ + CONVERT(VARCHAR(10),@OrderDate) + ”’ ‘ + ‘ AND OrderDate < ''' + CONVERT(VARCHAR(10),Dateadd(day, 1, @OrderDate),120) + ''''
+ ' And OrderStatus NOT IN (''01'', ''61'',''04'',''21'',''22'')'
DECLARE @DepartmentIDS VARCHAR(500) //存储小范围的数据
SET @DepartmentIDS = ''
SELECT @DepartmentIDS = @DepartmentIDS + ',' + CONVERT(VARCHAR(4),pdr.SlaveDepartmentID)
FROM permDepartmentRelation pdr WHERE pdr.MasterDepartment = @DepartmentID
IF (@DepartmentIDS <> ”)
BEGIN
SET @DepartmentIDS = SUBSTRING(@DepartmentIDS,2,len(@DepartmentIDS))
END
SET @Query = @Query + ‘ AND b.DepartmentID IN(‘ + @DepartmentIDS + ‘)’
EXEC(@Query)
3,若利用了join,且where中有条件对这个连接表进行筛选,则可将条件直接放在join时进行筛选,以减少IO吞吐。
即:利用join时,where中的条件主要针对于主表。