January 18, 2014 at 1:36 pm
Hi,
I have a table with some columns & rows. I want to do insert and update on that table. Which action is faster inserting 1 row or updating 1row? Please let me know the answer with detailed explanation.
January 18, 2014 at 10:18 pm
ramana3327 (1/18/2014)
Hi,I have a table with some columns & rows. I want to do insert and update on that table. Which action is faster inserting 1 row or updating 1row? Please let me know the answer with detailed explanation.
This sounds a whole lot like an interview question that I might ask of a Senior DBA or a test question I might ask if teaching. I suggest you lookup INSERT and UPDATE along with Indexes, Foreign Keys, and Triggers. After you discover the answer, then post it here with a "detailed explanation" and we'll let you know if you got it right or not.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 19, 2014 at 2:24 am
I would ask why it matters. If you need to change a value in a row, you need to update. If you need to put a new row in the table, you need to insert. Whether one is a few microseconds faster than the other isn't really relevant here, they're not substitutable for each other.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
January 19, 2014 at 4:01 am
Further, it's completely dependent on a number of factors that you haven't outlined. You're not comparing apples to apples in this question.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
January 19, 2014 at 9:17 am
Hi,
Thanks to everyone.
I read that update is faster. Why because already table space is fixed, and you are just changing the value but for the insertion it needs to find the space for the insertion. I hope this is right one, please let me know.
January 19, 2014 at 10:23 am
Maybe, maybe not. There's a hell of a lot of factors involved.
Again, why is it a concern? It's not as if you can replace an insert with an update just because the update is a couple microseconds faster.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
January 19, 2014 at 12:11 pm
Like I said, it's probably an interview question where there's not an actual correct answer other than "It Depends". It's a nice short question that requires a lot of knowledge of the things I mentioned without coming out to ask specific questions about each of those items and also prevents "rote" answers because it will cause an extended discussion if the person actually knows those things.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 19, 2014 at 1:03 pm
ramana3327 (1/19/2014)
Hi,Thanks to everyone.
I read that update is faster. Why because already table space is fixed, and you are just changing the value but for the insertion it needs to find the space for the insertion. I hope this is right one, please let me know.
What about a case where modifying the data changes a clustered index key value? Will that still be faster? Again, you're comparing apples and hammers. UPDATE is one operation and INSERT is a different. They serve different purposes and have different requirements. One absolutely doesn't substitute for the other in any way, so why would you compare them?
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
January 19, 2014 at 5:21 pm
Poor ol' lonely DELETE. No one ever asks questions about that. :laugh:
--Jeff Moden
Change is inevitable... Change for the better is not.
January 20, 2014 at 6:11 am
could it be the OP is actually confused and is really enquiring about the MERGE option?
-----------------------------------------------------------------------------------------------------------
"Ya can't make an omelette without breaking just a few eggs" 😉
January 20, 2014 at 8:24 am
Grant Fritchey (1/19/2014)
ramana3327 (1/19/2014)
Hi,Thanks to everyone.
I read that update is faster. Why because already table space is fixed, and you are just changing the value but for the insertion it needs to find the space for the insertion. I hope this is right one, please let me know.
What about a case where modifying the data changes a clustered index key value? Will that still be faster? Again, you're comparing apples and hammers. UPDATE is one operation and INSERT is a different. They serve different purposes and have different requirements. One absolutely doesn't substitute for the other in any way, so why would you compare them?
And what if you actually care about the previous value?
Either for audit, or is a slow changing dimension?
Speed is usually only one of the factors in determining how you would choose one over the other.
And in some cases, you might find yourself doing both an update and an insert. :w00t:
So I guess I wouldn't have an answer, but might have some other questions.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply