July 13, 2017 at 5:31 pm
I was asked to help a developer and was not given much information, so i hope you can help out
Using SQL Server 2014
The developer re wrote a stored procedure to use #temp tables instead of @variable to store some temp data. He said there is no updating of any regular tables in the database. For some reason he is getting deadlocks now when users are trying to use the application. He says its only when two or more people execute the same thing at the exact same time.
When using the @variable this problem did not exist and he decided to use the #temp table for better performance. Any help would be appreciated.
I looked at the stored procedure and found that he is using a Cursor with a join select statement to fill the temp table which I think is weird because he could probably use a set result query - select ...into #temp and be better off.
I would like to recommend something, so any feedback is appreciated.
Jeff
July 13, 2017 at 6:07 pm
I recommend using temp table over table variable when you are dealing with larger data sets for performance reasons. There are some advantage you have with temp table over table variable. Recommend not using cursor unless its absolutely necessary have a row by row processing. Also whats the query timing? any indexes to tune the query?
July 13, 2017 at 7:20 pm
Thanks for the reply.
Jeff
July 13, 2017 at 7:28 pm
It's hard to say much from the little detail posted, honestly.
Any chance we can the deadlock graph or code?
Cheers!
July 14, 2017 at 1:44 am
Post the deadlock graph please.
The deadlock can't be on the temp table (baring a bug in SQL), as those are per-session, each connection has its own copy of the temp table.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 14, 2017 at 6:10 am
Thanks again for the help. i will work with the developer and see if there is more information.
Jeff
July 14, 2017 at 9:26 am
If he is actually doing a SELECT ... INTO #temp, that could cause blocking, perhaps long blocking, but it normally wouldn't cause true "deadlocks". Is it an actual deadlock or just (very) long blocking?
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
July 14, 2017 at 10:39 am
I spoke to the developer and he want to deal with by adding the WITH NOLOCK in several places in the script and will be reverting back to the @variable tables.
As for the Cursor he understood my recommendation to use a set query instead, but said the SP was using that before and for some reason it was very slow, so converted it to a cursor.
Thanks for the help
Jeff
July 14, 2017 at 11:43 am
Table variables are almost always much slower than temp tables. I wouldn't use table variables unless I knew it was only 1 or 2 rows, ever, period, or I had to have the data past a ROLLBACK (such as error info, etc.).
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
July 14, 2017 at 12:47 pm
jayoub - Friday, July 14, 2017 10:39 AMI spoke to the developer and he want to deal with by adding the WITH NOLOCK in several places in the script and will be reverting back to the @variable tables.As for the Cursor he understood my recommendation to use a set query instead, but said the SP was using that before and for some reason it was very slow, so converted it to a cursor.
Thanks for the help
Heh... the sp was very slow so you folks converted it to a cursor, which will likely make things slower. Temp db is "deadlocking" and you don't know why. Now you're using WITH NOLOCK and are reverting back to Table Variables the latter of which is likely a part of the original cause of slowness. You folks need to slow down and stop jabbing at stuff.
Let's start at the beginning. What part of the stored procedure was slow and what was causing that part to be slow? I'm pretty sure that you good folks have no clue about the second part so stop writing code to try to fix it until you actually know what's wrong and how to fix it. 😉
Please see the second link under "Helpful Links" in my signature line below.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 14, 2017 at 3:07 pm
jayoub - Friday, July 14, 2017 10:39 AMI spoke to the developer and he want to deal with by adding the WITH NOLOCK in several places in the script and will be reverting back to the @variable tables.
So he wants to replace deadlocks with incorrect results and slow queries instead of fixing it. Sounds like a great plan!
for some reason it was very slow, so converted it to a cursor.
"The query is slow. Hmm, what to do? I know, use a slower and less efficient method!"
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply