SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Temporary Indexes


Temporary Indexes

Author
Message
Steve Jones
Steve Jones
SSC Guru
SSC Guru (141K reputation)SSC Guru (141K reputation)SSC Guru (141K reputation)SSC Guru (141K reputation)SSC Guru (141K reputation)SSC Guru (141K reputation)SSC Guru (141K reputation)SSC Guru (141K reputation)

Group: Administrators
Points: 141320 Visits: 19416
Comments posted to this topic are about the item Temporary Indexes

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
graeme.vincent
graeme.vincent
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 69
I use them all the time on temporary tables. Where you have temp tables with lots of data, or they are joining onto normal tables with lots of data, adding an index can have a massive positive impact on performance in joins. The improvement far outweighs any overhead caused by SQL Server creating and updating the index.
mar10br0
mar10br0
SSC Veteran
SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)SSC Veteran (216 reputation)

Group: General Forum Members
Points: 216 Visits: 351
Most definitely.
Most common situation is a version-upgrade of the application where the schema is changing and data needs to be migrated (especially when a normalization-change is involved).
The standard pattern for this is to add one or more additional (physical or computed) columns to the old tables that provide some new key used to obtain info handy for the migration. Create an index on those new columns if they are going to be used to filter the old data for migration purposes. Then build the new tables from the old (here the indexes could seriously speed up the migration process).
BTW, all this could be part of the multi-step release patter described in Solomon's article "Restructure 100 Million Row (or more) Tables in Seconds. SRSLY!" (http://www.sqlservercentral.com/articles/Data+Modeling/72814/). Where those additional columns/indexes are created in the pre-release (as a separate new table).

Another case is to investigate some bug and I want to analyse the data in ways never foreseen in original development. Some additional indexes can help playing with the data-query iteratively in a timely fashion (having to wait 10s of seconds for a result can be very annoying when you want to play with the query after seeing the results). I normally do this in a database copied to my local machine, since this sort of querying could seriously hamper the production tables (being locked for long periods).

Marco
ttudor
ttudor
SSC Veteran
SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)

Group: General Forum Members
Points: 211 Visits: 62
I do use temporary indexes when I'm crunching large datasets. This is usually the case for our month-end reports. In one report it cut our execution time from over 8 minutes to under 2. Even when I use table variables I've found performance gains by declaring a Primary Key if I can.
Victor Kirkpatrick
Victor Kirkpatrick
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2333 Visits: 474
Marco +1. I'll also use them in a version upgrade, where the schema is changing and I am converting tables to the new schema, I may need to store an old key in a table in order to use it to also convert the data in detail tables below the current one. In that case I'll create a temp (working) index on that old key, then drop the index and column at the end of the patch. Performance is vastly improved in the conversion.
blandry
blandry
SSC Eights!
SSC Eights! (961 reputation)SSC Eights! (961 reputation)SSC Eights! (961 reputation)SSC Eights! (961 reputation)SSC Eights! (961 reputation)SSC Eights! (961 reputation)SSC Eights! (961 reputation)SSC Eights! (961 reputation)

Group: General Forum Members
Points: 961 Visits: 723
I would guess that about 50-75% of our SQL use is to fill Datasets on the .NET side of things. We once had a talented DBA who suggested we use temporary indexes on a number of our SQL queries used to fill these Datasets in an effort in increase performance. We gave it a try...

What we found was that on queries that fill large Datasets, we did see some improvement, but on smaller (or some medium) fills, the temporary indexes were actually slowing performance. We were able to increase performance there by making a number of these indexes permanent so we avoided the create-on-the-fly element involved.

What is interesting about all this is that in "our world" we have to do a bit of experimenting each time we get into these situations. For example, sometimes its better to take just a plain old 'give me the data' Dataset fill, and then play with code on the .NET side to get whatever result we might be after - where other times (especially in reporting) we find its better to work on the SQL side and tweak around with temporary indexes and in some cases temporary tables/cursors to maximize performance.

I think temporary indexes have their place, but like most 'get the data' situations in software development, they are not the be-all-end-all answer to performance. They can be in individual cases, but it really depends on what the end goal is, and that takes some experimenting to determine.

There's no such thing as dumb questions, only poorly thought-out answers...
chrisfradenburg
chrisfradenburg
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2902 Visits: 2076
blandry (4/29/2011)
What we found was that on queries that fill large Datasets, we did see some improvement, but on smaller (or some medium) fills, the temporary indexes were actually slowing performance. We were able to increase performance there by making a number of these indexes permanent so we avoided the create-on-the-fly element involved.


I was actually wondering how many of these would work better as permanent indexes. The last place I worked used them for upgrades but that was it. We did index temporary tables but in those cases the indexes were permanent in relation to the lifespan of the table so I wouldn't consider those temporary indexes.
Eric M Russell
Eric M Russell
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28068 Visits: 11489
SQL Server routinely creates temporary "index" structures in memory or tempdb all the time, like during a hash join on large tables.
Staging tables and temporary indexes are common in scenarios where a moderate amount of reporting is performed directly out of the OLTP database rather than from a snapshot or external data mart. This can speed up the reporting process, but of course it will also result in a lot of transaction logging.
When I stage data for something like month end reporting purposes it's in summary form, and I generally retain it for archival purposes in case the reports need to be re-run later or leveraged for some other BI process. In that case the indexes are on the staging tables, so there is not so much reason to drop them afterward.


"The universe is complicated and for the most part beyond your control, but your life is only as complicated as you choose it to be."
Nadrek
Nadrek
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4334 Visits: 2741
Quite common: indexes on #temp tables.

Uncommon, but not unheard of: temporary indexes on permanent tables, always as a result of benchmarking with our actual production dataset such that the aggregate (Profiler SQL:BatchCompleted) stats, particularly Reads and Writes, are significantly better (including all the overhead) with the temporary index than without.

As another user mentioned, these really depend.
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)

Group: General Forum Members
Points: 64357 Visits: 18570
Temporary indexes is something that we use on a regular basis for reporting needs, etl needs and those once in a blue moon processes.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search