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 123»»»

Indexing and Deadlock in SQL Server 2008? Expand / Collapse
Author
Message
Posted Friday, January 03, 2014 7:58 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, January 14, 2014 4:23 AM
Points: 11, Visits: 39
All,

I am running into a deadlock in SQL Server 2008. Using a profiler I was able to determine that the deadlock happens as I am trying to select and update row(S) in table in 2 different sessions.

The select query from the profiler is shown below

select
nodeinstan0_.id as id35_,
nodeinstan0_.BPMN_ELEMENT_ID as BPMN2_35_,
nodeinstan0_.END_DATE as END3_35_,
nodeinstan0_.NODE_ID as NODE4_35_,
nodeinstan0_.NODE_INSTANCE_ID as NODE5_35_,
nodeinstan0_.NODE_NAME as NODE6_35_,
nodeinstan0_.PROCESS_INSTANCE_ID as PROCESS7_35_,
nodeinstan0_.START_DATE as START8_35_
from
NODE_INSTANCE_LOG nodeinstan0_ where
nodeinstan0_.NODE_INSTANCE_ID= @P0 and
nodeinstan0_.PROCESS_INSTANCE_ID= @P1 and
(nodeinstan0_.END_DATE is null)
My SQL Scripts for creating the table and index is show below

CREATE TABLE [dbo].[NODE_INSTANCE_LOG](
[id] [numeric](19, 0) IDENTITY(1,1) NOT NULL,
[BPMN_ELEMENT_ID] [varchar](255) NULL,
[END_DATE] [datetime] NULL,
[NODE_ID] [varchar](255) NOT NULL,
[NODE_INSTANCE_ID] [varchar](255) NOT NULL,
[NODE_NAME] [varchar](2000) NULL,
[PROCESS_INSTANCE_ID] [numeric](19, 0) NOT NULL,
[START_DATE] [datetime] NULL,
PRIMARY KEY CLUSTERED
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE NONCLUSTERED INDEX IX_NODE_INSTANCE_LOG_Index1
ON NODE_INSTANCE_LOG(NODE_INSTANCE_ID, PROCESS_INSTANCE_ID, END_DATE)
INCLUDE
( ID, BPMN_ELEMENT_ID, NODE_ID, NODE_NAME, START_DATE);
As I understood from number of other forums and blogs - e.g. http://social.msdn.microsoft.com/Forums/en-US/63b033bb-15fe-401b-9d3c-4edb56244a84/sql-server-non-clustered-index-vs-covering-index I have included the columns under the where clause in the "COVERING index" and the rest of the columns from the select query under non clustered Index. But I am still facing a deadlock.

Would appreciate if some once can point me in the right direction.

regards D
Post #1527549
Posted Friday, January 03, 2014 9:46 AM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 2:26 PM
Points: 5,850, Visits: 12,599
I think to help you people will need at least the update statement as well, but preferably either the XML from the deadlock graph in profiler or the output from trace flag 1222.

---------------------------------------------------------------------

Post #1527628
Posted Friday, January 03, 2014 9:57 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:42 PM
Points: 14,840, Visits: 27,315
The deadlock is probably being caused by multiple slow points, not just the select statement. I'd suggest getting the execution plans for the two queries involved in the deadlock and seeing what tuning opportunities you have. Also, it's pretty likely there's another data modification query involved in this process. You should identify that and check that the access across the various tables involved in all the queries involved are in the same order. A classic cause of deadlocks is reading from tableA and then needing to update tableB while another process reads from tableB and then needs to update TableA.

----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of: SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1527632
Posted Friday, January 03, 2014 10:25 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, January 14, 2014 4:23 AM
Points: 11, Visits: 39
Correct to my earlier observation. Its not a select/update that's causing the deadlock but its a select/select

I am pasting an extract from the Tracer


<deadlock victim="process6a9048">
<process-list>
<process id="process6a9048" taskpriority="0" logused="15888" waitresource="KEY: 6:72057594142588928 (fbd2e857cffa)" waittime="812" ownerId="1927048" transactionname="implicit_transaction" lasttranstarted="2014-01-03T17:01:35.453" XDES="0x91cd7960" lockMode="S" schedulerid="2" kpid="3640" status="suspended" spid="56" sbid="0" ecid="0" priority="0" trancount="1" lastbatchstarted="2014-01-03T17:01:35.723" lastbatchcompleted="2014-01-03T17:01:35.723" clientapp="jTDS" hostname="LDN-LRM-PC-189" hostpid="123" loginname="username" isolationlevel="read committed (2)" xactid="1927048" currentdb="6" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128058">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="62" sqlhandle="0x02000000e82b4000ebf38fb66978d5f9f2314a6046f957b3">
select nodeinstan0_.id as id35_, nodeinstan0_.BPMN_ELEMENT_ID as BPMN2_35_, nodeinstan0_.END_DATE as END3_35_, nodeinstan0_.NODE_ID as NODE4_35_, nodeinstan0_.NODE_INSTANCE_ID as NODE5_35_, nodeinstan0_.NODE_NAME as NODE6_35_, nodeinstan0_.PROCESS_INSTANCE_ID as PROCESS7_35_, nodeinstan0_.START_DATE as START8_35_ from NODE_INSTANCE_LOG nodeinstan0_ where nodeinstan0_.NODE_INSTANCE_ID= @P0 and nodeinstan0_.PROCESS_INSTANCE_ID= @P1 and (nodeinstan0_.END_DATE is null) </frame>
</executionStack>
<inputbuf>
(@P0 nvarchar(4000),@P1 bigint)select nodeinstan0_.id as id35_, nodeinstan0_.BPMN_ELEMENT_ID as BPMN2_35_, nodeinstan0_.END_DATE as END3_35_, nodeinstan0_.NODE_ID as NODE4_35_, nodeinstan0_.NODE_INSTANCE_ID as NODE5_35_, nodeinstan0_.NODE_NAME as NODE6_35_, nodeinstan0_.PROCESS_INSTANCE_ID as PROCESS7_35_, nodeinstan0_.START_DATE as START8_35_ from NODE_INSTANCE_LOG nodeinstan0_ where nodeinstan0_.NODE_INSTANCE_ID= @P0 and nodeinstan0_.PROCESS_INSTANCE_ID= @P1 and (nodeinstan0_.END_DATE is null) </inputbuf>
</process>
<process id="process6dddc8" taskpriority="0" logused="114316" waitresource="KEY: 6:72057594142588928 (44ec7e8e62c7)" waittime="602" ownerId="1927022" transactionname="implicit_transaction" lasttranstarted="2014-01-03T17:01:35.227" XDES="0x91d25950" lockMode="S" schedulerid="4" kpid="17412" status="suspended" spid="60" sbid="0" ecid="0" priority="0" trancount="1" lastbatchstarted="2014-01-03T17:01:35.937" lastbatchcompleted="2014-01-03T17:01:35.937" clientapp="jTDS" hostname="LDN-LRM-PC-189" hostpid="123" loginname="username" isolationlevel="read committed (2)" xactid="1927022" currentdb="6" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128058">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="62" sqlhandle="0x02000000e82b4000ebf38fb66978d5f9f2314a6046f957b3">
select nodeinstan0_.id as id35_, nodeinstan0_.BPMN_ELEMENT_ID as BPMN2_35_, nodeinstan0_.END_DATE as END3_35_, nodeinstan0_.NODE_ID as NODE4_35_, nodeinstan0_.NODE_INSTANCE_ID as NODE5_35_, nodeinstan0_.NODE_NAME as NODE6_35_, nodeinstan0_.PROCESS_INSTANCE_ID as PROCESS7_35_, nodeinstan0_.START_DATE as START8_35_ from NODE_INSTANCE_LOG nodeinstan0_ where nodeinstan0_.NODE_INSTANCE_ID= @P0 and nodeinstan0_.PROCESS_INSTANCE_ID= @P1 and (nodeinstan0_.END_DATE is null) </frame>
</executionStack>
<inputbuf>
(@P0 nvarchar(4000),@P1 bigint)select nodeinstan0_.id as id35_, nodeinstan0_.BPMN_ELEMENT_ID as BPMN2_35_, nodeinstan0_.END_DATE as END3_35_, nodeinstan0_.NODE_ID as NODE4_35_, nodeinstan0_.NODE_INSTANCE_ID as NODE5_35_, nodeinstan0_.NODE_NAME as NODE6_35_, nodeinstan0_.PROCESS_INSTANCE_ID as PROCESS7_35_, nodeinstan0_.START_DATE as START8_35_ from NODE_INSTANCE_LOG nodeinstan0_ where nodeinstan0_.NODE_INSTANCE_ID= @P0 and nodeinstan0_.PROCESS_INSTANCE_ID= @P1 and (nodeinstan0_.END_DATE is null) </inputbuf>
</process>
</process-list>
<resource-list>
<keylock hobtid="72057594142588928" dbid="6" objectname="reform.dbo.NODE_INSTANCE_LOG" indexname="IX_NODE_INSTANCE_LOG_Index1" id="lock89f43000" mode="X" associatedObjectId="72057594142588928">
<owner-list>
<owner id="process6dddc8" mode="X"/>
</owner-list>
<waiter-list>
<waiter id="process6a9048" mode="S" requestType="wait"/>
</waiter-list>
</keylock>
<keylock hobtid="72057594142588928" dbid="6" objectname="reform.dbo.NODE_INSTANCE_LOG" indexname="IX_NODE_INSTANCE_LOG_Index1" id="lock81b22a80" mode="X" associatedObjectId="72057594142588928">
<owner-list>
<owner id="process6a9048" mode="X"/>
</owner-list>
<waiter-list>
<waiter id="process6dddc8" mode="S" requestType="wait"/>
</waiter-list>
</keylock>
</resource-list>
</deadlock>

Post #1527650
Posted Friday, January 03, 2014 11:29 AM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Today @ 8:28 AM
Points: 63, Visits: 1,938
I can see from info that you have provided that your queries will not use indexes. Your table has varchar fields and query is called with nvarchar.
You need to alter connection string by setting sendStringParametersAsUnicode=false. See http://technet.microsoft.com/en-us/library/ms378988.aspx for more info.
Post #1527668
Posted Saturday, January 04, 2014 3:19 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:42 PM
Points: 14,840, Visits: 27,315
SELECT queries alone will not cause deadlocks. There has to be data modification queries built in there somewhere.

----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of: SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1527794
Posted Monday, January 06, 2014 4:13 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, January 14, 2014 4:23 AM
Points: 11, Visits: 39
Hi,

I have read the link
http://technet.microsoft.com/en-us/library/ms378988.aspx
for sendStringParametersAsUnicode=false.

I see no relationship between this parameter and indexes not being used.

Can you pls clarify?


regards
D
Post #1528026
Posted Monday, January 06, 2014 4:19 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:42 PM
Points: 14,840, Visits: 27,315
The code looks to me like it will be likely to use that index. Have you checked the execution plan to see what it is doing?

----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of: SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1528032
Posted Monday, January 06, 2014 5:15 AM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Today @ 8:28 AM
Points: 63, Visits: 1,938
angeshwar (1/6/2014)
Hi,

I have read the link
http://technet.microsoft.com/en-us/library/ms378988.aspx
for sendStringParametersAsUnicode=false.

I see no relationship between this parameter and indexes not being used.

Can you pls clarify?


<inputbuf>
(@P0 nvarchar(4000),@P1 bigint)select nodeinstan0_.id as id35_, nodeinstan0_.BPMN_ELEMENT_ID as BPMN2_35_, nodeinstan0_.END_DATE as END3_35_, nodeinstan0_.NODE_ID as NODE4_35_, nodeinstan0_.NODE_INSTANCE_ID as NODE5_35_, nodeinstan0_.NODE_NAME as NODE6_35_, nodeinstan0_.PROCESS_INSTANCE_ID as PROCESS7_35_, nodeinstan0_.START_DATE as START8_35_ from NODE_INSTANCE_LOG nodeinstan0_ where nodeinstan0_.NODE_INSTANCE_ID= @P0 and nodeinstan0_.PROCESS_INSTANCE_ID= @P1 and (nodeinstan0_.END_DATE is null) </inputbuf>

Here we can see that type of @P0 is NVARCHAR, however table NODE_INSTANCE_ID is VARCHAR

CREATE TABLE [dbo].[NODE_INSTANCE_LOG](
[id] [numeric](19, 0) IDENTITY(1,1) NOT NULL,
[BPMN_ELEMENT_ID] [varchar](255) NULL,
[END_DATE] [datetime] NULL,
[NODE_ID] [varchar](255) NOT NULL,
[NODE_INSTANCE_ID] [varchar](255) NOT NULL,
[NODE_NAME] [varchar](2000) NULL,
[PROCESS_INSTANCE_ID] [numeric](19, 0) NOT NULL,
[START_DATE] [datetime] NULL,
PRIMARY KEY CLUSTERED
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE NONCLUSTERED INDEX IX_NODE_INSTANCE_LOG_Index1
ON NODE_INSTANCE_LOG(NODE_INSTANCE_ID, PROCESS_INSTANCE_ID, END_DATE)
INCLUDE
( ID, BPMN_ELEMENT_ID, NODE_ID, NODE_NAME, START_DATE);


Check following link too http://blog.brianhartsock.com/2008/12/14/nvarchar-vs-varchar-in-sql-server-beware/. I hope it clarifies what I am trying to tell.
Post #1528046
Posted Monday, January 06, 2014 5:23 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:42 PM
Points: 14,840, Visits: 27,315
Oh, nice catch, I didn't see that.

Still look at the execution plan. It's a foundational start to everything you do when trying to tune performance problems on queries.

However, I'm still back to my original statement, a SELECT statement alone will absolutely NOT deadlock. So you must have other statements associated with the processes that are deadlocking. I would strongly suggest focusing there.


----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of: SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1528054
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse