SSRS的查询速度慢,但SSMS的查询速度快

时间:2019-02-14 08:21:41

标签: sql-server tsql reporting-services

我有以下查询:

    SELECT 
    COUNT(DISTINCT CASE WHEN ResultsA.Followup = 1 THEN ResultsA.Auditors END) as ColumnResults,
    COUNT(DISTINCT CASE WHEN ResultsA.UnannInspectionYN = 1 THEN ResultsA.Auditors END) ColumnResults2,
    COUNT(DISTINCT CASE WHEN ResultsA.UnannInspectionYN = 0 AND ResultsA.Followup = 0 THEN ResultsA.Auditors END) ColumnResults3,
    COUNT(DISTINCT ResultsA.Auditors) ColumnResults4
FROM 
    ResultsA
    INNER JOIN TableB ON TableB.Auditors = ResultsA.Auditors 
    INNER JOIN TableC ON TableC.TableCColumn = ResultsA.TableCColumn AND Country IN (@Country)
WHERE 
exists(Select SupData 
                  from IMP_SUP_DATA 
                  where impID = @id and TableC.TableCColumn = SupData)
AND
    TableB.ColumnA in ( 'Accepted and reviewed' , 'Accepted, but not reviewed', 'Submitted', 'Accepted (provisional)')
AND 
    TableB.Date BETWEEN @FromDate AND @ToDate
AND 
    ISNULL(TableC.Datainfo,'No Data') IN (@Datainfo)

查询来自大量数据,但是当我在SSMS中运行它时,它的速度非常快。我可以回溯几年,查询最多仍会在约10秒内返回。 但是,当我将查询作为报告运行时,在某些结果上可能要花几分钟甚至超时。

我该如何以最佳方式解决此问题?我希望从中学习,以防止将来发生。环境是sql server 2014。

1 个答案:

答案 0 :(得分:0)

OP在评论中提到,问题与SSMS和SSRS中的连接的不同SET选项无关

首先,通过运行报告并查询目录数据库来确认问题出在数据检索操作中

USE ReportServer
SELECT TOP 100 ReportPath, TimeDataRetrieval, TimeProcessing, TimeRendering 
FROM dbo.ExecutionLog2
ORDER BY TimeStart desc

TimeDataRetrieval的值应占整个持续时间的大部分

如果是这种情况,并且具有相同参数的相同查询在SSMS中运行很快,而在SSRS中运行缓慢,请考虑以下更改:

  • 将变量的显式CAST添加为确切类型,作为表中相应列的类型
  • 在WHERE子句中删除ISNULL()
  • 添加OPTION RECOMPILE,因为这是参数化的资源密集型查询

要评估以下查询(但首先要对CAST进行调整以适合正确的数据类型):

SELECT 
    COUNT(DISTINCT CASE WHEN ResultsA.Followup = 1 THEN ResultsA.Auditors END) as ColumnResults,
    COUNT(DISTINCT CASE WHEN ResultsA.UnannInspectionYN = 1 THEN ResultsA.Auditors END) ColumnResults2,
    COUNT(DISTINCT CASE WHEN ResultsA.UnannInspectionYN = 0 AND ResultsA.Followup = 0 THEN ResultsA.Auditors END) ColumnResults3,
    COUNT(DISTINCT ResultsA.Auditors) ColumnResults4
FROM 
    ResultsA
    INNER JOIN TableB ON TableB.Auditors = ResultsA.Auditors 
    INNER JOIN TableC ON TableC.TableCColumn = ResultsA.TableCColumn AND Country IN (CAST(@Country AS VARCHAR(255)) )
WHERE 
EXISTS (Select SupData 
                  from IMP_SUP_DATA 
                  where impID = CAST(@id AS INT) and TableC.TableCColumn = SupData)

AND    TableB.ColumnA in ( 'Accepted and reviewed' , 'Accepted, but not reviewed', 'Submitted', 'Accepted (provisional)')

AND    TableB.Date BETWEEN CAST(@FromDate AS DATETIME) AND CAST(@ToDate AS DATETIME)
AND    ( TableC.Datainfo IN (CAST(@Datainfo AS VARCHAR(255))) OR ( CAST(@Datainfo AS VARCHAR(255))  = 'No Data' AND TableC.Datainfo IS NULL))
OPTION ( RECOMPILE)