SQL SERVER Myth about SELECT INTO and locks on tempdb

3

Lately I've been studying a lot about database and everything. Today, performing a routine check on the processes of the instance of SQL Server verified that a statement that was running in one database was blocking execution in another database. By checking the queries that were running at the time, I saw that there was no direct link between queries, their structures look like these:

Query running on bank A:

SELECT ... INTO #tempA /*consultas de tabelas no banco A apenas*/

Query running on Bank B:

SELECT ... INTO #tempB /*consultas de tabelas no banco B apenas*/

Looking at this, I noticed that the only part in which the queries are similar is in the creation of the temporary table next to select in SELECT ... INTO #tmp I decided to search on this, I found some articles, however, all me left me doubting the verity of my conclusion.

SQL Server SELECT INTO and Blocking With Temp Tables

a>

SQL Server lock issues when using DDL (including SELECT INTO) clause in long running transactions

In these two articles that I took as an example, you can see totally conflicting comments and comments like:

Originals

  

Does this mean that you have to select a file in a file? Hell no. Just reading that claim and I burst into laugh.

     

Does select ... into #temp block something for the duration of the select? Not really.

     

Fortunately, it was only a problem for SQL 6.5. It was fixed in 7.0 and later.

Translations

  

Does select ... into #temp block anything by the length of the stored procedure that contains the select? No way. Just reading this statement and I started laughing.

     

Does select ... into #temp block anything by the duration of select? Not really.

     

Fortunately, it was just a problem for SQL 6.5. It was fixed in 7.0 and later.

** Quotes have been taken from multiple users.

While on the second link, your conclusions say the following:

Original

  

DDL (including SELECT INTO) statements containing long-running   explicit transactions, should be avoided if possible because it   creates exclusive locks on row (s) in system tables for the duration of   the transaction.

     

This might not cause detrimental effects, but often it misleads DBAs   to think there was a network connectivity or SQL Server resource   issue. If the code is not well written, you may encounter lock request   timeouts when performing typical administrative tasks such as checking   database properties from SSMS. In actuality, you are just experiencing   typical locks and blocks on row (s) in system tables.

Translation

  

DDL statements (including SELECT INTO) containing explicit transactions   should be avoided, if possible, because it creates   unique locks on the line (s) in the system tables by the   duration of the transaction.

     

This may not cause harmful effects, but often misleads   DBAs to think that there was a network connectivity problem or   SQL Server resource. If the code is not well written, you   you may encounter lock request timeouts when you run   Typical administrative tasks, such as checking bank properties   of SSMS data. In reality, you are experiencing blockages and   typical blocks in rows in the system tables.

Sorry for extending the question, but would like to know which one is correct? Really DDL's as SELECT ... INTO #temp cause locks on tempdb? Which of the links is more correct? Is there any place where I can research more about it? Is there any way to test the arguments of those involved in the above links?

Thanks in advance.

Q: I'm using SQL Server 12.0, better known as SQL Server 2014.

    
asked by anonymous 09.04.2018 / 19:33

1 answer

2

TEMPDB It is a database of the system and every time you use temporary tables (#MyTempTableExample) they are created in this database (not with this name however because multiple sessions may be using this same name, if you look you will see that the real name is something very strange). Note that tempdb is used all the time by the engine, especially for operations that process a lot of data where tempdb is used as a form of auxiliary "memory."

Here's a quick overview of some issues related to your issue.

1) Very common a block of physical resources, memory and especially IO. Note that when giving an insert of millions of records you will be massively using disk access (not only for the data but for the indexes and other structures and tb for the log). Then there might be a hardware bottleneck there.

2) The second link that you posted shows an article about a problem tested in SQL Server 2016 (the first link speaks of a similar problem but that occurred in a very old version that was corrected many years ago). In this link it is demonstrated that when creating and popular your temporary table some system tables are blocked, especially [sys].[sysrscols] . Personally I have not tested but it makes sense because when you create objects (tables, columns, etc) these objects are cataloged in these tables. Note that it is interesting to do this test on your system to evaluate if this occurs.

3) Still other considerations about tempdb especially when you use snapshot isolation levels that use tempdb to save versions of your data.

4) Finally the use of temporary tables can often be replaced by table variables. Note however that depends on case by case. As variables (usually) stay in memory they tend to be more performative when they are small. By experience, temporary tables are better when you are populating them with a lot of data (this is due to the amount of memory allocated to the table variables that when burst starts to use tempdb but with an overhead that greatly worsens the performance of those tables variables.).

Conclusion

Temporary tables are an important day-to-day tool but as all SQL-SERVER tools are subject to problems in some specific scenarios and should be used with care.

Then to determine if your SQL INTO in these temporary tables is having a negative impact on performance you will have to check if the problem is really this, a good starting point is this second link that you posted.

    
10.04.2018 / 16:47