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 436531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:29:14+00:00 2026-05-12T20:29:14+00:00

I want to do some quick inserts but avoid duplicates into a Table. For

  • 0

I want to do some quick inserts but avoid duplicates into a Table.
For argument’s sake lets call it MarketPrices, I’ve been experimenting with two ways of doing it but not sure how to benchmark which will be faster.

INSERT INTO MarketPrices (SecurityCode, BuyPrice, SellPrice, IsMarketOpen)
SELECT @SecurityCode, @BuyPrice,  @SellPrice, @IsMarketOpen
EXCEPT
SELECT SecurityCode, BuyPrice, SellPrice, j.bool as IsActive FROM MarketPrices
CROSS JOIN (SELECT 0 as bool UNION SELECT 1 as bool ) as j

OR

DECLARE @MktId int
SET @MktId = (SELECT SecurityId FROM MarketPrices 
              where SecurityCode = @SecurityCode 
              and BuyPrice=@BuyPrice 
              and SellPrice = @SellPrice)

IF (@MktId is NULL)  
BEGIN
    INSERT INTO MarketPrices (SecurityCode, BuyPrice, SellPrice, IsMarketOpen)
    VALUES
    (@SecurityCode,@BuyPrice, @SellPrice, @IsMarketOpen)
END

Assume that @whatever is an input parameter in the stored procedure.

I want to be able to insert a new record for every SecurityCode when the BuyPrice or SellPrice or both are different from every other previous occurance. I don’t care about IsMarketOpen.

