Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Receiving duplicate key insert error with DISTINCT clause Expand / Collapse
Author
Message
Posted Wednesday, March 11, 2009 12:29 PM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, August 8, 2014 3:17 PM
Points: 60, Visits: 563
I have been working on a bug for a couple days now which I have determined to be caused by the use of the NOLOCK in a query. Here is the query
Declare @AbsTab table (org_id int, absr_id int, abs_id int, abs_date datetime primary key (absr_id, abs_id,abstab_id))  
Insert @AbsTab
select
a.org_id,
a.absr_id,
a.abs_id,
a.abs_date
from absence a (nolock)
where a.abs_date between @StartDate and @EndDate
and a.abs_deleted = 0
and a.abs_id not in (select abs_id from @CloseAbs)

I have determined that the (NOLOCK) is causing me problems because this query is using a covering index that includes updatable fields and we are seeing dirty reads. These dirty reads are causing duplicate key insert errors.

Msg 2627, Level 14, State 1, Line 117
Violation of PRIMARY KEY constraint 'PK__#4B31A61E__4C25CA57'. Cannot insert duplicate key in object 'dbo.@AbsTab'.

OK...I can understand this much and I can accept it, after all this is the sacrifice we have to make when we choose to use isolation level READ UNCOMMITTED.
My QUESTION, if I put a DISTINCT clause on the SELECT statement shouldn't this guarantee me unique rows?
Insert  @AbsTab  
select DISTINCT
a.org_id,
a.absr_id,
a.abs_id,
a.abs_date
from absence a (nolock)
where a.abs_date between @StartDate and @EndDate
and a.abs_deleted = 0
and a.abs_id not in (select abs_id from @CloseAbs)

I have tested this and I continue to receive errors.
I have written some code to trap the results being inserted into this table:

11053 31769404 46229984 2009-03-11 00:00:00.000
11053 31769404 46229984 2009-03-11 00:00:00.000

As you can see, the values are identical as far as I can tell.
Can anyone explain why the DISTINCT does not work in this case.

By the way, I am able to eliminate the error by adding an IDENTITY column to the table variable and making this column part of the PK. This does not get around the duplicate data but it does resolve the DUPLICATE KEY INSERT errors.
Post #673611
Posted Wednesday, March 11, 2009 1:57 PM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Tuesday, May 6, 2014 5:51 AM
Points: 6,266, Visits: 2,028
Are you sure only **one** insert is going at a time ?



* Noel
Post #673683
Posted Wednesday, March 11, 2009 2:08 PM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, August 8, 2014 3:17 PM
Points: 60, Visits: 563
Not sure exactly what your asking.
This is a table variable so all inserts are isolated to this process.
The plan is not taking advantage of parallelism.
Let me know if this does not answer your question.
Post #673695
Posted Wednesday, March 11, 2009 2:24 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, November 4, 2012 12:23 PM
Points: 2,087, Visits: 3,932
Hi Eric

The NOLOCK may be your problem if there are very much data manipulation while your statement is running. Maybe have a look at:

SET TRANSACTION ISOLATION LEVEL SNAPSHOT

Greets
Flo



The more I learn, the more I know what I do not know
Blog: Things about Software Architecture, .NET development and T-SQL

How to Post Data/Code to get the best Help How to Post Performance Problems
Post #673715
Posted Wednesday, March 11, 2009 2:30 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, July 21, 2014 8:27 AM
Points: 1,343, Visits: 7,176
When I try to run just your declare:
(Declare @AbsTab table (org_id int, absr_id int, abs_id int, abs_date datetime primary key (absr_id, abs_id,abstab_id)))

I get:
Msg 1911, Level 16, State 1, Line 1
Column name 'abstab_id' does not exist in the target table or view.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.

I'm curious, one of the errors you got referred to 'dbo.@AbsTab'. Is that a "hard" table rather than a table variable?

bc


bc
Post #673720
Posted Wednesday, March 11, 2009 2:37 PM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, August 8, 2014 3:17 PM
Points: 60, Visits: 563
Sorry...my bad
remove abstabid from the PK
that was a remnant from my previous testing...jugling a lot of stuff
Post #673728
Posted Wednesday, March 11, 2009 2:38 PM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, August 8, 2014 3:17 PM
Points: 60, Visits: 563
@AbsTab is a declared table variable
Post #673730
Posted Wednesday, March 11, 2009 2:40 PM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, August 8, 2014 3:17 PM
Points: 60, Visits: 563
The table that I am querying is too hot of a table to turn snapshot processing on.
I am more curious as to why DISTINCT does not work here.

Post #673732
Posted Wednesday, March 11, 2009 2:54 PM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Tuesday, May 6, 2014 5:51 AM
Points: 6,266, Visits: 2,028
Nevermind I need some coffee :d



* Noel
Post #673746
Posted Wednesday, March 11, 2009 2:56 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, July 21, 2014 8:27 AM
Points: 1,343, Visits: 7,176
Do you see dupes when you run just the Select DISTINCT?

select DISTINCT
a.org_id,
a.absr_id,
a.abs_id,
a.abs_date
from absence a (nolock)
where a.abs_date between @StartDate and @EndDate
and a.abs_deleted = 0
and a.abs_id not in (select abs_id from @CloseAbs)

I can think of no reason DISTINCT shouldn't do as its told.

bc


bc
Post #673751
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse