SQL Server SELECT INTO and Blocking With Temp Tables

后端 未结 8 844
抹茶落季
抹茶落季 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.

    0 讨论(0)
  • 2020-12-13 19:50

    This will probably float around for a long time, feeding the pockets of various 'consultants'. Like all myths, it has a kernel of truth and a lot of BS.

    The truth: SQL 2000 and previous versions had known contention issues around the allocation of extents in tempdb. The contention was true in fact in all databases, but more visible in tempdb due to some heavy tempdb usage. It is documented in KB328551:

    When the tempdb database is heavily used, SQL Server may experience contention when it tries to allocate pages.

    From the sysprocesses system table output, the waitresource may show up as "2:1:1" (PFS Page) or "2:1:3" (SGAM Page). Depending on the degree of contention, this may also lead to SQL Server appearing unresponsive for short periods.

    These operations heavily use tempdb:
    Repeated create and drop of temporary tables (local or global).
    Table variables that use tempdb for storage purposes.
    Work tables associated with CURSORS.
    Work tables associated with an ORDER BY clause.
    Work tables associated with an GROUP BY clause.
    Work files associated with HASH PLANS.

    Heavy and significant use of these activities may lead to the contention problems.

    A trace flag -T1118 was added in SQL Server 2000 SP3 that was forcing SQL to use a round-robin algorithm for mixed pages allocations. This new algorithm, when correlated with the practice of deploying tempdb on top of a set of equal size files, one for each CPU, would alleviate the contention. The trace flag is still present in SQL 2005/2008, although its far less likely to be needed.

    Everything else about this myth is pretty much BS.

    • does use of #temp tables cause blocking? No. At worst it increases contention under load in SQL 2000 and earlier, but that is a far cry from saying that it blocks anything. You would have to measure first and see that this is the case, and if so deploy the remediation measures (allocate one tempdb file per CPU, make them equal size, turn on -T1118)..
    • Does select ... into #temp block something for the duration of the select? Not really.
    • Does select ... into #temp block something for the duration of the stored procedure containing the select? Hell no. Just reading that claim and I burst into laugh.

    For more details, there is this article: Misconceptions around TF1118.

    0 讨论(0)
  • 2020-12-13 19:58

    Why not do the following?

    SELECT X, Y, Z
    INTO #MyTable
    FROM YourTable
    WHERE 1 = 2
    

    The statement would run instantly - creating your temp table and avoiding any possible locking. Then you could insert into it as usual:

    INSERT #MyTable
    SELECT X, Y, Z
    FROM YourTable
    
    0 讨论(0)
  • 2020-12-13 19:59

    While SELECT INTO has been fixed from blocking the tempdb i would use caution when writing such code as under testing some system tables do get blocked.

    Reference: http://www.sqlservercentral.com/Forums/Topic1642797-2799-1.aspx

    0 讨论(0)
  • 2020-12-13 20:03

    You can get blocking if you create #temp tables inside a transaction. While this is generally not recommended I have seen this done a lot.

    However the blocking this causes is on some system tables in tempdb that do not affect other connections from creating temp tables (except perhaps for SQL versions prior to 2000?). It does mean that running sp_spacesused on tempdb will block unless you set transaction isolation level to read uncommitted. Also viewing properties on tempdb from SSMS will fail and timeout no doubt because it is using read committed transaction isolation level.

    0 讨论(0)
  • 2020-12-13 20:04

    I would say lack of locking proof means no locking, which is your proof. Why would the method that the temp table is created in (CREATE or SELECT ... INTO) make a difference in locking TempDB?

    0 讨论(0)
提交回复
热议问题