Sql-server – When to add indexes to #temp tables

indexsql-server-2008ssms

When testing stored procedures in SSMS, it sometimes says there is a missing index on
#someTempTable____________________000000000000005B] (someField) etc etc

When I do add them like this to the sp:

CREATE NONCLUSTERED INDEX [<Name of Missing Index, sysname,>]
ON [dbo].[#someTable] ([someField])
GO

I never seem to see a speed improvement. So, I often do not add such indexes. When should I be adding such indexes?

Best Answer

Add an index if you'll use your temp table and its index twice or more during the query run.

Or to maintain usual index tasks, like uniqueness

If your data loaded into temp table are already sorted, the to create temp table with the same clustered index as sort of data

BUT

taking into account sql server's feature of temp tables reuse- if you decide to create an index on temp table - try to do it in CREATE TABLE statement. If you'll add an index explicitly after table creation - it will prevent sql server to reuse that table next time