Inserting data to Global Temporary table taking longer time

Padmanabhan, Venkatesh 246 Reputation points
2021-05-20T11:38:51.94+00:00

Hi, I have a very large table - Table1

I am trying to have a temporary Global table , which contains the subset of the large table. But the insertion of data from Table1 to Global table is taking a longer time. I do not have access to run the sql execution plan / create index. The Table1 does not contain a IDENTITY column, but has another column as Primary key.

Below is the code which I am trying:

create table session.##change_criteria(CHANGE_DATE_FROM DATE not null, CHANGE_DATE_TO DATE not null)

 insert into session.##change_criteria values ('2020-09-21','2020-09-28')

 create TABLE session.##new_changes ( Test_ID integer not null,  TestName char(15) not null )

 insert into session.##new_changes select Test_ID, TestName from TABLE1 with (nolock)
where CAST(CREATION as date) >= (select CHANGE_DATE_FROM from session.##change_criteria) and
  CAST(CREATION as date) <= (select CHANGE_DATE_TO from session.##change_criteria) or 
  CAST(RECONDT as date)  >= (select CHANGE_DATE_FROM from session.##change_criteria) and
 CAST(RECONDT as date) <=  (select CHANGE_DATE_TO from session.##change_criteria)

The last insert is causing performance issues, to insert a weeks data alone , it is taking more than 2.5 hours. The first 3 statements do not take any time, only the last insert is having the issues.

How to fix this?

SQL Server Other
{count} votes

4 answers

Sort by: Most helpful
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2021-05-20T22:18:15.46+00:00

    I don't think that counts as a global temp table. I'm already past my bedtime, so I don't have the time to test. But since you have the table in a schema, I would think that the database is in the same database as the source table. Which I guess is fully logged, so quite some time may be spend on growing log and data files.


  2. CarrinWu-MSFT 6,891 Reputation points
    2021-05-21T07:13:08.883+00:00

    Hi @Padmanabhan, Venkatesh ,

    Welcome to Microsoft Q&A!

    According to this blog, we could know SELECT...INTO was considerably faster than INSERT…INTO.

    And I also made a test as below, we could see SELECT…INTO is faster:
    98498-1.png
    98499-2.png

    Best regards,
    Carrin


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. Viorel 122.5K Reputation points
    2021-05-21T08:01:25.63+00:00

    If CREATION and RECONDT are datetime, then consider this approach too:

    declare @change_date_from as datetime, @change_date_to as datetime
    
    select @change_date_from = CHANGE_DATE_FROM, @change_date_to = dateadd(day, 1, CHANGE_DATE_TO)
    from session.##change_criteria
    

    Then use the next WHERE condition:

    . . .
    WHERE ( CREATION >= @change_date_from and CREATION < @change_date_to ) or
          ( RECONDT >= @change_date_from and RECONDT < @change_date_to )
    

    Show details about these columns and maybe check if indices are required.


  4. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2021-05-21T21:40:20.84+00:00

    I do not have access to run the sql execution plan

    Then you may have to ask some to get the execution plan for you. For most of situations when you have a slow query, it is essential to get the execution plan. Else you are only fumbling around in darkness.

    Then again, this is a fairly simple query, and unless those dates select a small portion of the table, a table scan is the only reasonable option. So it can be worth looking at other bottlenecks.

    Open a new query window and run the script. When it has completed run this query:

    SELECT * FROM sys.dm_exec_session_wait_stats 
    WHERE session_id = @@spid ORDER BY wait_time_ms DESC
    

    What are your top waits?

    Note: this query will not run on SQL 2014 or earlier.

    Also, please share the output of "SELECT @@version".

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.