Say I have a simple table that has the following fields:
- ID: int, autoincremental (identity), primary key
- Name: varchar(50), unique, has unique index
- Tag: int
I never use the ID field for lookup, because my application is always based on working with the Name field.
I need to change the Tag value from time to time. I'm using the following trivial SQL code:
UPDATE Table SET Tag = XX WHERE Name = YY;
I wondered if anyone knows whether the above is always faster than:
DELETE FROM Table WHERE Name = YY;
INSERT INTO Table (Name, Tag) VALUES (YY, XX);
Again - I know that in the second example the ID is changed, but it does not matter for my application.
One command on the same row should always be faster than two on that same row. So the UPDATE only would be better.
EDIT set up the table:
run this, which takes 1 second on my system (sql server 2005):
run this, which took 2 seconds on my system:
Obviously, the answer varies based on what database you are using, but UPDATE can always be implemented faster than DELETE+INSERT. Since in-memory ops are mostly trivial anyways, given a hard-drive based database, an UPDATE can change a database field in-place on the hdd, while a delete would remove a row (leaving an empty space), and insert a new row, perhaps to the end of the table (again, it's all in the implementation).
The other, minor, issue is that when you UPDATE a single variable in a single row, the other columns in that row remain the same. If you DELETE and then do an INSERT, you run the risk of forgetting about other columns and consequently leaving them behind (in which case you would have to do a SELECT before your DELETE to temporarily store your other columns before writing them back with INSERT).
Just tried updating 43 fields on a table with 44 fields, the remaining field was the primary clustered key.
The update took 8 seconds.
A Delete + Insert is faster than the minimum time interval that the "Client Statistics" reports via SQL Management Studio.
Peter
MS SQL 2008
I am afraid the body of your question is unrelated to title question.
If to answer the title:
then answer is NO!
Just google for
Such update(s) result in more costly (more processing) realization of update through insert+update than direct insert+update. These are the cases when
My fast (non-exhaustive) search, not pretending to be covering one, gave me [1], [2]
[1]
Update Operations
(Sybase® SQL Server Performance and Tuning Guide
Chapter 7: The SQL Server Query Optimizer)
http://www.lcard.ru/~nail/sybase/perf/11500.htm
[2]
UPDATE Statements May be Replicated as DELETE/INSERT Pairs
http://support.microsoft.com/kb/238254
The question of speed is irrelevant without a specific speed problem.
If you are writing SQL code to make a change to an existing row, you UPDATE it. Anything else is incorrect.
If you're going to break the rules of how code should work, then you'd better have a damn good, quantified reason for it, and not a vague idea of "This way is faster", when you don't have any idea what "faster" is.
The bigger the table (number of and size of columns) the more expensive it becomes to delete and insert rather than update. Because you have to pay the price of UNDO and REDO. DELETEs consume more UNDO space than UPDATEs, and your REDO contains twice as many statements as are necessary.
Besides, it is plain wrong from a business point of view. Consider how much harder it would be to understand a notional audit trail on that table.
There are some scenarios involving bulk updates of all the rows in a table where it is faster to create a new table using CTAS from the old table (applying the update in the the projection of the SELECT clause), dropping the old table and renaming the new table. The side-effects are creating indexes, managing constraints and renewing privileges, but it is worth considering.