Is there anything glaringly stupid about either of the above approaches? Is one faster than the other?

  • 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-12T20:29:14+00:00Added an answer on May 12, 2026 at 8:29 pm

    EDIT: to prevent race conditions in concurrent environments, use WITH (UPDLOCK) in the correlated subquery or EXCEPT‘d SELECT. The test script I wrote below doesn’t require it, since it uses temporary tables that are only visible to the current connection, but in a real environment, operating against user tables, it would be necessary.

    MERGE doesn’t require UPDLOCK.


    Inspired by mcl’s answer re: unique index & let the database throw an error, I decided to benchmark conditional inserts vs. try/catch.

    The results appear to support the conditional insert over try/catch, but YMMV. It’s a very simple scenario (one column, small table, etc), executed on one machine, etc.

    Here are the results (SQL Server 2008, build 10.0.1600.2):

    duplicates (short table)    
      try/catch:                14440 milliseconds / 100000 inserts
      conditional insert:        2983 milliseconds / 100000 inserts
      except:                    2966 milliseconds / 100000 inserts
      merge:                     2983 milliseconds / 100000 inserts
    
    uniques
      try/catch:                 3920 milliseconds / 100000 inserts
      conditional insert:        3860 milliseconds / 100000 inserts
      except:                    3873 milliseconds / 100000 inserts
      merge:                     3890 milliseconds / 100000 inserts
    
      straight insert:           3173 milliseconds / 100000 inserts
    
    duplicates (tall table)
      try/catch:                14436 milliseconds / 100000 inserts
      conditional insert:        3063 milliseconds / 100000 inserts
      except:                    3063 milliseconds / 100000 inserts
      merge:                     3030 milliseconds / 100000 inserts
    

    Notice, even on unique inserts, there’s slightly more overhead to try/catch than a conditional insert. I wonder if this varies by version, CPU, number of cores, etc.

    I did not benchmark the IF conditional inserts, just WHERE. I assume the IF variety would show more overhead, since a) would you have two statements, and b) you would need to wrap the two statements in a transaction and set the isolation level to serializable (!). If someone wanted to test this, you would need to change the temp table to a regular user table (serializable doesn’t apply to local temp tables).

    Here is the script:

    -- tested on SQL 2008.
    -- to run on SQL 2005, comment out the statements using MERGE
    set nocount on
    
    if object_id('tempdb..#temp') is not null drop table #temp
    create table #temp (col1 int primary key)
    go
    
    -------------------------------------------------------
    
    -- duplicate insert test against a table w/ 1 record
    
    -------------------------------------------------------
    
    insert #temp values (1)
    go
    
    declare @x int, @y int, @now datetime, @duration int
    select @x = 1, @y = 0, @now = getdate()
    while @y < 100000 begin
      set @y = @y+1
      begin try 
        insert #temp select @x
      end try
      begin catch end catch
    end
    set @duration = datediff(ms,@now,getdate())
    raiserror('duplicates (short table), try/catch: %i milliseconds / %i inserts',-1,-1,@duration,@y) with nowait
    go
    
    declare @x int, @y int, @now datetime, @duration int
    select @x = 1, @y = 0, @now = getdate()
    while @y < 100000 begin
      set @y = @y+1
      insert #temp select @x where not exists (select * from #temp where col1 = @x)
    end
    set @duration = datediff(ms,@now,getdate())
    raiserror('duplicates (short table), conditional insert: %i milliseconds / %i inserts',-1,-1,@duration, @y) with nowait
    go
    
    declare @x int, @y int, @now datetime, @duration int
    select @x = 1, @y = 0, @now = getdate()
    while @y < 100000 begin
      set @y = @y+1
      insert #temp select @x except select col1 from #temp
    end
    set @duration = datediff(ms,@now,getdate())
    raiserror('duplicates (short table), except: %i milliseconds / %i inserts',-1,-1,@duration, @y) with nowait
    go
    
    -- comment this batch out for SQL 2005
    declare @x int, @y int, @now datetime, @duration int
    select @x = 1, @y = 0, @now = getdate()
    while @y < 100000 begin
      set @y = @y+1
      merge #temp t using (select @x) s (col1) on t.col1 = s.col1 when not matched by target then insert values (col1);
    end
    set @duration = datediff(ms,@now,getdate())
    raiserror('duplicates (short table), merge: %i milliseconds / %i inserts',-1,-1,@duration, @y) with nowait
    go
    
    -------------------------------------------------------
    
    -- unique insert test against an initially empty table
    
    -------------------------------------------------------
    
    truncate table #temp
    declare @x int, @now datetime, @duration int
    select @x = 0, @now = getdate()
    while @x < 100000 begin
      set @x = @x+1
      insert #temp select @x
    end
    set @duration = datediff(ms,@now,getdate())
    raiserror('uniques, straight insert: %i milliseconds / %i inserts',-1,-1,@duration, @x) with nowait
    go
    
    truncate table #temp
    declare @x int, @now datetime, @duration int
    select @x = 0, @now = getdate()
    while @x < 100000 begin
      set @x = @x+1
      begin try 
        insert #temp select @x
      end try
      begin catch end catch
    end
    set @duration = datediff(ms,@now,getdate())
    raiserror('uniques, try/catch: %i milliseconds / %i inserts',-1,-1,@duration, @x) with nowait
    go
    
    truncate table #temp
    declare @x int, @now datetime, @duration int
    select @x = 0, @now = getdate()
    while @x < 100000 begin
      set @x = @x+1
      insert #temp select @x where not exists (select * from #temp where col1 = @x)
    end
    set @duration = datediff(ms,@now,getdate())
    raiserror('uniques, conditional insert: %i milliseconds / %i inserts',-1,-1,@duration, @x) with nowait
    go
    
    truncate table #temp
    declare @x int, @now datetime, @duration int
    select @x = 0, @now = getdate()
    while @x < 100000 begin
      set @x = @x+1
      insert #temp select @x except select col1 from #temp
    end
    set @duration = datediff(ms,@now,getdate())
    raiserror('uniques, except: %i milliseconds / %i inserts',-1,-1,@duration, @x) with nowait
    go
    
    -- comment this batch out for SQL 2005
    truncate table #temp
    declare @x int, @now datetime, @duration int
    select @x = 1, @now = getdate()
    while @x < 100000 begin
      set @x = @x+1
      merge #temp t using (select @x) s (col1) on t.col1 = s.col1 when not matched by target then insert values (col1);
    end
    set @duration = datediff(ms,@now,getdate())
    raiserror('uniques, merge: %i milliseconds / %i inserts',-1,-1,@duration, @x) with nowait
    go
    
    -------------------------------------------------------
    
    -- duplicate insert test against a table w/ 100000 records
    
    -------------------------------------------------------
    
    declare @x int, @y int, @now datetime, @duration int
    select @x = 1, @y = 0, @now = getdate()
    while @y < 100000 begin
      set @y = @y+1
      begin try 
        insert #temp select @x
      end try
      begin catch end catch
    end
    set @duration = datediff(ms,@now,getdate())
    raiserror('duplicates (tall table), try/catch: %i milliseconds / %i inserts',-1,-1,@duration,@y) with nowait
    go
    
    declare @x int, @y int, @now datetime, @duration int
    select @x = 1, @y = 0, @now = getdate()
    while @y < 100000 begin
      set @y = @y+1
      insert #temp select @x where not exists (select * from #temp where col1 = @x)
    end
    set @duration = datediff(ms,@now,getdate())
    raiserror('duplicates (tall table), conditional insert: %i milliseconds / %i inserts',-1,-1,@duration, @y) with nowait
    go
    
    declare @x int, @y int, @now datetime, @duration int
    select @x = 1, @y = 0, @now = getdate()
    while @y < 100000 begin
      set @y = @y+1
      insert #temp select @x except select col1 from #temp
    end
    set @duration = datediff(ms,@now,getdate())
    raiserror('duplicates (tall table), except: %i milliseconds / %i inserts',-1,-1,@duration, @y) with nowait
    go
    
    -- comment this batch out for SQL 2005
    declare @x int, @y int, @now datetime, @duration int
    select @x = 1, @y = 0, @now = getdate()
    while @y < 100000 begin
      set @y = @y+1
      merge #temp t using (select @x) s (col1) on t.col1 = s.col1 when not matched by target then insert values (col1);
    end
    set @duration = datediff(ms,@now,getdate())
    raiserror('duplicates (tall table), merge: %i milliseconds / %i inserts',-1,-1,@duration, @y) with nowait
    go
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could take a look at SyndicationFeed or at SyndicationContent. May 12, 2026 at 11:09 pm
  • Editorial Team
    Editorial Team added an answer If your data in real life is many-to-many then I… May 12, 2026 at 11:09 pm
  • Editorial Team
    Editorial Team added an answer The scans are automated and can generate false positives. It… May 12, 2026 at 11:09 pm

Related Questions

Just a quick database design question: Do you ALWAYS use an ID field in
I am new to JQuery. I'm reading JQuery in action, but only up to
I have a process where an incoming user request to our system is being
Here's the scenario: I'm an SSIS virgin, but I found an excuse to start

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.