Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Stored Procedure Performance Issue

$
0
0
Hi all. I've done my homework on this one and I'm perplexed. I'm an old hand at optimizing queries and stored procedures but I've been solidly in the Ops world for a couple years and may be a bit rusty. I'm hoping someone has some insight.I had a stored procedure that behaved very poorly.[code]Select top 500 STRef2.KeyValue, STRef2.ExtraData From(Select SourceTable.KeyValue From SourceTable EXCEPTSelect KeyValue From TargetTable) ainner join SourceTable STRef2 on a.KeyValue = STRef2.KeyValue[/code]The problem here is the tables are very large - hundreds of millions of rows. So my typical reaction is "Reduce the working set". Which is readily done with a date filter.That lead me to this:[code]DECLARE @hourstoprocess intSET @hourstoprocess = -24;WITH SourceCTE AS ( SELECT KeyValue, ExtraData FROM SourceTable WHERE startdatetimeutc >= DATEADD(hour, @hourstoprocess, getdate())),DestinationCTE AS ( SELECT DISTINCT KeyValue FROM DestinationTable)SELECT TOP 500 a.KeyValue, a.ExtraDataFROM SourceCTE aLEFT OUTER JOIN DestinationCTE b ON a.KeyValue = b.KeyValueWHERE b.KeyValue is null[/code]This is a rough estimation of the code with names changed to protect the innocent. Originally the variable was a parameter but I've removed it and created it as a variable inside the procedure. The trouble here is if I run the code inside the procedure outside of the procedure I get results rather quickly. If I run it inside the procedure it takes 10-50 times longer. Additionally, if I replace the variable in the dateadd function with a value it runs as if it wasn't in the stored procedure.Near as I can tell this might be considered something akin to parameter sniffing. I've seen that the solution to that can be to force a recompile for each run of the procedure. So I've tried that with no luck.I thought maybe if I didn't do the datemath in the CTE it would help so I created a date variable to use for the right side of the where clause in the SourceCTE. No luck.I've tried to optimize for the value -24 and for UNKNOWN. Also no luck.I've tried doing this with temp tables and table variables instead of CTEs. Neither of those made any difference.Looking at the Execution Plans for the difference between variable and no-variable in the SourceCTE the only difference is the cost of the relative steps. The steps are otherwise identical.So I'm turning to the friendly folks here to see if anyone has an idea.

Viewing all articles
Browse latest Browse all 3145

Trending Articles