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;








5















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)










share|improve this question







New contributor



outjet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.














  • 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

















5















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)










share|improve this question







New contributor



outjet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.














  • 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













5












5








5








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)










share|improve this question







New contributor



outjet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question







New contributor



outjet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question







New contributor



outjet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question






New contributor



outjet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 10 hours ago









outjetoutjet

1262 bronze badges




1262 bronze badges




New contributor



outjet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




outjet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









  • 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





    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










3 Answers
3






active

oldest

votes


















2














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.






share|improve this answer






























    1














    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.






    share|improve this answer























    • 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


















    0














    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.]






    share|improve this answer























    • 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













    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.









    draft saved

    draft discarded


















    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









    2














    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.






    share|improve this answer



























      2














      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.






      share|improve this answer

























        2












        2








        2







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 7 hours ago









        Max VernonMax Vernon

        54.6k13 gold badges119 silver badges245 bronze badges




        54.6k13 gold badges119 silver badges245 bronze badges























            1














            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.






            share|improve this answer























            • 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















            1














            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.






            share|improve this answer























            • 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













            1












            1








            1







            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.






            share|improve this answer













            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            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

















            • 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











            0














            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.]






            share|improve this answer























            • 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















            0














            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.]






            share|improve this answer























            • 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













            0












            0








            0







            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.]






            share|improve this answer













            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.]







            share|improve this answer












            share|improve this answer



            share|improve this answer










            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

















            • 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










            outjet is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

            Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

            Tom Holland Mục lục Đầu đời và giáo dục | Sự nghiệp | Cuộc sống cá nhân | Phim tham gia | Giải thưởng và đề cử | Chú thích | Liên kết ngoài | Trình đơn chuyển hướngProfile“Person Details for Thomas Stanley Holland, "England and Wales Birth Registration Index, 1837-2008" — FamilySearch.org”"Meet Tom Holland... the 16-year-old star of The Impossible""Schoolboy actor Tom Holland finds himself in Oscar contention for role in tsunami drama"“Naomi Watts on the Prince William and Harry's reaction to her film about the late Princess Diana”lưu trữ"Holland and Pflueger Are West End's Two New 'Billy Elliots'""I'm so envious of my son, the movie star! British writer Dominic Holland's spent 20 years trying to crack Hollywood - but he's been beaten to it by a very unlikely rival"“Richard and Margaret Povey of Jersey, Channel Islands, UK: Information about Thomas Stanley Holland”"Tom Holland to play Billy Elliot""New Billy Elliot leaving the garage"Billy Elliot the Musical - Tom Holland - Billy"A Tale of four Billys: Tom Holland""The Feel Good Factor""Thames Christian College schoolboys join Myleene Klass for The Feelgood Factor""Government launches £600,000 arts bursaries pilot""BILLY's Chapman, Holland, Gardner & Jackson-Keen Visit Prime Minister""Elton John 'blown away' by Billy Elliot fifth birthday" (video with John's interview and fragments of Holland's performance)"First News interviews Arrietty's Tom Holland"“33rd Critics' Circle Film Awards winners”“National Board of Review Current Awards”Bản gốc"Ron Howard Whaling Tale 'In The Heart Of The Sea' Casts Tom Holland"“'Spider-Man' Finds Tom Holland to Star as New Web-Slinger”lưu trữ“Captain America: Civil War (2016)”“Film Review: ‘Captain America: Civil War’”lưu trữ“‘Captain America: Civil War’ review: Choose your own avenger”lưu trữ“The Lost City of Z reviews”“Sony Pictures and Marvel Studios Find Their 'Spider-Man' Star and Director”“‘Mary Magdalene’, ‘Current War’ & ‘Wind River’ Get 2017 Release Dates From Weinstein”“Lionsgate Unleashing Daisy Ridley & Tom Holland Starrer ‘Chaos Walking’ In Cannes”“PTA's 'Master' Leads Chicago Film Critics Nominations, UPDATED: Houston and Indiana Critics Nominations”“Nominaciones Goya 2013 Telecinco Cinema – ENG”“Jameson Empire Film Awards: Martin Freeman wins best actor for performance in The Hobbit”“34th Annual Young Artist Awards”Bản gốc“Teen Choice Awards 2016—Captain America: Civil War Leads Second Wave of Nominations”“BAFTA Film Award Nominations: ‘La La Land’ Leads Race”“Saturn Awards Nominations 2017: 'Rogue One,' 'Walking Dead' Lead”Tom HollandTom HollandTom HollandTom Hollandmedia.gettyimages.comWorldCat Identities300279794no20130442900000 0004 0355 42791085670554170004732cb16706349t(data)XX5557367