SELECT
case RTPD.Details3 when 'A' then '2' when 'R' then '1' else '3' end as DataTypeOrder,
RTPD.*
FROM RSTTransactionRptDetails RTPD with (nolock)
INNER JOIN RSTPayrollPrintParameters PP with (nolock)
ON RTPD.PayrollNumber = PP.PayrollNumber
AND RTPD.PayrunPeriod = PP.PayrunPeriod
AND RTPD.PeriodRunType = PP.PeriodRunType
AND RTPD.PayrunNo = PP.PayrunNo
WHERE RTPD.ReportCode = '0020'
AND RTPD.PayrunPeriod + RTPD.PeriodRunType + convert(char,RTPD.PayrunNo) IN (@PayrunPeriods1)
AND RTPD.PayrollNumber IN (@PayrollNumber)
ORDER BY CASE WHEN UPPER(ISNULL(PP.CompAmdRptPaygroup,'F')) ='T' THEN RTPD.Details51 END ASC,
CASE WHEN UPPER(ISNULL(PP.CompAmdRptDept,'F')) = 'T' THEN RTPD.Details52 END ASC,
CASE WHEN UPPER(ISNULL(PP.CompAmdRptCostcode,'F')) ='T' THEN RTPD.Details53 END ASC, RTPD.Details4 ASC,RTPD.Details48 DESC, RTPD.Details6 ASC
我尝试使用如下所示的临时表,但是该报告未显示任何数据:
IF OBJECT_ID('tempdb.dbo.#transacRptDetails') IS NOT NULL
DROP TABLE #transacRptDetails
SELECT
case RTPD.Details3 when 'A' then '2' when 'R' then '1' else '3' end as DataTypeOrder,RTPD.*
into #transacRptDetails
FROM RSTTransactionRptDetails RTPD with (nolock)
INNER JOIN RSTPayrollPrintParameters PP with (nolock)
ON RTPD.PayrollNumber = PP.PayrollNumber
AND RTPD.PayrunPeriod = PP.PayrunPeriod
AND RTPD.PeriodRunType = PP.PeriodRunType
AND RTPD.PayrunNo = PP.PayrunNo
WHERE RTPD.ReportCode = '0020'
AND RTPD.PayrunPeriod + RTPD.PeriodRunType + convert(char,RTPD.PayrunNo) IN (@PayrunPeriods1)
AND RTPD.PayrollNumber IN (@PayrollNumber)
ORDER BY CASE WHEN UPPER(ISNULL(PP.CompAmdRptPaygroup,'F')) ='T' THEN RTPD.Details51 END ASC,
CASE WHEN UPPER(ISNULL(PP.CompAmdRptDept,'F')) = 'T' THEN RTPD.Details52 END ASC,
CASE WHEN UPPER(ISNULL(PP.CompAmdRptCostcode,'F')) ='T' THEN RTPD.Details53 END ASC, RTPD.Details4 ASC,RTPD.Details48 DESC, RTPD.Details6 ASC
此查询在4分钟内返回200万条记录,通常会导致超时。由于我在where条件下已经做了我可以做的事情,所以我如何提高它的性能。 表格RSTTransactionRptDetails还会过滤最多的数据(大约需要5分钟才能获得200万条记录)
答案 0 :(得分:0)
您可以先尝试过滤表RSTTransactionRptDetails
吗,这会减少联接结果并使其更轻巧。另外,您也可以只选择所需的字段来代替select *
。
SELECT
case RTPD.Details3 when 'A' then '2' when 'R' then '1' else '3' end as DataTypeOrder,
RTPD.*
FROM (select * from RSTTransactionRptDetails WHERE RTPD.ReportCode = '0020'
AND RTPD.PayrunPeriod + RTPD.PeriodRunType + convert(char,RTPD.PayrunNo) IN (@PayrunPeriods1)
AND RTPD.PayrollNumber IN (@PayrollNumber)) RTPD with (nolock)
INNER JOIN RSTPayrollPrintParameters PP with (nolock)
ON RTPD.PayrollNumber = PP.PayrollNumber
AND RTPD.PayrunPeriod = PP.PayrunPeriod
AND RTPD.PeriodRunType = PP.PeriodRunType
AND RTPD.PayrunNo = PP.PayrunNo
ORDER BY CASE WHEN UPPER(ISNULL(PP.CompAmdRptPaygroup,'F')) ='T' THEN RTPD.Details51 END ASC,
CASE WHEN UPPER(ISNULL(PP.CompAmdRptDept,'F')) = 'T' THEN RTPD.Details52 END ASC,
CASE WHEN UPPER(ISNULL(PP.CompAmdRptCostcode,'F')) ='T' THEN RTPD.Details53 END ASC, RTPD.Details4 ASC,RTPD.Details48 DESC, RTPD.Details6 ASC