improve performance inserting into temp table

Ismail Cassiem 61 Reputation points
2022-04-06T20:50:57.973+00:00

Hi,

  1. My execution plan show an high cost on a temp table insert to all the columns within the temp table
    any ideas how i can improve the performance on this

DROP TABLE IF EXISTS #Working_usp_1
SELECT
col1,....col25
INTO #Working_usp_1
FROM

Results_usp_1

  1. The sp calls other sp that creates temp tables that my session cant access to create a runtime index on the temp table
    the below executes but still appears in my execution plan
    DECLARE @Index1 AS nvarchar(max)
    SET @Index1 = 'CREATE NONCLUSTERED INDEX IDX_Index6 on [dbo].[' + (SELECT TOP 1 name FROM tempdb.sys.tables WHERE [name] like '#Table1_%') + '] (col1]) INCLUDE (col2)'
    EXEC sp_executesql @Index1

Please Help

regards

Developer technologies Transact-SQL
0 comments No comments
{count} votes

Accepted answer
  1. Ronen Ariely 15,206 Reputation points
    2022-04-06T22:07:51.047+00:00

    Hi XXXX,

    Off-topic: I think you could spend some minutes to came up with a better nickname than a number

    My execution plan show

    You speak about the Execution Plan but you did not provided it. Please provide the Execution Plan (the full EP which mean the XML and not a screenshot), so we will have something to discuss about

    high cost on a temp table insert to all the columns within the temp table

    We cannot change the fact that writing to the disk cost. You can take some general actions to improve performance of INSERT like

    (1) using fast SSD

    (2) test the option to create the table fist and use INSERT INTO instead of SELECT INTO

    (3) remember to drop temp tables as quickly: as you can

    (4) Do NOT create indexes

    Indexes meant to help in finding the rows and cannot help in INSERT

    (5) tempdb serves all the databases and might be busy. Test the option of using regular table in the database if this fit your system

    (6) Check the option of using a memory-optimized table variable

    https://learn.microsoft.com/en-us/sql/relational-databases/in-memory-oltp/faster-temp-table-and-table-variable-by-using-memory-optimization?view=sql-server-ver15

    The sp calls other sp that creates temp tables instead

    Are you sure that the issue in the INSERT and not the select for example? Well the Execution Plan might help in this. Try to simplifies the procedure

    CREATE NONCLUSTERED INDEX I

    Obviously create index not help you in insert data.

    Try to provide more information and the Execution Plan


9 additional answers

Sort by: Most helpful
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2022-04-06T21:38:53.17+00:00

    I don't see the relation between your SELECT INTO in the first part of your post and the index creation in your second part.

    If you have the data in one temp table, why copy it to another?

    Keep in mind that the cost percentages you see in the execution plans are only estimates, and your real bottleneck may be elsewhere.

    0 comments No comments

  2. Ismail Cassiem 61 Reputation points
    2022-04-07T14:17:08.833+00:00

    Good day,
    Thank You all for assisting.

    This SP has like 2000 rows and i cant share the executionplan (125 plans) , finance company
    the index name was only to hide the real name

    I cant create a physical table , basically limited access on source & db to create.

    I am having a look at the insert into but the table has 20cols to define and there's about 10 temp tables

    Any other ideas please?


  3. Ismail Cassiem 61 Reputation points
    2022-04-07T15:13:58.203+00:00

    QRY runs for 30 day range, 10min :(

    i am tasked to bring it down to 3min without changing the DB, trying to upload the execution plans that has high cost

    0 comments No comments

  4. Ismail Cassiem 61 Reputation points
    2022-04-07T15:23:27.993+00:00

    ![191061-image.png]1


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.