PRINT '-------------------------------TEMP--------------------------------------------'
SELECT DISTINCT ZipCode
INTO #ZipCodes
FROM tb_PROD_ADDRESS_RANGE par
WHERE par.DeletedFlag = 0
SELECT
FileStatusId
,FileStatusReasonId
FROM tb_RECORD_IMPORT as import
LEFT JOIN #ZipCodes AS pr ON import.ZIP_CODE = pr.ZipCode
PRINT '-------------------------------CTE--------------------------------------------'
;WITH ZipCodes AS (
SELECT DISTINCT ZipCode
FROM tb_PROD_ADDRESS_RANGE par
WHERE par.DeletedFlag = 0
)
SELECT
FileStatusId
,FileStatusReasonId
FROM tb_RECORD_IMPORT as import
LEFT JOIN ZipCodes AS pr ON import.ZIP_CODE = pr.ZipCode
ABOVE is the actual sql from the actual procedure.
I've got a proc that is lagging in all environments, and it's lagging on a CTE, and the process runs better with a temp table.
It's part of a larger proc, but it lags on this spot, regardless where it may be moved in the process.
What I don't understand is why this would be the case. The target table isn't THAT big and the CTE equates to 910 records.
When I look at the impact of this chunk of code, the CTE seems like it should perform better. But when running as part of the bigger package/procedure, it's the spot where things grind to a stand still.
I don't think the problem is the CTE but something else in the internal guts. I've had people suggest parameter sniffing, but there are no parameters!.
Statistics are up-to-date. Are there problems with SSIS hanging on to memory or something that would be impacting this?
***********************************EPILOGUE********************************************************* Work took over, and since this problem had been 'fixed' and bigger fires were burning there was a delay...
Before getting the answer about the best method to share the xml for the execution plan, I had gotten as far as generating the execution plan as recommended by the good people in the comments...
and that had the answer although I'm still puzzled why the difference is reverse of the test below, but that's why it's important to test sql code within the context of being used...
The execution plan for the temp table solution is a typical plan with some seeks and parallelization happening. many objects, not sure what everything means yet. There is even a suggested index.
However, The CTE solution plan is very easy to grasp. It is one action, 100%. A full table scan.
I suspect the real problem isn't even the full table scan in an of itself, but the fact that this chunk of code is sandwiched around a lot of other code that is tying up resources.
-------------------------------TEMP--------------------------------------------
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'tb_PROD_ADDRESS_RANGE'. Scan count 1, logical reads 4215, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 141 ms, elapsed time = 143 ms.
(910 row(s) affected)
(206949 row(s) affected)
Table '#ZipCodes___________________________________________________________________________________________________________00000000017C'. Scan count 17, logical reads 4, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'tb_RECORD_IMPORT'. Scan count 17, logical reads 22157, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 1822 ms, elapsed time = 1378 ms.
-------------------------------CTE--------------------------------------------
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
(206949 row(s) affected)
Table 'tb_RECORD_IMPORT'. Scan count 1, logical reads 20027, physical reads 0, read-ahead reads 202, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 140 ms, elapsed time = 1121 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.