Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

.net - SQL Server 2008: INSERT if not exits, maintain unique column

I'm using SQL Server 2008.

I've got a column NVARCHAR(MAX) in a table which I want to make sure is unique. The table has 600,000 records and grows every day by 50,000 records.

Currently before adding an item to the table I check if it exists in the table and if not I insert it.

IF NOT EXISTS (SELECT * FROM Softs Where Title = 'example example example.')
BEGIN
INSERT INTO Softs (....)
VALUES (...)
END

I don't have a index on the Title column

Recently, I started getting timeouts when inserting items to the table.

What would be the correct way to maintain the uniques?

If it would really help I can change the NVARCHAR(MAX) to NVARCHAR(450)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's madness not to have an index.

It would help but the index key length can only be 900 bytes.

However, it's likely you already have duplicates because the potential for a 2nd EXISTS to run after the 1st EXISTS but before the 1st INSERT.

The index creation will tell you, and subsequently protect against this.

However, you can get errors under heavy load.

My favoured approach for high inserts/low duplicates is the JFDI pattern. Highly concurrent

BEGIN TRY
   INSERT etc
END TRY
BEGIN CATCH
    IF ERROR_NUMBER() <> 2627
      RAISERROR etc
END CATCH

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...