In SQL, is UPDATE always faster than DELETE+INSERT?

前端 未结 15 2200
梦谈多话
梦谈多话 2020-11-29 20:01

Say I have a simple table that has the following fields:

  1. ID: int, autoincremental (identity), primary key
  2. Name: varchar(50), unique, has unique index<
15条回答
  •  天命终不由人
    2020-11-29 20:39

    I am afraid the body of your question is unrelated to title question.

    If to answer the title:

    In SQL, is UPDATE always faster than DELETE+INSERT?

    then answer is NO!

    Just google for

    • "Expensive direct update"* "sql server"
    • "deferred update"* "sql server"

    Such update(s) result in more costly (more processing) realization of update through insert+update than direct insert+update. These are the cases when

    • one updates the field with unique (or primary) key or
    • when the new data does not fit (is bigger) in the pre-update row space allocated (or even maximum row size),resulting in fragmentation,
    • etc.

    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

提交回复
热议问题