<p>I have a sproc that puts 750K records into a temp table through a query as one of its first actions.  If I create indexes on the temp table before filling it, the item takes about twice as long to run compared to when I index after filling the table.  (The index is an integer in a single column, the table being indexed is just two columns each a single integer.)</p>

<p>This seems a little off to me, but then I don't have the firmest understanding of what goes on under the hood.  Does anyone have an answer for this? </p>

[{'answer_id': 28882, 'author': 'SQLMenace', 'author_id': 740, 'author_profile': 'https://Stackoverflow.com/users/740', 'pm_score': 1, 'selected': False, 'text': '<p>this is because if the data you insert is not in the order of the index, SQL will have to split pages to make room for additional rows to keep them together logically</p>\n'}, {'answer_id': 28884, 'author': 'Iker Jimenez', 'author_id': 2697, 'author_profile': 'https://Stackoverflow.com/users/2697', 'pm_score': 2, 'selected': False, 'text': "<p>You should NEVER EVER create an index on an empty table if you are going to massively load it right afterwards.\nIndexes have to be maintained as the data on the table changes, so imagine as if for every insert on the table the index was being recalculated (which is an expensive operation).\nLoad the table first and create the index after finishing with the load.\nThat's were the performance difference is going.</p>\n"}, {'answer_id': 28888, 'author': 'palehorse', 'author_id': 312, 'author_profile': 'https://Stackoverflow.com/users/312', 'pm_score': 2, 'selected': False, 'text': '<p>After performing large data manipulation operations, you frequently have to update the underlying indexes.  You can do that by using the UPDATE STATISTICS [table] statement.</p>\n\n<p>The other option is to drop and recreate the index which, if you are doing large data insertions, will likely perform the inserts much faster.  You can even incorporate that into your stored procedure.</p>\n'}, {'answer_id': 28892, 'author': 'Jon Galloway', 'author_id': 5, 'author_profile': 'https://Stackoverflow.com/users/5', 'pm_score': 5, 'selected': False, 'text': "<p>If you create a clustered index, it affects the way the data is physically ordered on the disk. It's better to add the index after the fact and let the database engine reorder the rows when it knows how the data is distributed.</p>\n\n<p>For example, let's say you needed to build a brick wall with numbered bricks so that those with the highest number are at the bottom of the wall. It would be a difficult task if you were just handed the bricks in random order, one at a time - you wouldn't know which bricks were going to turn out to be the highest numbered, and you'd have to tear the wall down and rebuild it over and over. It would be a lot easier to handle that task if you had all the bricks lined up in front of you, and could organize your work.</p>\n\n<p>That's how it is for the database engine - if you let it know about the whole job, it can be much more efficient than if you just feed it a row at a time.</p>\n"}, {'answer_id': 28897, 'author': 'tghw', 'author_id': 2363, 'author_profile': 'https://Stackoverflow.com/users/2363', 'pm_score': 3, 'selected': False, 'text': "<p>It's because the database server has to do calculations each and every time you insert a new row.  Basically, you end up reindexing the table each time.  It doesn't seem like a very expensive operation, and it's not, but when you do that many of them together, you start to see the impact.  That's why you usually want to index after you've populated your rows, since it will just be a one-time cost.</p>\n"}, {'answer_id': 28902, 'author': 'Dima Malenko', 'author_id': 2586, 'author_profile': 'https://Stackoverflow.com/users/2586', 'pm_score': 1, 'selected': False, 'text': '<p>This due to the fact that when SQL Server indexes table with data it is able to produce exact statistics of values in indexed column. At some moments SQL Server will recalculate statistics, but when you perform massive inserts the distribution of values may change after the statistics was calculated last time.</p>\n\n<p>The fact that statistics is out of date can be discovered on Query Analyzer. When you see that on a certain table scan number of rows expected differs to much from actual numbers of rows processed.</p>\n\n<p>You should use <a href="http://msdn.microsoft.com/en-us/library/ms187348.aspx" rel="nofollow noreferrer">UPDATE STATISTICS</a> to recalculate distribution of values after you insert all the data. After that no performance difference should be observed.</p>\n'}, {'answer_id': 28904, 'author': 'jason saldo', 'author_id': 1293, 'author_profile': 'https://Stackoverflow.com/users/1293', 'pm_score': 2, 'selected': False, 'text': '<p>Think of it this way.</p>\n<blockquote>\n<p>Given<br />\nunorderedList = {5, 1,3}<br />\norderedList = {1,3,5}</p>\n<p>add 2 to both lists.<br />\nunorderedList = {5, 1,3,2}<br />\norderedList = {1,2,3,5}</p>\n</blockquote>\n<p>What list do you think is easier to add to?</p>\n<p>Btw ordering your input before load will give you a boost.</p>\n'}, {'answer_id': 28905, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 1, 'selected': False, 'text': "<p>If you have an index on a table, as you add data to the table SQL Server will have to re-order the table to make room in the appropriate place for the new records.  If you're adding a lot of data, it will have to reorder it over and over again.  By creating an index only after the data is loaded, the re-order only needs to happen once.</p>\n\n<p>Of course, if you are importing the records in index order it shouldn't matter so much.</p>\n"}, {'answer_id': 29202, 'author': 'Dana the Sane', 'author_id': 2567, 'author_profile': 'https://Stackoverflow.com/users/2567', 'pm_score': 1, 'selected': False, 'text': '<p>In addition to the index overhead, running each query as a transaction is a bad idea for the same reason. If you run chunks of inserts (say 100) within 1 explicit transaction, you should also see a performance increase.</p>\n'}]
['https://Stackoverflow.com/questions/28877', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1327/']