<p>I have a table with more than a millon rows. This table is used to index <code>tiff</code> images. Each image has fields like <code>date</code>, <code>number</code>, etc. I have users that index these images in batches of 500. I need to know if it is better to first insert 500 rows and then perform 500 updates or, when the user finishes indexing, to do the 500 inserts with all the data. A very important thing is that if I do the 500 inserts at first, this time is free for me because I can do it the night before. </p>

<p>So the question is: is it better to do inserts or inserts and updates, and why? I have defined a <code>id</code> value for each image, and I also have other indices on the fields.</p>

[{'answer_id': 41854, 'author': 'Giovanni Galbo', 'author_id': 4050, 'author_profile': 'https://Stackoverflow.com/users/4050', 'pm_score': 0, 'selected': False, 'text': "<p>I'm not a database guy, but I imagine doing the inserts in one shot would be faster because the updates require a lookup whereas the inserts do not.</p>\n"}, {'answer_id': 41863, 'author': 'Krishna Kumar', 'author_id': 2141, 'author_profile': 'https://Stackoverflow.com/users/2141', 'pm_score': 2, 'selected': False, 'text': '<p>Doing the inserts first and then the updates does seem to be a better idea for several reasons. You will be inserting at a time of low transaction volume. Since inserts have more data, this is a better time to do it.</p>\n\n<p>Since you are using an id value (which is presumably indexed) for updates, the overhead of updates will be very low. You would also have less data during your updates.</p>\n\n<p>You could also turn off transactions at the batch (500 inserts/updates) level and use it for each individual record, thus reducing some overhead.</p>\n\n<p>Finally, test this out to see the actual performance on your server before making a final decision.</p>\n'}, {'answer_id': 41873, 'author': 'Booji Boy', 'author_id': 1433, 'author_profile': 'https://Stackoverflow.com/users/1433', 'pm_score': 1, 'selected': False, 'text': '<p>The execution plan for each query will tell you which one should be more expensive. The real limiting factor will be the writes to disk, so you may need to run some tests while running perfmon to see which query causes more writes and causes the disk queue to get the longest (longer is bad).  </p>\n'}, {'answer_id': 41884, 'author': 'spoulson', 'author_id': 3347, 'author_profile': 'https://Stackoverflow.com/users/3347', 'pm_score': 2, 'selected': False, 'text': "<p>This isn't a cut and dry question.  Krishna's and Galegian's points are spot on.</p>\n\n<p>For updates, the impact will be lessened if the updates are affecting fixed-length fields.  If updating varchar or blob fields, you may add a cost of page splits during update when the new value surpasses the length of the old value.</p>\n"}, {'answer_id': 41895, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': 2, 'selected': False, 'text': "<p>I think inserts will run faster.  They do not require a lookup (when you do an update you are basically doing the equivalent of a select with the where clause).  And also, an insert won't lock the rows the way an update will, so it won't interfere with any selects that are happening against the table at the same time.</p>\n"}, {'answer_id': 41899, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 6, 'selected': True, 'text': "<p>Updates in Sql server result in ghosted rows - i.e. Sql crosses one row out and puts a new one in.  The crossed out row is deleted later.</p>\n\n<p>Both inserts and updates can cause page-splits in this way, they both effectively 'add' data, it's just that updates flag the old stuff out first.</p>\n\n<p>On top of this updates need to look up the row first, which for lots of data can take longer than the update.</p>\n\n<p>Inserts will just about always be quicker, especially if they are either in order or if the underlying table doesn't have a clustered index.</p>\n\n<p>When inserting larger amounts of data into a table look at the current indexes - they can take a while to change and build.  Adding values in the middle of an index is always slower.</p>\n\n<p>You can think of it like appending to an address book: Mr Z can just be added to the last page, while you'll have to find space in the middle for Mr M.</p>\n"}]
['https://Stackoverflow.com/questions/41837', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1154/']