Using SPID in DB Tables (instead of Table Variable)How to Drop Tables using a variable in SQL Server?SPID Change for a process?Parallelism with temp table but not table variable?Effect on execution plans when a table variable has a primary keyShould I use tempdb or memory optimized table variable?Using table variable instead of temp table makes query execution slowHow to insert into TABLE Variable?Using Table as a Variable for a Stored ProcedurePermission error when using sys.dm_exec_input_buffer with @@SPIDuse a table as a variable
What "fuel more powerful than anything the West (had) in stock" put Laika in orbit aboard Sputnik 2?
Why does FFmpeg choose 10+20+20 ms instead of an even 16 ms for 60 fps GIF images?
Why is Google approaching my VPS machine?
Using SPID in DB Tables (instead of Table Variable)
Why does "git status" show I'm on the master branch and "git branch" does not in a newly created repository?
My credit card has no magnetic stripe. Is this a problem in the USA?
Is it okay for a chapter's POV to shift as it progresses?
Movie where a man was put into a computer before death, wife doesn't trust him anymore
What is the difference between a Hosaka, Ono-Sendai, and a "deck"?
Is the purpose of sheet music to be played along to? Or a guide for learning and reference during playing?
How would you say "Sorry, that was a mistake on my part"?
Can you perfectly wrap a cube with this blocky shape?
Intel 8080-based home computers
Operation Unz̖̬̜̺̬a͇͖̯͔͉l̟̭g͕̝̼͇͓̪͍o̬̝͍̹̻
Why do so many pure math PhD students drop out or leave academia, compared to applied mathematics PhDs?
Is passive Investigation essentially truesight against illusions?
Create Array from list of indices/values
Does this sentence I constructed with my junior high school latin work? I write online advertising and want to come off as snobby as possible
What were the problems on the Apollo 11 lunar module?
At which point can a system be compromised when downloading archived data from an untrusted source?
Optimising the Selection of MaxValue in Association
Sending a photo of my bank account card to the future employer
Increasing muscle power without increasing volume
How to honestly answer questions from a girlfriend like "How did you find this place" without giving the impression I'm always talking about my exes?
Using SPID in DB Tables (instead of Table Variable)
How to Drop Tables using a variable in SQL Server?SPID Change for a process?Parallelism with temp table but not table variable?Effect on execution plans when a table variable has a primary keyShould I use tempdb or memory optimized table variable?Using table variable instead of temp table makes query execution slowHow to insert into TABLE Variable?Using Table as a Variable for a Stored ProcedurePermission error when using sys.dm_exec_input_buffer with @@SPIDuse a table as a variable
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Transactional database used for booking things...
Our vendor was asked to replace #temptables with @tablevariables (because of heavy compile locks) but instead they replaced with an actual table that adds SPID as a column to ensure the stored procedure only acts on the applicable rows.
Do you see any risk in this method of operation? Before all transactions were isolated within their own transaction... I worried we may end up locking this table a bunch but their opinion is that SQL uses row-level locking and this won't create more locks.
CREATE TABLE dbo.qryTransactions (
ID int IDENTITY (0,1) NOT NULL CONSTRAINT pk_qryTransactions PRIMARY KEY CLUSTERED,
spid int NOT NULL,
OrderID int,
ItemID int,
TimeTransactionStart datetime,
TimeTransactionEnd datetime,
...other fields
)
CREATE INDEX idx_qryTransactions_spidID ON qryTransactions (spid, ID) INCLUDE (ItemID, OrderID, TimeTransactionStart, TimeTransactionEnd)
sql-server stored-procedures table-variable
New contributor
add a comment |
Transactional database used for booking things...
Our vendor was asked to replace #temptables with @tablevariables (because of heavy compile locks) but instead they replaced with an actual table that adds SPID as a column to ensure the stored procedure only acts on the applicable rows.
Do you see any risk in this method of operation? Before all transactions were isolated within their own transaction... I worried we may end up locking this table a bunch but their opinion is that SQL uses row-level locking and this won't create more locks.
CREATE TABLE dbo.qryTransactions (
ID int IDENTITY (0,1) NOT NULL CONSTRAINT pk_qryTransactions PRIMARY KEY CLUSTERED,
spid int NOT NULL,
OrderID int,
ItemID int,
TimeTransactionStart datetime,
TimeTransactionEnd datetime,
...other fields
)
CREATE INDEX idx_qryTransactions_spidID ON qryTransactions (spid, ID) INCLUDE (ItemID, OrderID, TimeTransactionStart, TimeTransactionEnd)
sql-server stored-procedures table-variable
New contributor
1
It depends. How many sessions are concurrently using the table, how active are said sessions, how many records are within the table, do the records in this new table get purged or do they remain, etc.? 5 sessions, no big deal. 500 sessions, well chances are good you're going to run into blocking you'd not see with temporary tables/variables local to each session.
– John Eisbrener
9 hours ago
What version of sql server are you using?
– Anthony Genovese
9 hours ago
2016 Enterprise - 13.0.5216.0
– outjet
8 hours ago
Records per session hitting table will be 1-50... they will be purged, so that table itself probably won't get more than 1,000 rows at once... concurrent sessions likely around 50....
– outjet
8 hours ago
If you are forced to go down this path (please try to avoid it) I would give serious thought to partitioning on the spid value, making sure that lock escalation on the table is set to AUTO. Then at least the wiping of a specific spid's data could be done in a switch and truncate operation.
– Jonathan Fite
16 mins ago
add a comment |
Transactional database used for booking things...
Our vendor was asked to replace #temptables with @tablevariables (because of heavy compile locks) but instead they replaced with an actual table that adds SPID as a column to ensure the stored procedure only acts on the applicable rows.
Do you see any risk in this method of operation? Before all transactions were isolated within their own transaction... I worried we may end up locking this table a bunch but their opinion is that SQL uses row-level locking and this won't create more locks.
CREATE TABLE dbo.qryTransactions (
ID int IDENTITY (0,1) NOT NULL CONSTRAINT pk_qryTransactions PRIMARY KEY CLUSTERED,
spid int NOT NULL,
OrderID int,
ItemID int,
TimeTransactionStart datetime,
TimeTransactionEnd datetime,
...other fields
)
CREATE INDEX idx_qryTransactions_spidID ON qryTransactions (spid, ID) INCLUDE (ItemID, OrderID, TimeTransactionStart, TimeTransactionEnd)
sql-server stored-procedures table-variable
New contributor
Transactional database used for booking things...
Our vendor was asked to replace #temptables with @tablevariables (because of heavy compile locks) but instead they replaced with an actual table that adds SPID as a column to ensure the stored procedure only acts on the applicable rows.
Do you see any risk in this method of operation? Before all transactions were isolated within their own transaction... I worried we may end up locking this table a bunch but their opinion is that SQL uses row-level locking and this won't create more locks.
CREATE TABLE dbo.qryTransactions (
ID int IDENTITY (0,1) NOT NULL CONSTRAINT pk_qryTransactions PRIMARY KEY CLUSTERED,
spid int NOT NULL,
OrderID int,
ItemID int,
TimeTransactionStart datetime,
TimeTransactionEnd datetime,
...other fields
)
CREATE INDEX idx_qryTransactions_spidID ON qryTransactions (spid, ID) INCLUDE (ItemID, OrderID, TimeTransactionStart, TimeTransactionEnd)
sql-server stored-procedures table-variable
sql-server stored-procedures table-variable
New contributor
New contributor
New contributor
asked 10 hours ago
outjetoutjet
1262 bronze badges
1262 bronze badges
New contributor
New contributor
1
It depends. How many sessions are concurrently using the table, how active are said sessions, how many records are within the table, do the records in this new table get purged or do they remain, etc.? 5 sessions, no big deal. 500 sessions, well chances are good you're going to run into blocking you'd not see with temporary tables/variables local to each session.
– John Eisbrener
9 hours ago
What version of sql server are you using?
– Anthony Genovese
9 hours ago
2016 Enterprise - 13.0.5216.0
– outjet
8 hours ago
Records per session hitting table will be 1-50... they will be purged, so that table itself probably won't get more than 1,000 rows at once... concurrent sessions likely around 50....
– outjet
8 hours ago
If you are forced to go down this path (please try to avoid it) I would give serious thought to partitioning on the spid value, making sure that lock escalation on the table is set to AUTO. Then at least the wiping of a specific spid's data could be done in a switch and truncate operation.
– Jonathan Fite
16 mins ago
add a comment |
1
It depends. How many sessions are concurrently using the table, how active are said sessions, how many records are within the table, do the records in this new table get purged or do they remain, etc.? 5 sessions, no big deal. 500 sessions, well chances are good you're going to run into blocking you'd not see with temporary tables/variables local to each session.
– John Eisbrener
9 hours ago
What version of sql server are you using?
– Anthony Genovese
9 hours ago
2016 Enterprise - 13.0.5216.0
– outjet
8 hours ago
Records per session hitting table will be 1-50... they will be purged, so that table itself probably won't get more than 1,000 rows at once... concurrent sessions likely around 50....
– outjet
8 hours ago
If you are forced to go down this path (please try to avoid it) I would give serious thought to partitioning on the spid value, making sure that lock escalation on the table is set to AUTO. Then at least the wiping of a specific spid's data could be done in a switch and truncate operation.
– Jonathan Fite
16 mins ago
1
1
It depends. How many sessions are concurrently using the table, how active are said sessions, how many records are within the table, do the records in this new table get purged or do they remain, etc.? 5 sessions, no big deal. 500 sessions, well chances are good you're going to run into blocking you'd not see with temporary tables/variables local to each session.
– John Eisbrener
9 hours ago
It depends. How many sessions are concurrently using the table, how active are said sessions, how many records are within the table, do the records in this new table get purged or do they remain, etc.? 5 sessions, no big deal. 500 sessions, well chances are good you're going to run into blocking you'd not see with temporary tables/variables local to each session.
– John Eisbrener
9 hours ago
What version of sql server are you using?
– Anthony Genovese
9 hours ago
What version of sql server are you using?
– Anthony Genovese
9 hours ago
2016 Enterprise - 13.0.5216.0
– outjet
8 hours ago
2016 Enterprise - 13.0.5216.0
– outjet
8 hours ago
Records per session hitting table will be 1-50... they will be purged, so that table itself probably won't get more than 1,000 rows at once... concurrent sessions likely around 50....
– outjet
8 hours ago
Records per session hitting table will be 1-50... they will be purged, so that table itself probably won't get more than 1,000 rows at once... concurrent sessions likely around 50....
– outjet
8 hours ago
If you are forced to go down this path (please try to avoid it) I would give serious thought to partitioning on the spid value, making sure that lock escalation on the table is set to AUTO. Then at least the wiping of a specific spid's data could be done in a switch and truncate operation.
– Jonathan Fite
16 mins ago
If you are forced to go down this path (please try to avoid it) I would give serious thought to partitioning on the spid value, making sure that lock escalation on the table is set to AUTO. Then at least the wiping of a specific spid's data could be done in a switch and truncate operation.
– Jonathan Fite
16 mins ago
add a comment |
3 Answers
3
active
oldest
votes
It seems to me using the @@SPID
like that is asking for trouble.
Session ID's are reused frequently; as soon as a user connection logs out, that session ID is available to be used again, and is likely to be used by the next session attempting to connect.
To make it work at least semi-reliably, you'd need a login trigger that purges prior rows from the table with the same @@SPID
. If you do that, you're likely to see a lot of locking on the table using the @@SPID
column.
SQL Server does indeed use row locking, but it also uses page locking, and table locking. Of course, you might be able to mitigate that via good indexing, but this still looks like an anti-pattern to me.
If the stored procedure is the only method used to access the affected tables, you could investigate using an application lock, via sp_getapplock
to essentially serialize access to the relevant parts. Docs for sp_getapplock are here. Erik Darling has an interesting post about it here.
add a comment |
Yes, I do see risks. It is naive to count on SQL using Row Locking. For example, I'm pretty sure inserts and deletes will use page locks at least. SQL Engine chooses the lock type based on several factors and none of those factors include "their opinion".
Blanket solutions like changing temp tables to table variables are generally bad ideas also.
Table Variables are very useful in some situations but they do have limitations and performance issues. I prefer temp tables in most circumstances. Particularly when the table will hold more than a few dozen rows.
I would require the vendor to explain why the system experienced "heavy compile locks" and how that degraded performance. Remember, anytime you look you will find "heavy locks" of some sort. That does not necessarily mean the locks are a problem.
Max's comments about @@SPID are also important.
Additionally, the transaction model and error processing could be big issues. If your system experiences deadlocks or input data quality issues then the standard error processing can result in the session being terminated without the qryTransactions table being properly reset.
IMO the wrong solution approach to the original problem.
Thanks for the answer - to your points: our traces show heavy LCK waits for a compile lock on stored procedure InsertOrders. There is a parent procedure Common_InsertOrders_1 which declares the temp table #qryTransactions and then nested procedure InsertOrders_2 queries the Temp Table. Also, we learned there is also frequent recompiling because after 6 modifications to an empty temporary table, any stored procedure referencing that temporary table will need to be recompiled
– outjet
5 hours ago
add a comment |
A bit more rambling than can fit in a comment block ... and want to highlight a comment the OP made in response to Ray's answer:
- parent proc (Common_InsertOrders_1) creates a temp table
- a child proc (InsertOrders) queries the temp table
- compilation locks are being seen for the child proc (InsertOrders)
Going off on a slight tangent for a minute ... on what would happen with this scenario is Sybase ASE ...
- each temp table gets a unique object id (sure, the object id could be re-used at some point but this is rare, and certainly won't happen for concurrent sessions)
- Sybase ASE will normally force a recompile on each execution of the child proc because of the change in object id for the temp table
- Sybase ASE will also force a recompile of the child proc if it sees that the structure of the temp table has changed (eg, different number of columns, different column names/datatypes/nullability) between stored proc invocations
- more recent versions of Sybase ASE have a configuration that (effectively) tells the compiler to ignore changes in temp table object ids thus eliminating the recompilation of the child proc (NOTE: recompilations will still occur if the table structure changes)
Back to the OP's issue (compilation locks on the child proc) ...
- is there a chance that some vestiges of the Sybase ASE behavior could still reside in SQL Server (from when the two products were peas in a pod)?
- are there any SQL Server configurations that could reduce (eliminate?) the recompilations of the child proc (if due to changes in object id)?
- can the OP verify that the parent proc is creating the temp table with the same exact structure/DDL each time?
As for the idea of using a single permanent table with @@SPID to differentiate rows between sessions ... been there, seen that ... yuck; recurring issues:
- how/when to clean up of orphaned rows
- re-use of the @@SPID by the database engine could lead to data accuracy issues if orphaned data exists (or during cleanup of orphaned data, eg, delete where @@SPID=10 but there's a new/current/active session with @@SPID=10 => the cleanup deletes too much data)
- potential for lock escalation from row locks to page/table locks
- if the table has indexes then potential (b)locking when updating the indexes
- depending on which database the table resides in you could be looking at a lot more activity for writing to the log device (in Sybase ASE it's possible to, effectively, disable logging in tempdb)
- even row-level (exclusive) locks can block other sessions (depends on the isolation level and whether or not a session can scan-past/skip said exclusive locks)
I'd want to go back and (re)investigate the root issue (recompilation locks on the child proc) and see if there's a way to reduce (eliminate?) said compilation locks. [Unfortunately my SQL Server knowledge on these issues is ... NULL ... so would be interested in input from some SQL Server compiler experts.]
I agree, I think more time needs to be spent on investigating the compile locks. This has a few points to investigate. support.microsoft.com/en-us/help/263889/…
– Jonathan Fite
19 mins ago
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "182"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
outjet is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f243115%2fusing-spid-in-db-tables-instead-of-table-variable%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
It seems to me using the @@SPID
like that is asking for trouble.
Session ID's are reused frequently; as soon as a user connection logs out, that session ID is available to be used again, and is likely to be used by the next session attempting to connect.
To make it work at least semi-reliably, you'd need a login trigger that purges prior rows from the table with the same @@SPID
. If you do that, you're likely to see a lot of locking on the table using the @@SPID
column.
SQL Server does indeed use row locking, but it also uses page locking, and table locking. Of course, you might be able to mitigate that via good indexing, but this still looks like an anti-pattern to me.
If the stored procedure is the only method used to access the affected tables, you could investigate using an application lock, via sp_getapplock
to essentially serialize access to the relevant parts. Docs for sp_getapplock are here. Erik Darling has an interesting post about it here.
add a comment |
It seems to me using the @@SPID
like that is asking for trouble.
Session ID's are reused frequently; as soon as a user connection logs out, that session ID is available to be used again, and is likely to be used by the next session attempting to connect.
To make it work at least semi-reliably, you'd need a login trigger that purges prior rows from the table with the same @@SPID
. If you do that, you're likely to see a lot of locking on the table using the @@SPID
column.
SQL Server does indeed use row locking, but it also uses page locking, and table locking. Of course, you might be able to mitigate that via good indexing, but this still looks like an anti-pattern to me.
If the stored procedure is the only method used to access the affected tables, you could investigate using an application lock, via sp_getapplock
to essentially serialize access to the relevant parts. Docs for sp_getapplock are here. Erik Darling has an interesting post about it here.
add a comment |
It seems to me using the @@SPID
like that is asking for trouble.
Session ID's are reused frequently; as soon as a user connection logs out, that session ID is available to be used again, and is likely to be used by the next session attempting to connect.
To make it work at least semi-reliably, you'd need a login trigger that purges prior rows from the table with the same @@SPID
. If you do that, you're likely to see a lot of locking on the table using the @@SPID
column.
SQL Server does indeed use row locking, but it also uses page locking, and table locking. Of course, you might be able to mitigate that via good indexing, but this still looks like an anti-pattern to me.
If the stored procedure is the only method used to access the affected tables, you could investigate using an application lock, via sp_getapplock
to essentially serialize access to the relevant parts. Docs for sp_getapplock are here. Erik Darling has an interesting post about it here.
It seems to me using the @@SPID
like that is asking for trouble.
Session ID's are reused frequently; as soon as a user connection logs out, that session ID is available to be used again, and is likely to be used by the next session attempting to connect.
To make it work at least semi-reliably, you'd need a login trigger that purges prior rows from the table with the same @@SPID
. If you do that, you're likely to see a lot of locking on the table using the @@SPID
column.
SQL Server does indeed use row locking, but it also uses page locking, and table locking. Of course, you might be able to mitigate that via good indexing, but this still looks like an anti-pattern to me.
If the stored procedure is the only method used to access the affected tables, you could investigate using an application lock, via sp_getapplock
to essentially serialize access to the relevant parts. Docs for sp_getapplock are here. Erik Darling has an interesting post about it here.
answered 7 hours ago
Max VernonMax Vernon
54.6k13 gold badges119 silver badges245 bronze badges
54.6k13 gold badges119 silver badges245 bronze badges
add a comment |
add a comment |
Yes, I do see risks. It is naive to count on SQL using Row Locking. For example, I'm pretty sure inserts and deletes will use page locks at least. SQL Engine chooses the lock type based on several factors and none of those factors include "their opinion".
Blanket solutions like changing temp tables to table variables are generally bad ideas also.
Table Variables are very useful in some situations but they do have limitations and performance issues. I prefer temp tables in most circumstances. Particularly when the table will hold more than a few dozen rows.
I would require the vendor to explain why the system experienced "heavy compile locks" and how that degraded performance. Remember, anytime you look you will find "heavy locks" of some sort. That does not necessarily mean the locks are a problem.
Max's comments about @@SPID are also important.
Additionally, the transaction model and error processing could be big issues. If your system experiences deadlocks or input data quality issues then the standard error processing can result in the session being terminated without the qryTransactions table being properly reset.
IMO the wrong solution approach to the original problem.
Thanks for the answer - to your points: our traces show heavy LCK waits for a compile lock on stored procedure InsertOrders. There is a parent procedure Common_InsertOrders_1 which declares the temp table #qryTransactions and then nested procedure InsertOrders_2 queries the Temp Table. Also, we learned there is also frequent recompiling because after 6 modifications to an empty temporary table, any stored procedure referencing that temporary table will need to be recompiled
– outjet
5 hours ago
add a comment |
Yes, I do see risks. It is naive to count on SQL using Row Locking. For example, I'm pretty sure inserts and deletes will use page locks at least. SQL Engine chooses the lock type based on several factors and none of those factors include "their opinion".
Blanket solutions like changing temp tables to table variables are generally bad ideas also.
Table Variables are very useful in some situations but they do have limitations and performance issues. I prefer temp tables in most circumstances. Particularly when the table will hold more than a few dozen rows.
I would require the vendor to explain why the system experienced "heavy compile locks" and how that degraded performance. Remember, anytime you look you will find "heavy locks" of some sort. That does not necessarily mean the locks are a problem.
Max's comments about @@SPID are also important.
Additionally, the transaction model and error processing could be big issues. If your system experiences deadlocks or input data quality issues then the standard error processing can result in the session being terminated without the qryTransactions table being properly reset.
IMO the wrong solution approach to the original problem.
Thanks for the answer - to your points: our traces show heavy LCK waits for a compile lock on stored procedure InsertOrders. There is a parent procedure Common_InsertOrders_1 which declares the temp table #qryTransactions and then nested procedure InsertOrders_2 queries the Temp Table. Also, we learned there is also frequent recompiling because after 6 modifications to an empty temporary table, any stored procedure referencing that temporary table will need to be recompiled
– outjet
5 hours ago
add a comment |
Yes, I do see risks. It is naive to count on SQL using Row Locking. For example, I'm pretty sure inserts and deletes will use page locks at least. SQL Engine chooses the lock type based on several factors and none of those factors include "their opinion".
Blanket solutions like changing temp tables to table variables are generally bad ideas also.
Table Variables are very useful in some situations but they do have limitations and performance issues. I prefer temp tables in most circumstances. Particularly when the table will hold more than a few dozen rows.
I would require the vendor to explain why the system experienced "heavy compile locks" and how that degraded performance. Remember, anytime you look you will find "heavy locks" of some sort. That does not necessarily mean the locks are a problem.
Max's comments about @@SPID are also important.
Additionally, the transaction model and error processing could be big issues. If your system experiences deadlocks or input data quality issues then the standard error processing can result in the session being terminated without the qryTransactions table being properly reset.
IMO the wrong solution approach to the original problem.
Yes, I do see risks. It is naive to count on SQL using Row Locking. For example, I'm pretty sure inserts and deletes will use page locks at least. SQL Engine chooses the lock type based on several factors and none of those factors include "their opinion".
Blanket solutions like changing temp tables to table variables are generally bad ideas also.
Table Variables are very useful in some situations but they do have limitations and performance issues. I prefer temp tables in most circumstances. Particularly when the table will hold more than a few dozen rows.
I would require the vendor to explain why the system experienced "heavy compile locks" and how that degraded performance. Remember, anytime you look you will find "heavy locks" of some sort. That does not necessarily mean the locks are a problem.
Max's comments about @@SPID are also important.
Additionally, the transaction model and error processing could be big issues. If your system experiences deadlocks or input data quality issues then the standard error processing can result in the session being terminated without the qryTransactions table being properly reset.
IMO the wrong solution approach to the original problem.
answered 7 hours ago
RayRay
3161 silver badge4 bronze badges
3161 silver badge4 bronze badges
Thanks for the answer - to your points: our traces show heavy LCK waits for a compile lock on stored procedure InsertOrders. There is a parent procedure Common_InsertOrders_1 which declares the temp table #qryTransactions and then nested procedure InsertOrders_2 queries the Temp Table. Also, we learned there is also frequent recompiling because after 6 modifications to an empty temporary table, any stored procedure referencing that temporary table will need to be recompiled
– outjet
5 hours ago
add a comment |
Thanks for the answer - to your points: our traces show heavy LCK waits for a compile lock on stored procedure InsertOrders. There is a parent procedure Common_InsertOrders_1 which declares the temp table #qryTransactions and then nested procedure InsertOrders_2 queries the Temp Table. Also, we learned there is also frequent recompiling because after 6 modifications to an empty temporary table, any stored procedure referencing that temporary table will need to be recompiled
– outjet
5 hours ago
Thanks for the answer - to your points: our traces show heavy LCK waits for a compile lock on stored procedure InsertOrders. There is a parent procedure Common_InsertOrders_1 which declares the temp table #qryTransactions and then nested procedure InsertOrders_2 queries the Temp Table. Also, we learned there is also frequent recompiling because after 6 modifications to an empty temporary table, any stored procedure referencing that temporary table will need to be recompiled
– outjet
5 hours ago
Thanks for the answer - to your points: our traces show heavy LCK waits for a compile lock on stored procedure InsertOrders. There is a parent procedure Common_InsertOrders_1 which declares the temp table #qryTransactions and then nested procedure InsertOrders_2 queries the Temp Table. Also, we learned there is also frequent recompiling because after 6 modifications to an empty temporary table, any stored procedure referencing that temporary table will need to be recompiled
– outjet
5 hours ago
add a comment |
A bit more rambling than can fit in a comment block ... and want to highlight a comment the OP made in response to Ray's answer:
- parent proc (Common_InsertOrders_1) creates a temp table
- a child proc (InsertOrders) queries the temp table
- compilation locks are being seen for the child proc (InsertOrders)
Going off on a slight tangent for a minute ... on what would happen with this scenario is Sybase ASE ...
- each temp table gets a unique object id (sure, the object id could be re-used at some point but this is rare, and certainly won't happen for concurrent sessions)
- Sybase ASE will normally force a recompile on each execution of the child proc because of the change in object id for the temp table
- Sybase ASE will also force a recompile of the child proc if it sees that the structure of the temp table has changed (eg, different number of columns, different column names/datatypes/nullability) between stored proc invocations
- more recent versions of Sybase ASE have a configuration that (effectively) tells the compiler to ignore changes in temp table object ids thus eliminating the recompilation of the child proc (NOTE: recompilations will still occur if the table structure changes)
Back to the OP's issue (compilation locks on the child proc) ...
- is there a chance that some vestiges of the Sybase ASE behavior could still reside in SQL Server (from when the two products were peas in a pod)?
- are there any SQL Server configurations that could reduce (eliminate?) the recompilations of the child proc (if due to changes in object id)?
- can the OP verify that the parent proc is creating the temp table with the same exact structure/DDL each time?
As for the idea of using a single permanent table with @@SPID to differentiate rows between sessions ... been there, seen that ... yuck; recurring issues:
- how/when to clean up of orphaned rows
- re-use of the @@SPID by the database engine could lead to data accuracy issues if orphaned data exists (or during cleanup of orphaned data, eg, delete where @@SPID=10 but there's a new/current/active session with @@SPID=10 => the cleanup deletes too much data)
- potential for lock escalation from row locks to page/table locks
- if the table has indexes then potential (b)locking when updating the indexes
- depending on which database the table resides in you could be looking at a lot more activity for writing to the log device (in Sybase ASE it's possible to, effectively, disable logging in tempdb)
- even row-level (exclusive) locks can block other sessions (depends on the isolation level and whether or not a session can scan-past/skip said exclusive locks)
I'd want to go back and (re)investigate the root issue (recompilation locks on the child proc) and see if there's a way to reduce (eliminate?) said compilation locks. [Unfortunately my SQL Server knowledge on these issues is ... NULL ... so would be interested in input from some SQL Server compiler experts.]
I agree, I think more time needs to be spent on investigating the compile locks. This has a few points to investigate. support.microsoft.com/en-us/help/263889/…
– Jonathan Fite
19 mins ago
add a comment |
A bit more rambling than can fit in a comment block ... and want to highlight a comment the OP made in response to Ray's answer:
- parent proc (Common_InsertOrders_1) creates a temp table
- a child proc (InsertOrders) queries the temp table
- compilation locks are being seen for the child proc (InsertOrders)
Going off on a slight tangent for a minute ... on what would happen with this scenario is Sybase ASE ...
- each temp table gets a unique object id (sure, the object id could be re-used at some point but this is rare, and certainly won't happen for concurrent sessions)
- Sybase ASE will normally force a recompile on each execution of the child proc because of the change in object id for the temp table
- Sybase ASE will also force a recompile of the child proc if it sees that the structure of the temp table has changed (eg, different number of columns, different column names/datatypes/nullability) between stored proc invocations
- more recent versions of Sybase ASE have a configuration that (effectively) tells the compiler to ignore changes in temp table object ids thus eliminating the recompilation of the child proc (NOTE: recompilations will still occur if the table structure changes)
Back to the OP's issue (compilation locks on the child proc) ...
- is there a chance that some vestiges of the Sybase ASE behavior could still reside in SQL Server (from when the two products were peas in a pod)?
- are there any SQL Server configurations that could reduce (eliminate?) the recompilations of the child proc (if due to changes in object id)?
- can the OP verify that the parent proc is creating the temp table with the same exact structure/DDL each time?
As for the idea of using a single permanent table with @@SPID to differentiate rows between sessions ... been there, seen that ... yuck; recurring issues:
- how/when to clean up of orphaned rows
- re-use of the @@SPID by the database engine could lead to data accuracy issues if orphaned data exists (or during cleanup of orphaned data, eg, delete where @@SPID=10 but there's a new/current/active session with @@SPID=10 => the cleanup deletes too much data)
- potential for lock escalation from row locks to page/table locks
- if the table has indexes then potential (b)locking when updating the indexes
- depending on which database the table resides in you could be looking at a lot more activity for writing to the log device (in Sybase ASE it's possible to, effectively, disable logging in tempdb)
- even row-level (exclusive) locks can block other sessions (depends on the isolation level and whether or not a session can scan-past/skip said exclusive locks)
I'd want to go back and (re)investigate the root issue (recompilation locks on the child proc) and see if there's a way to reduce (eliminate?) said compilation locks. [Unfortunately my SQL Server knowledge on these issues is ... NULL ... so would be interested in input from some SQL Server compiler experts.]
I agree, I think more time needs to be spent on investigating the compile locks. This has a few points to investigate. support.microsoft.com/en-us/help/263889/…
– Jonathan Fite
19 mins ago
add a comment |
A bit more rambling than can fit in a comment block ... and want to highlight a comment the OP made in response to Ray's answer:
- parent proc (Common_InsertOrders_1) creates a temp table
- a child proc (InsertOrders) queries the temp table
- compilation locks are being seen for the child proc (InsertOrders)
Going off on a slight tangent for a minute ... on what would happen with this scenario is Sybase ASE ...
- each temp table gets a unique object id (sure, the object id could be re-used at some point but this is rare, and certainly won't happen for concurrent sessions)
- Sybase ASE will normally force a recompile on each execution of the child proc because of the change in object id for the temp table
- Sybase ASE will also force a recompile of the child proc if it sees that the structure of the temp table has changed (eg, different number of columns, different column names/datatypes/nullability) between stored proc invocations
- more recent versions of Sybase ASE have a configuration that (effectively) tells the compiler to ignore changes in temp table object ids thus eliminating the recompilation of the child proc (NOTE: recompilations will still occur if the table structure changes)
Back to the OP's issue (compilation locks on the child proc) ...
- is there a chance that some vestiges of the Sybase ASE behavior could still reside in SQL Server (from when the two products were peas in a pod)?
- are there any SQL Server configurations that could reduce (eliminate?) the recompilations of the child proc (if due to changes in object id)?
- can the OP verify that the parent proc is creating the temp table with the same exact structure/DDL each time?
As for the idea of using a single permanent table with @@SPID to differentiate rows between sessions ... been there, seen that ... yuck; recurring issues:
- how/when to clean up of orphaned rows
- re-use of the @@SPID by the database engine could lead to data accuracy issues if orphaned data exists (or during cleanup of orphaned data, eg, delete where @@SPID=10 but there's a new/current/active session with @@SPID=10 => the cleanup deletes too much data)
- potential for lock escalation from row locks to page/table locks
- if the table has indexes then potential (b)locking when updating the indexes
- depending on which database the table resides in you could be looking at a lot more activity for writing to the log device (in Sybase ASE it's possible to, effectively, disable logging in tempdb)
- even row-level (exclusive) locks can block other sessions (depends on the isolation level and whether or not a session can scan-past/skip said exclusive locks)
I'd want to go back and (re)investigate the root issue (recompilation locks on the child proc) and see if there's a way to reduce (eliminate?) said compilation locks. [Unfortunately my SQL Server knowledge on these issues is ... NULL ... so would be interested in input from some SQL Server compiler experts.]
A bit more rambling than can fit in a comment block ... and want to highlight a comment the OP made in response to Ray's answer:
- parent proc (Common_InsertOrders_1) creates a temp table
- a child proc (InsertOrders) queries the temp table
- compilation locks are being seen for the child proc (InsertOrders)
Going off on a slight tangent for a minute ... on what would happen with this scenario is Sybase ASE ...
- each temp table gets a unique object id (sure, the object id could be re-used at some point but this is rare, and certainly won't happen for concurrent sessions)
- Sybase ASE will normally force a recompile on each execution of the child proc because of the change in object id for the temp table
- Sybase ASE will also force a recompile of the child proc if it sees that the structure of the temp table has changed (eg, different number of columns, different column names/datatypes/nullability) between stored proc invocations
- more recent versions of Sybase ASE have a configuration that (effectively) tells the compiler to ignore changes in temp table object ids thus eliminating the recompilation of the child proc (NOTE: recompilations will still occur if the table structure changes)
Back to the OP's issue (compilation locks on the child proc) ...
- is there a chance that some vestiges of the Sybase ASE behavior could still reside in SQL Server (from when the two products were peas in a pod)?
- are there any SQL Server configurations that could reduce (eliminate?) the recompilations of the child proc (if due to changes in object id)?
- can the OP verify that the parent proc is creating the temp table with the same exact structure/DDL each time?
As for the idea of using a single permanent table with @@SPID to differentiate rows between sessions ... been there, seen that ... yuck; recurring issues:
- how/when to clean up of orphaned rows
- re-use of the @@SPID by the database engine could lead to data accuracy issues if orphaned data exists (or during cleanup of orphaned data, eg, delete where @@SPID=10 but there's a new/current/active session with @@SPID=10 => the cleanup deletes too much data)
- potential for lock escalation from row locks to page/table locks
- if the table has indexes then potential (b)locking when updating the indexes
- depending on which database the table resides in you could be looking at a lot more activity for writing to the log device (in Sybase ASE it's possible to, effectively, disable logging in tempdb)
- even row-level (exclusive) locks can block other sessions (depends on the isolation level and whether or not a session can scan-past/skip said exclusive locks)
I'd want to go back and (re)investigate the root issue (recompilation locks on the child proc) and see if there's a way to reduce (eliminate?) said compilation locks. [Unfortunately my SQL Server knowledge on these issues is ... NULL ... so would be interested in input from some SQL Server compiler experts.]
answered 37 mins ago
markpmarkp
2,1251 gold badge3 silver badges17 bronze badges
2,1251 gold badge3 silver badges17 bronze badges
I agree, I think more time needs to be spent on investigating the compile locks. This has a few points to investigate. support.microsoft.com/en-us/help/263889/…
– Jonathan Fite
19 mins ago
add a comment |
I agree, I think more time needs to be spent on investigating the compile locks. This has a few points to investigate. support.microsoft.com/en-us/help/263889/…
– Jonathan Fite
19 mins ago
I agree, I think more time needs to be spent on investigating the compile locks. This has a few points to investigate. support.microsoft.com/en-us/help/263889/…
– Jonathan Fite
19 mins ago
I agree, I think more time needs to be spent on investigating the compile locks. This has a few points to investigate. support.microsoft.com/en-us/help/263889/…
– Jonathan Fite
19 mins ago
add a comment |
outjet is a new contributor. Be nice, and check out our Code of Conduct.
outjet is a new contributor. Be nice, and check out our Code of Conduct.
outjet is a new contributor. Be nice, and check out our Code of Conduct.
outjet is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f243115%2fusing-spid-in-db-tables-instead-of-table-variable%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
It depends. How many sessions are concurrently using the table, how active are said sessions, how many records are within the table, do the records in this new table get purged or do they remain, etc.? 5 sessions, no big deal. 500 sessions, well chances are good you're going to run into blocking you'd not see with temporary tables/variables local to each session.
– John Eisbrener
9 hours ago
What version of sql server are you using?
– Anthony Genovese
9 hours ago
2016 Enterprise - 13.0.5216.0
– outjet
8 hours ago
Records per session hitting table will be 1-50... they will be purged, so that table itself probably won't get more than 1,000 rows at once... concurrent sessions likely around 50....
– outjet
8 hours ago
If you are forced to go down this path (please try to avoid it) I would give serious thought to partitioning on the spid value, making sure that lock escalation on the table is set to AUTO. Then at least the wiping of a specific spid's data could be done in a switch and truncate operation.
– Jonathan Fite
16 mins ago