SQL Server SELECT INTO and Blocking With Temp Tables

后端 未结 8 870
抹茶落季
抹茶落季 2020-12-13 19:08

So, recently a DBA is trying to tell us that we cannot use the syntax of

SELECT X, Y, Z
INTO #MyTable
FROM YourTable

To create temporary ta

8条回答
  •  粉色の甜心
    2020-12-13 19:49

    That advice has been floating around for a long time:

    Bottlenecks in SQL Server 6.5

    Many people use a SELECT...INTO query to create a temporary table, something like this:

    SELECT * INTO #TempTable FROM SourceTable

    While this works, it creates locks against the tempdb database for the duration of the SELECT statement (quite a while if you are trawling through a lot of data in the source table, and longer still if the SELECT...INTO is at the start of a longer-running explicit transaction) While the lock is in place, no other user can create temporary tables. The actual location of the bottleneck is a lock on tempdb system tables. In later versions of SQL Server, the locking model has changed and the problem is avoided.

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

提交回复
热议问题