Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 610455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:40:38+00:00 2026-05-13T17:40:38+00:00

There is quite often situation when you need to execute INSERT, UPDATE or DELETE

  • 0

There is quite often situation when you need to execute INSERT, UPDATE or DELETE statement based on some condition. And my question is whether the affect on the performance of the query add IF EXISTS before the command.

Example

IF EXISTS(SELECT 1 FROM Contacs WHERE [Type] = 1)
    UPDATE Contacs SET [Deleted] = 1 WHERE [Type] = 1

What about INSERTs or DELETEs?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:40:38+00:00Added an answer on May 13, 2026 at 5:40 pm

    I’m not completely sure, but I get the impression that this question is really about upsert, which is the following atomic operation:

    • If the row exists in both the source and target, UPDATE the target;
    • If the row only exists in the source, INSERT the row into the target;
    • (Optionally) If the row exists in the target but not the source, DELETE the row from the target.

    Developers-turned-DBAs often naïvely write it row-by-row, like this:

    -- For each row in source
    IF EXISTS(<target_expression>)
        IF @delete_flag = 1
            DELETE <target_expression>
        ELSE
            UPDATE target
            SET <target_columns> = <source_values>
            WHERE <target_expression>
    ELSE
        INSERT target (<target_columns>)
        VALUES (<source_values>)
    

    This is just about the worst thing you can do, for several reasons:

    • It has a race condition. The row can disappear between IF EXISTS and the subsequent DELETE or UPDATE.

    • It’s wasteful. For every transaction you have an extra operation being performed; maybe it’s trivial, but that depends entirely on how well you’ve indexed.

    • Worst of all – it’s following an iterative model, thinking about these problems at the level of a single row. This will have the largest (worst) impact of all on overall performance.

    One very minor (and I emphasize minor) optimization is to just attempt the UPDATE anyway; if the row doesn’t exist, @@ROWCOUNT will be 0 and you can then “safely” insert:

    -- For each row in source
    BEGIN TRAN
    
    UPDATE target
    SET <target_columns> = <source_values>
    WHERE <target_expression>
    
    IF (@@ROWCOUNT = 0)
        INSERT target (<target_columns>)
        VALUES (<source_values>)
    
    COMMIT
    

    Worst-case, this will still perform two operations for every transaction, but at least there’s a chance of only performing one, and it also eliminates the race condition (kind of).

    But the real issue is that this is still being done for each row in the source.

    Before SQL Server 2008, you had to use an awkward 3-stage model to deal with this at the set level (still better than row-by-row):

    BEGIN TRAN
    
    INSERT target (<target_columns>)
    SELECT <source_columns> FROM source s
    WHERE s.id NOT IN (SELECT id FROM target)
    
    UPDATE t SET <target_columns> = <source_columns>
    FROM target t
    INNER JOIN source s ON t.d = s.id
    
    DELETE t
    FROM target t
    WHERE t.id NOT IN (SELECT id FROM source)
    
    COMMIT
    

    As I said, performance was pretty lousy on this, but still a lot better than the one-row-at-a-time approach. SQL Server 2008, however, finally introduced MERGE syntax, so now all you have to do is this:

    MERGE target
    USING source ON target.id = source.id
    WHEN MATCHED THEN UPDATE <target_columns> = <source_columns>
    WHEN NOT MATCHED THEN INSERT (<target_columns>) VALUES (<source_columns>)
    WHEN NOT MATCHED BY SOURCE THEN DELETE;
    

    That’s it. One statement. If you’re using SQL Server 2008 and need to perform any sequence of INSERT, UPDATE and DELETE depending on whether or not the row already exists – even if it’s just one row – there is no excuse not to be using MERGE.

    You can even OUTPUT the rows affected by a MERGE into a table variable if you need to find out afterward what was done. Simple, fast, and risk-free. Do it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.