LINQ to SQL - Dead Lock on Insert - sql-server

I am facing deadlock issue on SQL Server -2014 standard edition. Two inserts are getting deadlocked over a clustered index.
It is an OLTP database and table is high insert and high read, table already has primary key with clustered index and having more that 13 Million records
The SQL Statements are given below
exec sp_executesql N'INSERT INTO [dbo].[PatientBilledItem]([PatientBillUID], [EventOccuredDttm], [ServiceName], [BillableItemUID], [Amount], [CUser], [CWhen], [MUser], [MWhen], [StatusFlag], [OwnerOrganisationUID], [PatientBillableItemUID], [Discount], [NetAmount], [Description], [Comments], [ItemMultiplier], [BSMDDUID], [ItemName], [DiscountAuthorizedBy], [ConsultantShare], [ServiceTax], [CareProviderUID], [QNUOMUID], [SpecialAmount], [IsRefunded], [ConsultantDiscount], [EducationCess], [HigherEducationCess], [RecordedByUID], [OriginalCreditBillUID], [PatientPackageItemUID], [BatchID], [VATPercentage], [InternalCost], [CalculateTaxOnMRP], [StoreUID], [BillPackageUID], [SplitDiscount], [RSLVLUID], [ParentUID], [PackageItemAmount], [RoundOff], [AuthNo], [ExpiryDttm], [BILGRDUID], [ApprovalCode], [ApprovedBy], [InternalBatchID])
VALUES (#p0, #p1, #p2, #p3, #p4, #p5, #p6, #p7, #p8, #p9, #p10, #p11, #p12, #p13, #p14, #p15, #p16, #p17, #p18, #p19, #p20, #p21, #p22, #p23, #p24, #p25, #p26, #p27, #p28, #p29, #p30, #p31, #p32, #p33, #p34, #p35, #p36, #p37, #p38, #p39, #p40, #p41, #p42, #p43, #p44, #p45, #p46, #p47, #p48)
SELECT [t0].[UID], [t0].[TIMESTAMP]
FROM [dbo].[PatientBilledItem] AS [t0]
WHERE [t0].[UID] = (SCOPE_IDENTITY())',N'#p0 bigint,#p1 datetime,#p2 nvarchar(4000),#p3 int,#p4 float,#p5 int,#p6 datetime,#p7 int,#p8 datetime,#p9 nvarchar(4000),#p10 int,#p11 bigint,#p12 float,#p13 float,#p14 nvarchar(4000),#p15 nvarchar(4000),#p16 float,#p17 int,#p18 nvarchar(4000),#p19 int,#p20 float,#p21 float,#p22 int,#p23 int,#p24 float,#p25 nvarchar(4000),#p26 float,#p27 float,#p28 float,#p29 int,#p30 bigint,#p31 bigint,#p32 nvarchar(4000),#p33 float,#p34 float,#p35 nvarchar(4000),#p36 int,#p37 int,#p38 float,#p39 int,#p40 bigint,#p41 float,#p42 float,#p43 nvarchar(4000),#p44 datetime,#p45 int,#p46 nvarchar(4000),#p47 int,#p48 nvarchar(4000)',#p0=3796014,#p1='2017-06-20 12:12:49.300',#p2=N'Consultancy',#p3=38328,#p4=500,#p5=4985,#p6='2017-06-20 12:13:12.603',#p7=4985,#p8='2017-06-20 12:13:12.603',#p9=N'A',#p10=4,#p11=10692556,#p12=NULL,#p13=500,#p14=NULL,#p15=NULL,#p16=1,#p17=1159,#p18=N'Dr. B K ',#p19=NULL,#p20=500,#p21=NULL,#p22=1395,#p23=NULL,#p24=NULL,#p25=NULL,#p26=NULL,#p27=NULL,#p28=NULL,#p29=NULL,#p30=NULL,#p31=NULL,#p32=NULL,#p33=NULL,#p34=NULL,#p35=NULL,#p36=NULL,#p37=NULL,#p38=NULL,#p39=NULL,#p40=NULL,#p41=NULL,#p42=NULL,#p43=NULL,#p44=NULL,#p45=NULL,#p46=NULL,#p47=NULL,#p48=NULL
exec sp_executesql N'INSERT INTO [dbo].[PatientBilledItem]([PatientBillUID], [EventOccuredDttm], [ServiceName], [BillableItemUID], [Amount], [CUser], [CWhen], [MUser], [MWhen], [StatusFlag], [OwnerOrganisationUID], [PatientBillableItemUID], [Discount], [NetAmount], [Description], [Comments], [ItemMultiplier], [BSMDDUID], [ItemName], [DiscountAuthorizedBy], [ConsultantShare], [ServiceTax], [CareProviderUID], [QNUOMUID], [SpecialAmount], [IsRefunded], [ConsultantDiscount], [EducationCess], [HigherEducationCess], [RecordedByUID], [OriginalCreditBillUID], [PatientPackageItemUID], [BatchID], [VATPercentage], [InternalCost], [CalculateTaxOnMRP], [StoreUID], [BillPackageUID], [SplitDiscount], [RSLVLUID], [ParentUID], [PackageItemAmount], [RoundOff], [AuthNo], [ExpiryDttm], [BILGRDUID], [ApprovalCode], [ApprovedBy], [InternalBatchID])
VALUES (#p0, #p1, #p2, #p3, #p4, #p5, #p6, #p7, #p8, #p9, #p10, #p11, #p12, #p13, #p14, #p15, #p16, #p17, #p18, #p19, #p20, #p21, #p22, #p23, #p24, #p25, #p26, #p27, #p28, #p29, #p30, #p31, #p32, #p33, #p34, #p35, #p36, #p37, #p38, #p39, #p40, #p41, #p42, #p43, #p44, #p45, #p46, #p47, #p48)
SELECT [t0].[UID], [t0].[TIMESTAMP]
FROM [dbo].[PatientBilledItem] AS [t0]
WHERE [t0].[UID] = (SCOPE_IDENTITY())',N'#p0 bigint,#p1 datetime,#p2 nvarchar(4000),#p3 int,#p4 float,#p5 int,#p6 datetime,#p7 int,#p8 datetime,#p9 nvarchar(4000),#p10 int,#p11 bigint,#p12 float,#p13 float,#p14 nvarchar(4000),#p15 nvarchar(4000),#p16 float,#p17 int,#p18 nvarchar(4000),#p19 int,#p20 float,#p21 float,#p22 int,#p23 int,#p24 float,#p25 nvarchar(4000),#p26 float,#p27 float,#p28 float,#p29 int,#p30 bigint,#p31 bigint,#p32 nvarchar(4000),#p33 float,#p34 float,#p35 nvarchar(4000),#p36 int,#p37 int,#p38 float,#p39 int,#p40 bigint,#p41 float,#p42 float,#p43 nvarchar(4000),#p44 datetime,#p45 int,#p46 nvarchar(4000),#p47 int,#p48 nvarchar(4000)',#p0=3796013,#p1='2017-06-20 12:13:10.030',#p2=N'Consultancy',#p3=83986,#p4=600,#p5=763,#p6='2017-06-20 12:13:12.023',#p7=763,#p8='2017-06-20 12:13:12.023',#p9=N'A',#p10=8,#p11=10692557,#p12=NULL,#p13=600,#p14=NULL,#p15=NULL,#p16=1,#p17=1159,#p18=N'Dr.L K S',#p19=NULL,#p20=600,#p21=NULL,#p22=1506,#p23=NULL,#p24=NULL,#p25=NULL,#p26=NULL,#p27=NULL,#p28=NULL,#p29=NULL,#p30=NULL,#p31=NULL,#p32=NULL,#p33=NULL,#p34=NULL,#p35=NULL,#p36=NULL,#p37=NULL,#p38=NULL,#p39=NULL,#p40=NULL,#p41=NULL,#p42=NULL,#p43=NULL,#p44=NULL,#p45=NULL,#p46=NULL,#p47=NULL,#p48=NULL
Yes the table is having trigger as well
The event XML is attached
<deadlock>
<victim-list>
<victimProcess id="processa19016ca8" />
</victim-list>
<process-list>
<process id="processa19016ca8" taskpriority="0" logused="3888" waitresource="KEY: 9:72057699966713856 (ffffffffffff)" waittime="1034" ownerId="1298235110" transactionname="user_transaction" lasttranstarted="2017-06-20T11:52:42.003" XDES="0x714fe0d90" lockMode="RangeI-N" schedulerid="1" kpid="7856" status="suspended" spid="66" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2017-06-20T11:52:42.830" lastbatchcompleted="2017-06-20T11:52:42.820" lastattention="1900-01-01T00:00:00.820" clientapp=".Net SqlClient Data Provider" hostname="WIN-2HO2RRV99BU" hostpid="2980" loginname="sa" isolationlevel="serializable (4)" xactid="1298235110" currentdb="9" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="1236" stmtend="3454" sqlhandle="0x0200000064f4502b431082b3ac55b13757583f58c2c98c7c0000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#p0 bigint,#p1 datetime,#p2 nvarchar(4000),#p3 int,#p4 float,#p5 int,#p6 datetime,#p7 int,#p8 datetime,#p9 nvarchar(4000),#p10 int,#p11 bigint,#p12 float,#p13 float,#p14 nvarchar(4000),#p15 nvarchar(4000),#p16 float,#p17 int,#p18 nvarchar(4000),#p19 int,#p20 float,#p21 float,#p22 int,#p23 int,#p24 float,#p25 nvarchar(4000),#p26 float,#p27 float,#p28 float,#p29 int,#p30 bigint,#p31 bigint,#p32 nvarchar(4000),#p33 float,#p34 float,#p35 nvarchar(4000),#p36 int,#p37 int,#p38 float,#p39 int,#p40 bigint,#p41 float,#p42 float,#p43 nvarchar(4000),#p44 datetime,#p45 int,#p46 nvarchar(4000),#p47 int,#p48 nvarchar(4000))INSERT INTO [dbo].[PatientBilledItem]([PatientBillUID], [EventOccuredDttm], [ServiceName], [BillableItemUID], [Amount], [CUser], [CWhen], [MUser], [MWhen], [StatusFlag], [OwnerOrganisationUID], [PatientBillableItemUID], [Discount], [NetAmount], [Description], [Comments], [ItemMultiplier], [BSMDDUID], [ItemName], [DiscountAuthorizedBy], [ConsultantShare], [ServiceTax], [CareProviderUID], [QNUOMUID], [Sp </inputbuf>
</process>
<process id="process18572db848" taskpriority="0" logused="3896" waitresource="KEY: 9:72057699966713856 (ffffffffffff)" waittime="1012" ownerId="1298235129" transactionname="user_transaction" lasttranstarted="2017-06-20T11:52:42.027" XDES="0x1ada4fed90" lockMode="RangeI-N" schedulerid="1" kpid="14928" status="suspended" spid="118" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2017-06-20T11:52:42.853" lastbatchcompleted="2017-06-20T11:52:42.847" lastattention="1900-01-01T00:00:00.847" clientapp=".Net SqlClient Data Provider" hostname="WIN-PGECRKPS51J" hostpid="3092" loginname="sa" isolationlevel="serializable (4)" xactid="1298235129" currentdb="9" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="1236" stmtend="3454" sqlhandle="0x0200000064f4502b431082b3ac55b13757583f58c2c98c7c0000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#p0 bigint,#p1 datetime,#p2 nvarchar(4000),#p3 int,#p4 float,#p5 int,#p6 datetime,#p7 int,#p8 datetime,#p9 nvarchar(4000),#p10 int,#p11 bigint,#p12 float,#p13 float,#p14 nvarchar(4000),#p15 nvarchar(4000),#p16 float,#p17 int,#p18 nvarchar(4000),#p19 int,#p20 float,#p21 float,#p22 int,#p23 int,#p24 float,#p25 nvarchar(4000),#p26 float,#p27 float,#p28 float,#p29 int,#p30 bigint,#p31 bigint,#p32 nvarchar(4000),#p33 float,#p34 float,#p35 nvarchar(4000),#p36 int,#p37 int,#p38 float,#p39 int,#p40 bigint,#p41 float,#p42 float,#p43 nvarchar(4000),#p44 datetime,#p45 int,#p46 nvarchar(4000),#p47 int,#p48 nvarchar(4000))INSERT INTO [dbo].[PatientBilledItem]([PatientBillUID], [EventOccuredDttm], [ServiceName], [BillableItemUID], [Amount], [CUser], [CWhen], [MUser], [MWhen], [StatusFlag], [OwnerOrganisationUID], [PatientBillableItemUID], [Discount], [NetAmount], [Description], [Comments], [ItemMultiplier], [BSMDDUID], [ItemName], [DiscountAuthorizedBy], [ConsultantShare], [ServiceTax], [CareProviderUID], [QNUOMUID], [Sp </inputbuf>
</process>
</process-list>
<resource-list>
<keylock hobtid="72057699966713856" dbid="9" objectname="HEALTHOBJECT.dbo.PatientBilledItem" indexname="IX_PatientBillableItem" id="lock28bc25a00" mode="RangeS-S" associatedObjectId="72057699966713856">
<owner-list>
<owner id="process18572db848" mode="RangeS-S" />
<owner id="process18572db848" mode="RangeI-N" requestType="convert" />
</owner-list>
<waiter-list>
<waiter id="processa19016ca8" mode="RangeI-N" requestType="convert" />
</waiter-list>
</keylock>
<keylock hobtid="72057699966713856" dbid="9" objectname="HEALTHOBJECT.dbo.PatientBilledItem" indexname="IX_PatientBillableItem" id="lock28bc25a00" mode="RangeS-S" associatedObjectId="72057699966713856">
<owner-list>
<owner id="processa19016ca8" mode="RangeS-S" />
<owner id="processa19016ca8" mode="RangeI-N" requestType="convert" />
</owner-list>
<waiter-list>
<waiter id="process18572db848" mode="RangeI-N" requestType="convert" />
</waiter-list>
</keylock>
</resource-list>
</deadlock>

INSERTs alone won't deadlock each other. If you call SELECT WHERE UID=SCOPE_IDENTITY() in the same transaction though, the server will have to take a SHARED lock on any indexes that contain UID. That's because you tried to read the value of SCOPE_IDENTITY() which will change if another row is added, leading to inconsistent data. If you use the REPEATABLE READ isolation level, you could end up with a deadlock.
The solution is very easy - don't use a separate SELECT. You can use the OUTPUT clause of INSERT to get all the new values, like identity, default values etc :
INSERT INTO [dbo].[PatientBilledItem]
([PatientBillUID], [EventOccuredDttm], [ServiceName], [BillableItemUID], [Amount], [CUser], [CWhen], [MUser], [MWhen], [StatusFlag], [OwnerOrganisationUID], [PatientBillableItemUID], [Discount], [NetAmount], [Description], [Comments], [ItemMultiplier], [BSMDDUID], [ItemName], [DiscountAuthorizedBy], [ConsultantShare], [ServiceTax], [CareProviderUID], [QNUOMUID], [SpecialAmount], [IsRefunded], [ConsultantDiscount], [EducationCess], [HigherEducationCess], [RecordedByUID], [OriginalCreditBillUID], [PatientPackageItemUID], [BatchID], [VATPercentage], [InternalCost], [CalculateTaxOnMRP], [StoreUID], [BillPackageUID], [SplitDiscount], [RSLVLUID], [ParentUID], [PackageItemAmount], [RoundOff], [AuthNo], [ExpiryDttm], [BILGRDUID], [ApprovalCode], [ApprovedBy], [InternalBatchID])
OUTPUT inserted.UID,inserted.Timestamp
VALUES ......
You can combine this with the SNAPSHOT isolation level to reduce blocking between transactions.

Based on provided info, first assumption is that these deadlocks are caused by one of following missing indexes
CREATE /*UNIQUE*/ NONCLUSTERED INDEX IUN_PatientBilledItem_UID_#_TIMESTAMP
ON dbo.PatientBilledItem (UID)
INCLUDE(TIMESTAMP)
or
CREATE /*UNIQUE*/ NONCLUSTERED INDEX IUN_PatientBilledItem_UID_TIMESTAMP
ON dbo.PatientBilledItem (UID, TIMESTAMP)
needed by following SELECT statements:
SELECT [t0].[UID], [t0].[TIMESTAMP]
FROM [dbo].[PatientBilledItem] AS [t0]
WHERE [t0].[UID] = (SCOPE_IDENTITY())
which generate RangeS-S locks (also because TX isolation level is SERIALIZABLE).
Note: Uncomment /*UNIQUE*/ keyword if current index has unique values or unique pair of values (compound index).

Related

Read XML on SQL Server : Select custom attribute

I'm trying to read an XML, it works pretty well on the whole except for the attribute named :
<custom-attribute attribute-id="loyaltyNumber">1234567890</custom-attribute>
which I am trying to get the value "1234567890"
Here is the test code:
DECLARE #XML XML = '<customers>
<customer customer-no="00000001">
<credentials>
<login>test#email.com</login>
</credentials>
<profile>
<preferred-locale>fr_BE</preferred-locale>
<custom-attributes>
<custom-attribute attribute-id="ServiceId">1</custom-attribute>
<custom-attribute attribute-id="loyaltyNumber">1234567890</custom-attribute>
</custom-attributes>
</profile>
<note/>
</customer>
<customer customer-no="00000002">
<credentials>
<login>test2#email.com</login>
</credentials>
<profile>
<preferred-locale>fr_FR</preferred-locale>
<custom-attributes>
<custom-attribute attribute-id="loyaltyNumber">1234567890</custom-attribute>
</custom-attributes>
</profile>
<note/>
</customer>
</customers>'
SELECT
CustomerNo = Events.value('#customer-no', 'int'),
--EventType = Events.value('#Type', 'varchar(20)'),
CustomerLogin =Events.value('(credentials/login)[1]', 'varchar(60)'),
CustomerLocal =Events.value('(profile/preferred-locale)[1]', 'varchar(60)'),
EventKind =Events.value('(profile/custom-attributes/custom-attribute)[2]', 'varchar(60)')
FROM
#XML.nodes('/customers/customer') AS XTbl(Events)
The current result is:
CustomerNo
CustomerLogin
CustomerLocal
EventKind
1
test#email.com
fr_BE
1234567890
2
test2#email.com
fr_FR
NULL
And it's logical since custom-attributes are optional, so you can't access them with the index.
So I'm looking for a way to access them by name: attribute-id="loyaltyNumber"
Thanks,
..filter the path to the attribute which has attribute-id = “loyaltyNumber”
EventKind =Events.value('(profile/custom-attributes/custom-attribute[#attribute-id="loyaltyNumber"])[1]', 'varchar(60)')
You can just add the attribute-id of the custom attribute to your query:
DECLARE #XML XML = '<customers>
<customer customer-no="00000001">
<credentials>
<login>test#email.com</login>
</credentials>
<profile>
<preferred-locale>fr_BE</preferred-locale>
<custom-attributes>
<custom-attribute attribute-id="ServiceId">1</custom-attribute>
<custom-attribute attribute-id="loyaltyNumber">1234567890</custom-attribute>
</custom-attributes>
</profile>
<note/>
</customer>
<customer customer-no="00000002">
<credentials>
<login>test2#email.com</login>
</credentials>
<profile>
<preferred-locale>fr_FR</preferred-locale>
<custom-attributes>
<custom-attribute attribute-id="loyaltyNumber">1234567777</custom-attribute>
</custom-attributes>
</profile>
<note/>
</customer>
</customers>'
SELECT
CustomerNo = Events.value('#customer-no', 'int'),
--EventType = Events.value('#Type', 'varchar(20)'),
CustomerLogin =Events.value('(credentials/login)[1]', 'varchar(60)'),
CustomerLocal =Events.value('(profile/preferred-locale)[1]', 'varchar(60)'),
EventKind =Events.value('(profile/custom-attributes/custom-attribute[#attribute-id = "loyaltyNumber"])[1]', 'varchar(60)')
FROM
#XML.nodes('/customers/customer') AS XTbl(Events)

Deadlock between two select

I am having a typical situation of a deadlock between two select statements.
I have a JMS listener running, handling two jms message at the same time.
They both run a select query and i end up with a key lock on the same table all the time. This is the details of the deadlock.
<deadlock-list>
<deadlock victim="process276e56fcca8">
<process-list>
<process id="process276e56fcca8" taskpriority="0" logused="764" waitresource="KEY: 7:72057594422558720 (8e392f9a7525)" waittime="4129" ownerId="24935297" transactionname="implicit_transaction" lasttranstarted="2017-09-02T10:01:18.853" XDES="0x276dd31f7b8" lockMode="S" schedulerid="5" kpid="25308" status="suspended" spid="65" sbid="0" ecid="0" priority="0" trancount="1" lastbatchstarted="2017-09-02T10:01:18.963" lastbatchcompleted="2017-09-02T10:01:18.963" lastattention="1900-01-01T00:00:00.963" clientapp="Microsoft JDBC Driver for SQL Server" hostname="GAETAN-PC" hostpid="0" loginname="fundhive" isolationlevel="read committed (2)" xactid="24935297" currentdb="7" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128058">
<executionStack>
<frame procname="adhoc" line="1" stmtend="274" sqlhandle="0x02000000ad097723a3df0ffd0ae8c2aae2d6e9938b5224fa0000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
select * ,61 as userId from getDocumentWorkflowMessages(20285) where 1=1 and vehicleGroupId = &apos;10372&apos; and investorLevelGroupId = &apos;13933&apos; </inputbuf>
</process>
<process id="process276f302fc28" taskpriority="0" logused="764" waitresource="KEY: 7:72057594422558720 (7d9127e683cc)" waittime="4188" ownerId="24935134" transactionname="implicit_transaction" lasttranstarted="2017-09-02T10:01:18.690" XDES="0x27671594728" lockMode="S" schedulerid="3" kpid="13788" status="suspended" spid="73" sbid="0" ecid="0" priority="0" trancount="1" lastbatchstarted="2017-09-02T10:01:18.783" lastbatchcompleted="2017-09-02T10:01:18.783" lastattention="1900-01-01T00:00:00.783" clientapp="Microsoft JDBC Driver for SQL Server" hostname="GAETAN-PC" hostpid="0" loginname="fundhive" isolationlevel="read committed (2)" xactid="24935134" currentdb="7" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128058">
<executionStack>
<frame procname="adhoc" line="1" stmtend="274" sqlhandle="0x0200000046471c35dd8038f6b5d5a8a05c3680d8fe3b66dc0000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
select * ,61 as userId from getDocumentWorkflowMessages(20285) where 1=1 and vehicleGroupId = &apos;10385&apos; and investorLevelGroupId = &apos;14481&apos; </inputbuf>
</process>
</process-list>
<resource-list>
<keylock hobtid="72057594422558720" dbid="7" objectname=" DocumentWorkflowStatuses" indexname="idx_DocumentWorkflowStatuses_isArchived" id="lock276c4fac900" mode="X" associatedObjectId="72057594422558720">
<owner-list>
<owner id="process276f302fc28" mode="X"/>
</owner-list>
<waiter-list>
<waiter id="process276e56fcca8" mode="S" requestType="wait"/>
</waiter-list>
</keylock>
<keylock hobtid="72057594422558720" dbid="7" objectname=" DocumentWorkflowStatuses" indexname="idx_DocumentWorkflowStatuses_isArchived" id="lock276e1728600" mode="X" associatedObjectId="72057594422558720">
<owner-list>
<owner id="process276e56fcca8" mode="X"/>
</owner-list>
<waiter-list>
<waiter id="process276f302fc28" mode="S" requestType="wait"/>
</waiter-list>
</keylock>
</resource-list>
</deadlock>
</deadlock-list>
I read lots of post but i am not sure how to approach the problem. I tried with and without an index on the table DocumentWorkflowStatus but it's the same things.
This is the getDocumentWorkflowMessages query :
CREATE FUNCTION [dbo].[getDocumentWorkflowMessages]
(
#projectId bigint
)
RETURNS TABLE
RETURN
SELECT t.*
,dbo.WorkflowStatuses.id AS workflowStatusId
,(
CASE
WHEN isnull(t.workflowScheme_fk, - 1) = - 1 -- IS NULL
THEN ''
ELSE dbo.WorkflowStatuses.NAME
END
) AS workflowStateName
,dbo.WorkflowStatuses.id AS workflowStateId
,CASE
WHEN isnull(t.workflowScheme_fk, '') = ''
THEN - 1
WHEN WorkflowStatusCategories.id IS NOT NULL
THEN WorkflowStatusCategories.id
ELSE isnull((
SELECT TOP 1 id
FROM dbo.getWorstWorkflowIdForStructure(t.projectLanguageId, t.vehicleGroupId, t.investorLevelGroupId, t.structureId)
), - 1)
END AS worstWorkflowCategoryId
from
dbo.getProjectDetailView(#projectId,1) t
LEFT JOIN
dbo.DocumentWorkflowStatuses docs ON docs.structure_fk = t.structureId
AND isnull(docs.versionLanguage_fk, - 1) = isnull(t.projectLanguageId, - 1)
AND isnull(docs.vehicleGroup_fk, 0) = isnull(t.vehicleGroupId, 0)
AND isnull(docs.vehicleInvestorGroup_fk, 0) = isnull(t.investorLevelGroupId, 0)
AND isnull(docs.country_fk, 0) = isnull(t.countryId,0)
LEFT JOIN
dbo.WorkflowStatuses
ON WorkflowStatuses.id = docs.workflowStatus_fk
LEFT JOIN
dbo.WorkflowStatusCategories
ON WorkflowStatusCategories.id = WorkflowStatuses.workflowStatusCategory_fk
GO
This is my index :
CREATE NONCLUSTERED INDEX idx_DocumentWorkflowStatuses_isArchived
ON [dbo].[DocumentWorkflowStatuses] ([structure_fk],[versionLanguage_fk],[vehicleGroup_fk],[vehicleInvestorGroup_fk],[country_fk])
INCLUDE ( [workflowStatus_fk])
GO

Parsing XML using T-SQL in SQL Server 2012

I've parsed XML using namespaces in SQL before but I'm having a real bear with this one. I'm trying to extract the ip addresses from the xml but having no luck. I'm thinking it has something to do with the xsd namespace, but can't seem to find the right combination of namespace declaration and node pathing.
Here's a sample of the xml:
<obj xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:vim25" versionId="6.0" xsi:type="ArrayOfGuestStackInfo">
<GuestStackInfo xsi:type="GuestStackInfo">
<dnsConfig>
<dhcp>false</dhcp>
<hostName>SXVCUP05</hostName>
<domainName>corp.dtcc.com</domainName>
<ipAddress>172.18.18.13</ipAddress>
<ipAddress>172.18.18.20</ipAddress>
<ipAddress>172.22.43.132</ipAddress>
<ipAddress>172.22.43.148</ipAddress>
<ipAddress>172.22.37.68</ipAddress>
<ipAddress>172.22.37.84</ipAddress>
<searchDomain>corp.dtcc.com</searchDomain>
<searchDomain>dtcc.com</searchDomain>
<searchDomain>backup.dtcc.com</searchDomain>
</dnsConfig>
<ipRouteConfig>
<ipRoute>
<network>0.0.0.0</network>
<prefixLength>0</prefixLength>
<gateway>
<ipAddress>172.28.224.1</ipAddress>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>172.18.8.133</network>
<prefixLength>32</prefixLength>
<gateway>
<ipAddress>172.28.224.10</ipAddress>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>172.22.8.14</network>
<prefixLength>32</prefixLength>
<gateway>
<ipAddress>172.28.224.10</ipAddress>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>172.28.224.0</network>
<prefixLength>24</prefixLength>
<gateway>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>172.28.224.112</network>
<prefixLength>32</prefixLength>
<gateway>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>172.28.224.255</network>
<prefixLength>32</prefixLength>
<gateway>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>224.0.0.0</network>
<prefixLength>4</prefixLength>
<gateway>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>255.255.255.255</network>
<prefixLength>32</prefixLength>
<gateway>
<device>0</device>
</gateway>
</ipRoute>
</ipRouteConfig>
</GuestStackInfo>
</obj>
Using the following code to parse the xml
WITH XMLNAMESPACES ('http://www.w3.org/2001/XMLSchema' as xsd)
SELECT a.b.value('ipAddress[1]', 'varchar(100)') AS ipaddress
FROM #sam_xml.nodes('/obj/GuestStackInfo/dnsConfig') a(b)
Thanks!
The values you are looking for are living in the default namespace xmlns. This is the only namespace you need to declare:
DECLARE #sam_xml XML=
N'<obj xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:vim25" versionId="6.0" xsi:type="ArrayOfGuestStackInfo">
<GuestStackInfo xsi:type="GuestStackInfo">
<dnsConfig>
<dhcp>false</dhcp>
<hostName>SXVCUP05</hostName>
<domainName>corp.dtcc.com</domainName>
<ipAddress>172.18.18.13</ipAddress>
<ipAddress>172.18.18.20</ipAddress>
<ipAddress>172.22.43.132</ipAddress>
<ipAddress>172.22.43.148</ipAddress>
<ipAddress>172.22.37.68</ipAddress>
<ipAddress>172.22.37.84</ipAddress>
<searchDomain>corp.dtcc.com</searchDomain>
<searchDomain>dtcc.com</searchDomain>
<searchDomain>backup.dtcc.com</searchDomain>
</dnsConfig>
<ipRouteConfig>
<ipRoute>
<network>0.0.0.0</network>
<prefixLength>0</prefixLength>
<gateway>
<ipAddress>172.28.224.1</ipAddress>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>172.18.8.133</network>
<prefixLength>32</prefixLength>
<gateway>
<ipAddress>172.28.224.10</ipAddress>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>172.22.8.14</network>
<prefixLength>32</prefixLength>
<gateway>
<ipAddress>172.28.224.10</ipAddress>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>172.28.224.0</network>
<prefixLength>24</prefixLength>
<gateway>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>172.28.224.112</network>
<prefixLength>32</prefixLength>
<gateway>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>172.28.224.255</network>
<prefixLength>32</prefixLength>
<gateway>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>224.0.0.0</network>
<prefixLength>4</prefixLength>
<gateway>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>255.255.255.255</network>
<prefixLength>32</prefixLength>
<gateway>
<device>0</device>
</gateway>
</ipRoute>
</ipRouteConfig>
</GuestStackInfo>
</obj>';
--the query will use .nodes() to dive one level deeper then your query in order to get all IP-addresses from within <dnsConfig>:
WITH XMLNAMESPACES(DEFAULT 'urn:vim25')
SELECT a.b.value('text()[1]', 'varchar(100)') as ipaddress
FROM #sam_xml.nodes('/obj/GuestStackInfo/dnsConfig/ipAddress') a(b)

Deadlock - SQL Server 2012 TempDB

We have an issue with deadlock in a SQL-server agent job. Attached is the xml_deadlock_report.
We have some stored procedures that executes in the job and we use temporary tables #.
Some example:
SET #columnsSQL = ' SELECT #columnArrayOut = COALESCE(#columnArrayOut + '','' , '' '' ) ' + CHAR(13)
+' + COLUMN_NAME ' + CHAR(13)
+'from tempdb.INFORMATION_SCHEMA.COLUMNS ' + CHAR(13)
+' where table_name = ' + CHAR(13)
+' object_name(' + CHAR(13)
+' object_id(''tempdb..'+ #globalTableName +'''),' + CHAR(13)
+' (select database_id from sys.databases where name = ''tempdb''))' + CHAR(13)
+'AND COLUMN_NAME <> ''SeqNbr''' + CHAR(13)
+'ORDER BY ORDINAL_POSITION '
SET #sqlScript = 'INSERT INTO '+ #globalTableName + CHAR(13)
+ 'SELECT * '+ CHAR(13)
+ 'FROM ('+ CHAR(13)
+ 'SELECT '+ #columnArray +', CAST(ROW_NUMBER() OVER(ORDER BY '+ #orderBy +') AS VARCHAR) AS SeqNbr FROM '+ #TableName +' WITH (NOLOCK)'+ CHAR(13)
+ 'UNION'+ CHAR(13)
+ 'SELECT '+ #columnArrayHeading +', ''0'''+ CHAR(13)
+ ') AS Temp'+ CHAR(13)
+ 'ORDER BY CAST(SeqNbr AS INT) ASC'+ CHAR(13)
+ 'SELECT '+ #columnArrayOut +', CASE convert(INT, SeqNbr) WHEN 0 THEN ''SeqNbr'' ELSE SeqNbr END
FROM '+ #globalTableName + CHAR(13)
+ 'ORDER BY CAST(SeqNbr AS INT) ASC'+ CHAR(13)
+ 'DROP TABLE '+ #globalTableName;
I am thinking it must be that we need to add nolock after some code, am I on the right track?
We have different customers on the same SQL-Server instance and it is SQL-Server 2012.
<deadlock>
<victim-list>
<victimProcess id="process6ff025c38" />
</victim-list>
<process-list>
<process id="process6ff025c38" taskpriority="0" logused="27564" waitresource="KEY: 2:281474978938880 (6f07e5ef564d)" waittime="3457" ownerId="2943291814" transactionname="ExportFilter" lasttranstarted="2015-12-28T07:15:10.007" XDES="0xdb84ad760" lockMode="S" schedulerid="3" kpid="1536" status="suspended" spid="72" sbid="0" ecid="0" priority="0" trancount="1" lastbatchstarted="2015-12-28T07:15:09.980" lastbatchcompleted="2015-12-28T07:15:09.970" lastattention="1900-01-01T00:00:00.970" clientapp="SQLAgent - TSQL JobStep (Job 0x93F327E5D307B843A95C7883AEE52C41 : Step 2)" isolationlevel="read committed (2)" xactid="2943291814" currentdb="9" lockTimeout="4294967295" clientoption1="538968096" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="78" sqlhandle="0x02000000bdb8ee370644f54bc076833c46eb8ce8ae6c21370000000000000000000000000000000000000000">
SELECT #columnArrayOut = COALESCE(#columnArrayOut + ',' , ' ' )
+ COLUMN_NAME
from tempdb.INFORMATION_SCHEMA.COLUMNS
where table_name =
object_name(
object_id('tempdb..##w_xxx004718_2328_MG_SO'),
(select database_id from sys.databases where name = 'tempdb'))
AND COLUMN_NAME <> 'SeqNbr'
ORDER BY ORDINAL_POSITION </frame>
<frame procname="mssqlsystemresource.sys.sp_executesql" line="1" stmtstart="-1" sqlhandle="0x0400ff7f427f99d9010000000000000000000000000000000000000000000000000000000000000000000000">
sp_executesql </frame>
isolationlevel="read committed (2)"
<frame procname="adhoc" line="1" stmtstart="78" sqlhandle="0x020000006df5ee06624475cdc854f45d0579dd554dddd61c0000000000000000000000000000000000000000">
SELECT #columnArrayOut = COALESCE(#columnArrayOut + ',' , ' ' )
+ COLUMN_NAME
from tempdb.INFORMATION_SCHEMA.COLUMNS
where table_name =
object_name(
object_id('tempdb..##w_xxx050820_2164_MG_LO'),
(select database_id from sys.databases where name = 'tempdb'))
AND COLUMN_NAME <> 'SeqNbr'
ORDER BY ORDINAL_POSITION </frame>
<frame procname="mssqlsystemresource.sys.sp_executesql" line="1" stmtstart="-1" sqlhandle="0x0400ff7f427f99d9010000000000000000000000000000000000000000000000000000000000000000000000">
sp_executesql </frame>
<resource-list>
<keylock hobtid="281474978938880" dbid="2" objectname="tempdb.sys.sysschobjs" indexname="clst" id="lock15895c380" mode="X" associatedObjectId="281474978938880">
<owner-list>
<owner id="processea1d7ecf8" mode="X" />
</owner-list>
<waiter-list>
<waiter id="process6ff025c38" mode="S" requestType="wait" />
</waiter-list>
</keylock>
<keylock hobtid="281474978938880" dbid="2" objectname="tempdb.sys.sysschobjs" indexname="clst" id="lock375a18480" mode="X" associatedObjectId="281474978938880">
<owner-list>
<owner id="process6ff025c38" mode="X" />
</owner-list>
<waiter-list>
<waiter id="processea1d7ecf8" mode="S" requestType="wait" />
</waiter-list>
</keylock>
</resource-list>
</deadlock>

Reduce SQL query execution time

I have a situation where our web application generates a Crystal Report based on the query result which takes almost 1 hr to execute.
I know this is crazy but, can anyone suggest any optimization or changes in the following giant query.
Thanks.
Note: I have shorten some of the similar queries.
CREATE procedure [dbo].[sp_ExportRevenueMenifest_27FEB]
(#vessel varchar(10), #voyage varchar(10),
#call varchar(10), #GLC varchar(10),
#Port varchar(10), #str varchar(200))
AS
create table #OCF (PortCode varchar(10),
PortName varchar(40),
BLNumber varchar(20), RS decimal(18,4),
RS1 decimal(18,4), Consignee varchar(500),
NoPackages varchar(50), Vessel varchar(10),
Vayage varchar(10), Call char(2),
Vessel_Name varchar(50),
Sailed_Date varchar(20), ExRate decimal(8,4)
)
insert into #OCF
SELECT a.PortofDischarge, Port_Master.Port_Name, a.BLNumber, SUM(B.PrepaidAmtUSD) AS PrepaidAmtUSD,SUM(B.COLLECTAmtUSD) AS COLLECTAmtUSD, a.Consignee, a.NoPackages, a.Vessel,
a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM BLMaster a INNER JOIN
BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND
a.Port = ExportVoyage_Master.Port AND a.Call = ExportVoyage_Master.Call INNER JOIN
Vessel_Master ON ExportVoyage_Master.Vessel_Code = Vessel_Master.Vessel_Code INNER JOIN
Port_Master ON a.PortofDischarge = Port_Master.Port_Code
WHERE (a.ImportExport = 2) AND (B.ConfigCode = 'OCF')
GROUP BY Port_Master.Port_Name, a.BLNumber, a.Consignee, a.NoPackages, a.Vessel, a.Voyage, a.Call, Vessel_Master.Vessel_Name,
ExportVoyage_Master.Sailed_Date, a.PortofDischarge, a.ExchangeRate, B.ConfigCode
create table #DDC (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #DDC
SELECT a.PortofDischarge, Port_Master.Port_Name, a.BLNumber, SUM(B.PrepaidAmtUSD) AS PrepaidAmtUSD,SUM(B.COLLECTAmtUSD) AS COLLECTAmtUSD, a.Consignee, a.NoPackages, a.Vessel,
a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM BLMaster a INNER JOIN
BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND
a.Port = ExportVoyage_Master.Port AND a.Call = ExportVoyage_Master.Call INNER JOIN
Vessel_Master ON ExportVoyage_Master.Vessel_Code = Vessel_Master.Vessel_Code INNER JOIN
Port_Master ON a.PortofDischarge = Port_Master.Port_Code
WHERE (a.ImportExport = 2) AND (B.ConfigCode = 'DDC')
GROUP BY Port_Master.Port_Name, a.BLNumber, a.Consignee, a.NoPackages, a.Vessel, a.Voyage, a.Call, Vessel_Master.Vessel_Name,
ExportVoyage_Master.Sailed_Date, a.PortofDischarge, a.ExchangeRate, B.ConfigCode
create table #FUM (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #FUM
SELECT a.PortofDischarge, ....., a.Vessel,
a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM BLMaster a INNER JOIN
BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
....INNER JOIN
Port_Master ON a.PortofDischarge = Port_Master.Port_Code
WHERE (a.ImportExport = 2) AND (B.ConfigCode = 'FUM')
GROUP BY Port_Master.Port_Name,.... B.ConfigCode
create table #PSS (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #PSS
SELECT a.PortofDischarge, .... a.ExchangeRate
FROM BLMaster a INNER JOIN
BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND
a.Port = ExportVoyage_Master.Port AND a.Call = ExportVoyage_Master.Call INNER JOIN
Vessel_Master ON ExportVoyage_Master.Vessel_Code = Vessel_Master.Vessel_Code INNER JOIN
Port_Master ON a.PortofDischarge = Port_Master.Port_Code
WHERE (a.ImportExport = 2) AND (B.ConfigCode = 'PSS')
GROUP BY Port_Master.Port_Name, ....., B.ConfigCode
create table #BAF (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #BAF
SELECT a.PortofDischarge, .... a.Vessel,
a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM BLMaster a INNER JOIN
BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
...... ON a.PortofDischarge = Port_Master.Port_Code
WHERE (a.ImportExport = 2) AND (B.ConfigCode = 'BAF')
GROUP BY Port_Master.Port_Name, ..... B.ConfigCode
create table #HD (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #HD
SELECT a.PortofDischarge, ..... a.Vessel,
a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM BLMaster a INNER JOIN
BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND
...... ON a.PortofDischarge = Port_Master.Port_Code
WHERE (a.ImportExport = 2) AND (B.ConfigCode = 'HD')
GROUP BY Port_Master.Port_Name, ..... B.ConfigCode
create table #WRS (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #WRS
SELECT a.PortofDischarge, Port_Master.Port_Name, a.BLNumber, SUM(B.PrepaidAmtUSD) AS PrepaidAmtUSD,SUM(B.COLLECTAmtUSD) AS COLLECTAmtUSD, a.Consignee, a.NoPackages, a.Vessel,
a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM BLMaster a INNER JOIN
BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND
a.Port = ExportVoyage_Master.Port AND a.Call = ExportVoyage_Master.Call INNER JOIN
Vessel_Master ON ExportVoyage_Master.Vessel_Code = Vessel_Master.Vessel_Code INNER JOIN
Port_Master ON a.PortofDischarge = Port_Master.Port_Code
WHERE (a.ImportExport = 2) AND (B.ConfigCode = 'WRS')
GROUP BY Port_Master.Port_Name, a.BLNumber, a.Consignee, a.NoPackages, a.Vessel, a.Voyage, a.Call, Vessel_Master.Vessel_Name,
ExportVoyage_Master.Sailed_Date, a.PortofDischarge, a.ExchangeRate, B.ConfigCode
create table #DIV (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #DIV
SELECT a.PortofDischarge, Port_Master.Port_Name, a.BLNumber, SUM(B.PrepaidAmtUSD) AS PrepaidAmtUSD,SUM(B.COLLECTAmtUSD) AS COLLECTAmtUSD, a.Consignee, a.NoPackages, a.Vessel,
a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM BLMaster a INNER JOIN
BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND
a.Port = ExportVoyage_Master.Port AND a.Call = ExportVoyage_Master.Call INNER JOIN
Vessel_Master ON ExportVoyage_Master.Vessel_Code = Vessel_Master.Vessel_Code INNER JOIN
Port_Master ON a.PortofDischarge = Port_Master.Port_Code
WHERE (a.ImportExport = 2) AND (B.ConfigCode = 'DIV')
GROUP BY Port_Master.Port_Name, a.BLNumber, a.Consignee, a.NoPackages, a.Vessel, a.Voyage, a.Call, Vessel_Master.Vessel_Name,
ExportVoyage_Master.Sailed_Date, a.PortofDischarge, a.ExchangeRate, B.ConfigCode
create table #PAN (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #PAN
SELECT a.PortofDischarge, Port_Master.Port_Name, a.BLNumber, SUM(B.PrepaidAmtUSD) AS PrepaidAmtUSD,SUM(B.COLLECTAmtUSD) AS COLLECTAmtUSD, a.Consignee, a.NoPackages, a.Vessel,
a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM BLMaster a INNER JOIN
BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND
a.Port = ExportVoyage_Master.Port AND a.Call = ExportVoyage_Master.Call INNER JOIN
Vessel_Master ON ExportVoyage_Master.Vessel_Code = Vessel_Master.Vessel_Code INNER JOIN
Port_Master ON a.PortofDischarge = Port_Master.Port_Code
WHERE (a.ImportExport = 2) AND (B.ConfigCode = 'PAN')
GROUP BY Port_Master.Port_Name, a.BLNumber, a.Consignee, a.NoPackages, a.Vessel, a.Voyage, a.Call, Vessel_Master.Vessel_Name,
ExportVoyage_Master.Sailed_Date, a.PortofDischarge, a.ExchangeRate, B.ConfigCode
create table #CAF (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #CAF
SELECT a.PortofDischarge, Port_Master.Port_Name, a.BLNumber, SUM(B.PrepaidAmtUSD) AS PrepaidAmtUSD,SUM(B.COLLECTAmtUSD) AS COLLECTAmtUSD, a.Consignee, a.NoPackages, a.Vessel,
a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM BLMaster a INNER JOIN
BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND
a.Port = ExportVoyage_Master.Port AND a.Call = ExportVoyage_Master.Call INNER JOIN
Vessel_Master ON ExportVoyage_Master.Vessel_Code = Vessel_Master.Vessel_Code INNER JOIN
Port_Master ON a.PortofDischarge = Port_Master.Port_Code
WHERE (a.ImportExport = 2) AND (B.ConfigCode = 'CAF')
GROUP BY Port_Master.Port_Name, a.BLNumber, a.Consignee, a.NoPackages, a.Vessel, a.Voyage, a.Call, Vessel_Master.Vessel_Name,
ExportVoyage_Master.Sailed_Date, a.PortofDischarge, a.ExchangeRate, B.ConfigCode
create table #CDR (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #CDR
SELECT a.PortofDischarge, Port_Master.Port_Name, a.BLNumber, SUM(B.PrepaidAmtUSD) AS PrepaidAmtUSD,SUM(B.COLLECTAmtUSD) AS COLLECTAmtUSD, a.Consignee, a.NoPackages, a.Vessel,
a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM BLMaster a INNER JOIN
..... ON a.PortofDischarge = Port_Master.Port_Code
WHERE (a.ImportExport = 2) AND (B.ConfigCode = 'CDR')
GROUP BY Port_Master.Port_Name, .... B.ConfigCode
create table #THC (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18),RS1 decimal(18),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #THC
SELECT a.PortofDischarge, ......, a.Vessel,
a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM BLMaster a INNER JOIN
BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
......
WHERE (a.ImportExport = 2) AND (B.ConfigCode = 'THC')
GROUP BY Port_Master.Port_Name, a.BLNumber, a.Consignee, a.NoPackages, a.Vessel, a.Voyage, a.Call, Vessel_Master.Vessel_Name,
ExportVoyage_Master.Sailed_Date, a.PortofDischarge, a.ExchangeRate, B.ConfigCode
create table #INL (PortCode varchar(10),....,ExRate decimal(8,4))
insert into #INL
SELECT a.PortofDischarge, Port_Master.Port_Name, a.BLNumber, SUM(B.PrepaidAmtRupee) AS PrepaidAmtRupee,SUM(B.CollectAmtRupee) AS collectAmtRupee, a.Consignee, a.NoPackages, a.Vessel,
a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM BLMaster a INNER JOIN
......
WHERE (a.ImportExport = 2) AND (B.ConfigCode = 'INL')
GROUP BY Port_Master.Port_Name, ......, B.ConfigCode
create table #ARB (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #ARB
SELECT a.PortofDischarge, Port_Master.Port_Name, a.BLNumber, SUM(B.PrepaidAmtRupee) AS PrepaidAmtRupee,SUM(B.CollectAmtRupee) AS collectAmtRupee, a.Consignee, a.NoPackages, a.Vessel,
a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM BLMaster a INNER JOIN
BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND
a.Port = ExportVoyage_Master.Port AND a.Call = ExportVoyage_Master.Call INNER JOIN
Vessel_Master ON ExportVoyage_Master.Vessel_Code = Vessel_Master.Vessel_Code INNER JOIN
Port_Master ON a.PortofDischarge = Port_Master.Port_Code
WHERE (a.ImportExport = 2) AND (B.ConfigCode = 'ARB')
GROUP BY Port_Master.Port_Name, a.BLNumber, a.Consignee, a.NoPackages, a.Vessel, a.Voyage, a.Call, Vessel_Master.Vessel_Name,
ExportVoyage_Master.Sailed_Date, a.PortofDischarge, a.ExchangeRate, B.ConfigCode
create table #DOC (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #DOC
SELECT a.PortofDischarge, .... a.Vessel,
a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM BLMaster a INNER JOIN
BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND
.... ON a.PortofDischarge = Port_Master.Port_Code
WHERE (a.ImportExport = 2) AND (B.ConfigCode = 'DOC')
GROUP BY Port_Master.Port_Name, ...., B.ConfigCode
create table #THCUSD (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #THCUSD
SELECT a.PortofDischarge, .... a.Vessel,
a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM BLMaster a INNER JOIN
BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND
.... ON a.PortofDischarge = Port_Master.Port_Code
WHERE (a.ImportExport = 2) AND (b.ConfigCode='THC')
GROUP BY Port_Master.Port_Name,...., a.ExchangeRate
create table #INLUSD (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #INLUSD
SELECT a.PortofDischarge, .... a.Vessel,
a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM BLMaster a INNER JOIN
BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND
.....ON a.PortofDischarge = Port_Master.Port_Code
WHERE (a.ImportExport = 2) AND (b.ConfigCode='INL')
GROUP BY Port_Master.Port_Name,.... a.ExchangeRate
create table #ARBUSD (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #ARBUSD
SELECT a.PortofDischarge, ....., a.Vessel,
a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM BLMaster a INNER JOIN
.....
Port_Master ON a.PortofDischarge = Port_Master.Port_Code
WHERE (a.ImportExport = 2) AND (b.ConfigCode='ARB')
GROUP BY Port_Master.Port_Name,.... a.ExchangeRate
create table #OTH (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #OTH
SELECT a.PortofDischarge, Port_Master.Port_Name, a.BLNumber, SUM(B.PrepaidAmtUSD) AS PrepaidAmtUSD,SUM(B.COLLECTAmtUSD) AS COLLECTAmtUSD, a.Consignee, a.NoPackages, a.Vessel,
a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM BLMaster a INNER JOIN
BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND
a.Port = ExportVoyage_Master.Port AND a.Call = ExportVoyage_Master.Call INNER JOIN
Vessel_Master ON ExportVoyage_Master.Vessel_Code = Vessel_Master.Vessel_Code INNER JOIN
Port_Master ON a.PortofDischarge = Port_Master.Port_Code
WHERE (a.ImportExport = 2) AND b.ConfigCode NOT IN('OCF','FUM','BAF','YAS','CAF','HD','DIV','PSS','CAF','PSS','PAN','WRS','CDR','THC','INL','DDC','ARB','DOC','STX')
GROUP BY Port_Master.Port_Name, a.BLNumber, a.Consignee, a.NoPackages, a.Vessel, a.Voyage, a.Call, Vessel_Master.Vessel_Name,
ExportVoyage_Master.Sailed_Date, a.PortofDischarge, a.ExchangeRate
DECLARE #Query varchar(8000)
SET #Query = ''
SELECT #Query ='SELECT Port_Master.Port_Name,Vessel_Master.Vessel_Name, Vessel_Master.Vessel_Code, ExportVoyage_Master.Voyage_Code, ExportVoyage_Master.Sailed_Date,
Vessel_Master.Nationality, ExportVoyage_Master.Master, a.Portofloading,a.PortofDischarge,a.FinalDestination, ExportVoyage_Master.Call,ExportVoyage_Master.Berth_Date,a.BLNumber,a.Shipper, a.Consignee,a.port,
a.MarksNo, a.GoodsDescription, a.NoPackages, BL_ClubbedContainer.ContainerNo,
BL_ClubbedContainer.TypeSize, A.ExchangeRate, a.BrokerCode,
isnull(OC.RS,0)OCF,isnull(FU.RS,0)FUM,isnull(BA.RS,0)BAF,isnull(CA.RS,0)CAF,isnull(HD.RS,0)HD,isnull(DI.RS,0)DIV,isnull(PS.RS,0)PSS,isnull(PA.RS,0)PAN,isnull(WR.RS,0)WRS,isnull(CD.RS,0)CDR,isnull(TH.RS,0)THC,isnull(DD.RS,0)DDC,isnull(INL.RS,0)INL,isnull(AR.RS,0)ARB,isnull(DO.RS,0)DOC,isnull(OT.RS,0)OTH,isnull(THUS.RS,0)THCUSD,isnull(INUS.RS,0)INLUSD,isnull(ARUS.RS,0)ARBUSD,isnull(OC.RS1,0)OCF1,isnull(FU.RS1,0)FUM1,isnull(BA.RS1,0)BAF1,isnull(CA.RS1,0)CAF1,isnull(HD.RS1,0)HD1,isnull(DI.RS1,0)DIV1,isnull(PS.RS1,0) PSS1,isnull(PA.RS1,0) PAN1,isnull(WR.RS1,0) WRS1,isnull(CD.RS1,0)CDR1,isnull(TH.RS1,0)THC1,isnull(DD.RS1,0)DDC1,isnull(INL.RS1,0)INL1,isnull(AR.RS1,0)ARB1,isnull(DO.RS1,0) DOC1,isnull(OT.RS1,0)OTH1,isnull(THUS.RS1,0)THCUSD1,isnull(INUS.RS1,0)INLUSD1,isnull(ARUS.RS1,0)ARBUSD1
FROM BLMaster a LEFT OUTER JOIN
BL_ClubbedContainer ON a.BLNumber = BL_ClubbedContainer.BLNumber INNER JOIN
ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND
a.Port = ExportVoyage_Master.Port AND a.Call = ExportVoyage_Master.Call INNER JOIN
Vessel_Master ON ExportVoyage_Master.Vessel_Code = Vessel_Master.Vessel_Code inner join port_master on a.FinalDestination=Port_Master.Port_Code
left outer join #OCF OC on OC.blnumber=a.blnumber
left outer join #DDC DD on DD.blnumber=a.blnumber
left outer join #FUM FU on FU.blnumber=a.blnumber
left outer join #BAF BA on BA.blnumber=a.blnumber
left outer join #HD HD on HD.blnumber=a.blnumber
left outer join #DIV DI on DI.blnumber=a.blnumber
left outer join #WRS WR on WR.blnumber=a.blnumber
left outer join #CAF CA on CA.blnumber=a.blnumber
left outer join #PSS PS on PS.blnumber=a.blnumber
left outer join #PAN PA on PA.blnumber=a.blnumber
left outer join #CDR CD on CD.blnumber=a.blnumber
left outer join #THC TH on TH.blnumber=a.blnumber
left outer join #INL INL on INL.blnumber=a.blnumber
left outer join #ARB AR on AR.blnumber=a.blnumber
left outer join #DOC DO on DO.blnumber=a.blnumber
left outer join #OTH OT on OT.blnumber=a.blnumber
left outer join #THCUSD THUS on THUS.blnumber=a.blnumber
left outer join #INLUSD INUS on INUS.blnumber=a.blnumber
left outer join #ARBUSD ARUS on ARUS.blnumber=a.blnumber
WHERE (a.ImportExport = 2) '+ #str + ' order by a.Portofdischarge,a.blnumber asc'
EXEC (#Query)
From my experience
1) Try to put your parameter into local variable before use.
create procedure [dbo].[usp_test](#Id varchar(20))
as
begin
select * from Test
where Id = #Id
end
to
alter procedure [dbo].[usp_test](#Id varchar(20))
as
begin
declare #local_id varchar(20) = #Id
select * from Test
where Id = #local_id
end
2) Use recompile hint. This will get new query plan which is more suitable for the query based on parameter value.
exec dbo.usp_test 1 with recompile
Ref: https://stackoverflow.com/a/30092833/1554116
Thank you guys for your efforts and time looking into my issue.
The query used to pull all the records from BLMaster without filtering with the parameters passed to the stored procedure.
P.S. Thanks to my vendor.
WHERE (a.ImportExport = 2) AND (B.ConfigCode = 'OCF')
/* Added below two lines to all WHERE clause solved my issue */
AND a.Vessel = #vessel
AND a.Voyage = #voyage
Table BLMaster has around 12lakhs records due to this it was taking so long time.

Resources