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

Index creation after of before populate? Expand / Collapse
Author
Message
Posted Monday, September 13, 2010 8:54 AM
SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Wednesday, March 26, 2014 4:29 AM
Points: 129, Visits: 510
Hello all,
What do you think is better, create indexes for temp tables after or before we populate them,
I tend to believe that create them before the inserts could be better, since we won't be blocking temp, but i have heard some other opinions
What do you think?

Thanks
Post #984857
Posted Monday, September 13, 2010 9:01 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 4:56 AM
Points: 268, Visits: 877
My preference, if this is possible to do in your scenario, is:

1. Create table and unique clustered index
2. Insert data IN ORDER OF CLUSTERED INDEX
3. Create non clustered indexes

This way, you avoid fragmentation and unecessary sorting,

If you have to sort the data to achieve step 2, it doesn't matter so much which way around you do it.


.
Post #984858
Posted Monday, September 13, 2010 9:14 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 3:57 PM
Points: 6,544, Visits: 8,758
If you are inserting a large amount of data, you will probably find it faster to add the index afterwards.

Wayne
Microsoft Certified Master: SQL Server 2008
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
Post #984868
Posted Monday, September 13, 2010 9:17 AM


SSC-Dedicated

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

Group: Administrators
Last Login: Yesterday @ 4:56 PM
Points: 32,771, Visits: 14,935
Ultimately you need to test and get an idea of when it's quicker which way. It often does depend on how much data, and your disk I/O.






Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #984870
Posted Monday, September 13, 2010 9:31 AM
SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Wednesday, March 26, 2014 4:29 AM
Points: 129, Visits: 510
Actually my concern is that we have several dbs and ost of users, so I really need to be careful with the use of temp db, I like the approach of
Create table
create clustered index
Inserts
nonclustered
Post #984881
Posted Monday, September 13, 2010 9:36 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 12:10 AM
Points: 6,997, Visits: 8,410
ricardo_chicas (9/13/2010)
Actually my concern is that we have several dbs and ost of users, so I really need to be careful with the use of temp db, I like the approach of
Create table
create clustered index
Inserts
nonclustered


In general that would be the prefered order if you load a full set. It will get you most optimal indexes with regards to fragmentation.
Keep in mind loading in order of the clustering index will have advantages too with regards to space usage and IO.

However, tempdb is always a special case, so profound testing may be in order !


Johan


Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


"press F1 for solution", "press shift+F1 for urgent solution"


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me but most of the time this is me
Post #984887
Posted Monday, September 13, 2010 10:00 PM
SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Friday, March 07, 2014 12:08 AM
Points: 138, Visits: 919
Which approach if we populate large data into a table many times?
Eg: Today I populate 2GB data follow below steps
Create Clustered Index on table
Insert data
Create Non-Clusterd Index(s)
Tomorrow, I will also populate data into the table.
Thanks,
Post #985212
Posted Tuesday, September 14, 2010 8:10 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 4:56 AM
Points: 268, Visits: 877
The same principle 'probably' applies

i.e. Create table and clustered index, populate IN CLUSTERED INDEX ORDER, create additional indexes

But...

(1) With this much data you may well be sorting it first unless you are getting it straight out of another indexed tables (e.g. aggregation). If you have a sort step you'll need to test whether sorting is more intensive than creating the clustered index afterwards. It may well be.

(2) With this much data there may be a better way of doing this - e.g. only populating changes - you'd have to write logic for this

(3) With more complex requirements, as previous posters have said, you should test different techniques and see what works best - There are lots of potential differences vetween environments.

There is no 'best' way of doing things which always applies!



.
Post #985531
Posted Tuesday, September 14, 2010 8:15 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 12:10 AM
Points: 6,997, Visits: 8,410
Any partitioning involved ... search BOL for "sliding windows"

Johan


Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


"press F1 for solution", "press shift+F1 for urgent solution"


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me but most of the time this is me
Post #985536
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse