When querying the database in order to query more quickly, generally use temporary tables, such as SELECT * into #t from TableA, but if the data is too large, but the temporary may also be very slow, this time can be a temporary table index, such as CREATE INDEX ix_temptable On #T (Field 1, Field 2, Field 3). If the temporary table field is unknown, how to index it, you can use the following SQL to find out all the fields of the temporary tables, to index all the fields
DECLARE @COL VARCHAR (1000)
SET @COL = ' '
SELECT @COL = @COL + column_name + ', ' from
TEMPDB.INFORMATION_SCHEMA.COLUMNS A
INNER JOIN (SELECT [NAME] from TEMPDB.dbo.sysobjects WHERE [ID] = object_id (N ' TEMPDB: #T ')) B
On a.table_name = B.[name] and a.ordinal_position <= 3
ORDER by Ordinal_position
Print @COL
SET @COL = Left (@COL, LEN (@COL)-1)
EXEC (' CREATE INDEX ix_temptable on #T (' + @COL + ') ')
Database query optimization--Indexing temporary tables