patternModerate
When to add indexes to #temp tables?
Viewed 0 times
tablestempindexeswhenadd
Problem
When testing stored procedures in SSMS, it sometimes says there is a missing index on
When I do add them like this to the sp:
I never seem to see a speed improvement. So, I often do not add such indexes. When should I be adding such indexes?
#someTempTable____________________000000000000005B] (someField) etc etcWhen I do add them like this to the sp:
CREATE NONCLUSTERED INDEX []
ON [dbo].[#someTable] ([someField])
GOI never seem to see a speed improvement. So, I often do not add such indexes. When should I be adding such indexes?
Solution
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
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
Context
StackExchange Database Administrators Q#10991, answer score: 12
Revisions (0)
No revisions yet.