Sql-server – reuse of temp table getting more time to execute

sql-server-2008temporary-tables

i'm creating temp table as,

SELECT 
    T.CD,
    T.OBJECT_CD,
    T.PRICE 
    INTO #TEMP FROM TABLE_NAME T

EDIT : ADDED CLUSTERED INDEX TO TEMP TABLE

CREATE CLUSTERED INDEX IX_Temp_OBJECT_CD
    ON #Temp (OBJECT_CD);

and using this table #TEMP two times in another query to get results as,

SELECT MAX(T1.PRICE),
        MIN(T1.PRICE),
        MAX(T2.PRICE),
        MIN(T2.PRICE)
        FROM PP INNER JOIN #TEMP T1 ON T1.OBJECT_CD = PP.OBJECT_CD
        INNER JOIN #TEMP T2 ON T2.CD = PP.CD AND T2.OBJECT_CD = PP.OBJECT_CD

but the query is taking more time to execute. when i remove any one of T1 or T2 from JOIN the query retrieves result instantly. what could be the reason?

Best Answer

I would add a second column to the clustered index statement, make it be

CREATE CLUSTERED INDEX IX_Temp_OBJECT_CD ON #Temp (OBJECT_CD, CD)

That may help, depending on how many CD's there are to OBJECT_CD's.