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


Index creation - Before OR AFTER Data?


Index creation - Before OR AFTER Data?

Author
Message
Hughesy
Hughesy
SSC Veteran
SSC Veteran (210 reputation)SSC Veteran (210 reputation)SSC Veteran (210 reputation)SSC Veteran (210 reputation)SSC Veteran (210 reputation)SSC Veteran (210 reputation)SSC Veteran (210 reputation)SSC Veteran (210 reputation)

Group: General Forum Members
Points: 210 Visits: 498
Hello,

I have an monthly update mechanism that builds a database structure from scratch and populates it from five restored databases i.e. all DDL is created via multiple scripts for schema creation etc and then pulls in the necessary data from the source databases.

At the moment the constraints have been built as part of the DDL to ensure the data is consistent which will have created indexes in certain situations (primary key).

I am now looking at creating non-clustered indexes on foreign keys. Should I create these indexes during the schema creation or after the populations scripts?

Thanks
Suresh B.
Suresh B.
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2050 Visits: 5326
>>Should I create these indexes during the schema creation or after the populations scripts?

Create indexes after pupulating the data.
Benefits:
1) Data population will be faster as there is no index update.
2) New index will be free from fragmentation.
GSquared
GSquared
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24813 Visits: 9730
If you need the indexes to accomplish the data population efficiently, then create them before. Otherwise, after.

For example, if one of the tables you need to populate depends on data in one of the other tables you are populating, there might be an index that makes that more efficient. In that case, creating that index might be a good idea.

But for bulk-loading tables, creating the indexes afterwards is definitely faster and better.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Hughesy
Hughesy
SSC Veteran
SSC Veteran (210 reputation)SSC Veteran (210 reputation)SSC Veteran (210 reputation)SSC Veteran (210 reputation)SSC Veteran (210 reputation)SSC Veteran (210 reputation)SSC Veteran (210 reputation)SSC Veteran (210 reputation)

Group: General Forum Members
Points: 210 Visits: 498
Thanks both for the information.

I have decided to leave the primary constraints and unique constraints which by definition create indexes on the table creation statements so that the data is consistent. I will then insert the new data (these are scripts that insert into() select... from etc etc the five source databases) and afterwards re-build the indexes on all tables using the ALTER INDEX ALL ON statement.

I will then apply the non-clustered indexes for the foreign key fields and run sp_updatestats so that the Query optimizer work more effectively.

Does this seem like a good way of ensuring good performance from the indexes?

As a note, this will purely be a READ_ONLY database and therefore I have left the fillfactor at 100%
Anipaul
Anipaul
SSCertifiable
SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)

Group: General Forum Members
Points: 7459 Visits: 1407
Create index after populating database surely will improve the performance of populating database. Your actions are good.



Steve Jones
Steve Jones
SSC Guru
SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)

Group: Administrators
Points: 66170 Visits: 19120
Agree with above. Do it after.

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
Matt Miller (4)
Matt Miller (4)
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12881 Visits: 18584
Agreed as long as we're only talking about non-clustered indexes.

If we were talking clustered indexes, it becomes a "it depends". Assuming the data files were roughly in the clustered index order it would make sense to create the clustered key FIRST, and then load. If the file order is vastly different from the clustered index, it is usually faster to add it in after the initial load.

----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
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