Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Temporary Indexes Expand / Collapse
Author
Message
Posted Thursday, April 28, 2011 9:31 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Yesterday @ 3:52 PM
Points: 33,268, Visits: 15,440
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
Post #1100701
Posted Thursday, April 28, 2011 9:33 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, March 14, 2014 9:20 PM
Points: 13, Visits: 66
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.
Post #1100702
Posted Friday, April 29, 2011 2:20 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, October 5, 2012 3:30 AM
Points: 138, 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
Post #1100752
Posted Friday, April 29, 2011 6:01 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, June 9, 2014 5:19 AM
Points: 170, Visits: 47
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.
Post #1100818
Posted Friday, April 29, 2011 6:04 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 5:07 AM
Points: 1,795, Visits: 365
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.
Post #1100821
Posted Friday, April 29, 2011 6:05 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, May 7, 2012 9:23 AM
Points: 304, Visits: 716
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...
Post #1100822
Posted Friday, April 29, 2011 6:59 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, August 4, 2014 8:10 AM
Points: 1,635, Visits: 1,972
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.
Post #1100858
Posted Friday, April 29, 2011 7:00 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, September 15, 2014 6:39 PM
Points: 1,657, Visits: 4,740
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.
Post #1100859
Posted Friday, April 29, 2011 9:51 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Monday, September 15, 2014 11:09 AM
Points: 869, Visits: 2,399
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.
Post #1100925
Posted Friday, April 29, 2011 10:10 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 9:32 PM
Points: 21,755, Visits: 15,459
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


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1100936
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse