Deadlock - SQL Server 2012 TempDB - sql-server

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>

Related

Snowflake Pandas connection issue

I'm trying to insert a dataframe into a snowflake table using the pandas connector and am getting permission issues, but using the "normal" snowflake connector works fine.
import snowflake.connector as snow
from snowflake.connector.pandas_tools import *
cur = self.conn.cursor()
my_schema = "my_schema"
my_table = "my_table"
cur.execute(f"""INSERT INTO {my_schema}.{my_table}(load_date, some_id)
values (current_timestamp, 'xxx')""")
write_pandas(self.conn, daily_epc_df, table_name=my_table, schema=my_schema)
But I'm getting
File "/Users/abohr/virtualenv/peak38/lib/python3.8/site-packages/snowflake/connector/errors.py", line 85, in default_errorhandler
raise error_class(
snowflake.connector.errors.ProgrammingError: 001757 (42601): SQL compilation error:
Table '"my_schema"."my_table"' does not exist
the same connection can insert and then doesn't work on the same table.
I also tried
df.to_sql(..., method=pd_writer)
and get
pandas.io.sql.DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': not all arguments converted during string formatting
Why is it talking about sqlite_master if I'm trying to connect to Snowflake? Do the pandas functions require different connections?
my libs:
name = "snowflake-connector-python"
version = "2.3.3"
description = "Snowflake Connector for Python"
category = "main"
optional = false
python-versions = ">=3.5"
[[package]]
name = "pandas"
version = "1.0.5"
description = "Powerful data structures for data analysis, time series, and statistics"
category = "main"
optional = false
python-versions = ">=3.6.1"```
on Python 3.8
I found my issue - I added quote_identifiers=False to
write_pandas(self.conn, daily_epc_df, table_name=my_table, schema=my_schema, quote_identifiers=False)
and now it works.
But seems very wrong that the default behavior would break, I'm still suspicious.
Hmm I've done something superior I think to pandas ...
Tell me if you agree ...
CREATE OR REPLACE PROCEDURE DEMO_DB.PUBLIC.SNOWBALL(
db_name STRING,
schema_name STRING,
snowball_table STRING,
max_age_days FLOAT,
limit FLOAT
)
RETURNS VARIANT
LANGUAGE JAVASCRIPT
COMMENT = 'Collects table and column stats.'
EXECUTE AS OWNER
AS
$$
var validLimit = Math.max(LIMIT, 0); // prevent SQL syntax error caused by negative numbers
var sqlGenerateInserts = `
WITH snowball_tables AS (
SELECT CONCAT_WS('.', table_catalog, table_schema, table_name) AS full_table_name, *
FROM IDENTIFIER(?) -- <<DB_NAME>>.INFORMATION_SCHEMA.TABLES
),
snowball_columns AS (
SELECT CONCAT_WS('.', table_catalog, table_schema, table_name) AS full_table_name, *
FROM IDENTIFIER(?) -- <<DB_NAME>>.INFORMATION_SCHEMA.COLUMNS
),
snowball AS (
SELECT table_name, MAX(stats_run_date_time) AS stats_run_date_time
FROM IDENTIFIER(?) -- <<SNOWBALL_TABLE>> table
GROUP BY table_name
)
SELECT full_table_name, aprox_row_count,
CONCAT (
'INSERT INTO IDENTIFIER(''', ?, ''') ', -- SNOWBALL table
'(table_name,total_rows,table_last_altered,table_created,table_bytes,col_name,',
'col_data_type,col_hll,col_avg_length,col_null_cnt,col_min,col_max,col_top,col_mode,col_avg,stats_run_date_time)',
'SELECT ''', full_table_name, ''' AS table_name, ',
table_stats_sql,
', ARRAY_CONSTRUCT( ', col_name, ') AS col_name',
', ARRAY_CONSTRUCT( ', col_data_type, ') AS col_data_type',
', ARRAY_CONSTRUCT( ', col_hll, ') AS col_hll',
', ARRAY_CONSTRUCT( ', col_avg_length, ') AS col_avg_length',
', ARRAY_CONSTRUCT( ', col_null_cnt, ') AS col_null_cnt',
', ARRAY_CONSTRUCT( ', col_min, ') AS col_min',
', ARRAY_CONSTRUCT( ', col_max, ') AS col_max',
', ARRAY_CONSTRUCT( ', col_top, ') AS col_top',
', ARRAY_CONSTRUCT( ', col_MODE, ') AS col_MODE',
', ARRAY_CONSTRUCT( ', col_AVG, ') AS col_AVG',
', CURRENT_TIMESTAMP() AS stats_run_date_time ',
' FROM ', quoted_table_name
) AS insert_sql
FROM (
SELECT
tbl.full_table_name,
tbl.row_count AS aprox_row_count,
CONCAT ( '"', col.table_catalog, '"."', col.table_schema, '"."', col.table_name, '"' ) AS quoted_table_name,
CONCAT (
'COUNT(1) AS total_rows,''',
IFNULL( tbl.last_altered::VARCHAR, 'NULL'), ''' AS table_last_altered,''',
IFNULL( tbl.created::VARCHAR, 'NULL'), ''' AS table_created,',
IFNULL( tbl.bytes::VARCHAR, 'NULL'), ' AS table_bytes' ) AS table_stats_sql,
LISTAGG (
CONCAT ('''', col.full_table_name, '.', col.column_name, '''' ), ', '
) AS col_name,
LISTAGG ( CONCAT('''', col.data_type, '''' ), ', ' ) AS col_data_type,
LISTAGG ( CONCAT( ' HLL(', '"', col.column_name, '"',') ' ), ', ' ) AS col_hll,
LISTAGG ( CONCAT( ' AVG(ZEROIFNULL(LENGTH(', '"', col.column_name, '"','))) ' ), ', ' ) AS col_avg_length,
LISTAGG ( CONCAT( ' SUM( IFF( ', '"', col.column_name, '"',' IS NULL, 1, 0) ) ' ), ', ') AS col_null_cnt,
LISTAGG ( IFF ( col.data_type = 'NUMBER', CONCAT ( ' MODE(', '"', col.column_name, '"', ') ' ), 'NULL' ), ', ' ) AS col_MODE,
LISTAGG ( IFF ( col.data_type = 'NUMBER', CONCAT ( ' MIN(', '"', col.column_name, '"', ') ' ), 'NULL' ), ', ' ) AS col_min,
LISTAGG ( IFF ( col.data_type = 'NUMBER', CONCAT ( ' MAX(', '"', col.column_name, '"', ') ' ), 'NULL' ), ', ' ) AS col_max,
LISTAGG ( IFF ( col.data_type = 'NUMBER', CONCAT ( ' AVG(', '"', col.column_name,'"',') ' ), 'NULL' ), ', ' ) AS col_AVG,
LISTAGG ( CONCAT ( ' APPROX_TOP_K(', '"', col.column_name, '"', ', 100, 10000)' ), ', ' ) AS col_top
FROM snowball_tables tbl JOIN snowball_columns col ON col.full_table_name = tbl.full_table_name
LEFT OUTER JOIN snowball sb ON sb.table_name = tbl.full_table_name
WHERE (tbl.table_catalog, tbl.table_schema) = (?, ?)
AND ( sb.table_name IS NULL OR sb.stats_run_date_time < TIMESTAMPADD(DAY, - FLOOR(?), CURRENT_TIMESTAMP()) )
--AND tbl.row_count > 0 -- NB: also excludes views (table_type = 'VIEW')
GROUP BY tbl.full_table_name, aprox_row_count, quoted_table_name, table_stats_sql, stats_run_date_time
ORDER BY stats_run_date_time NULLS FIRST )
LIMIT ` + validLimit;
var tablesAnalysed = [];
var currentSql;
try {
currentSql = sqlGenerateInserts;
var generateInserts = snowflake.createStatement( {
sqlText: currentSql,
binds: [
`"${DB_NAME}".information_schema.tables`,
`"${DB_NAME}".information_schema.columns`,
SNOWBALL_TABLE, SNOWBALL_TABLE,
DB_NAME, SCHEMA_NAME, MAX_AGE_DAYS, LIMIT
]
} );
var insertStatements = generateInserts.execute();
// loop over generated INSERT statements and execute them
while (insertStatements.next()) {
var tableName = insertStatements.getColumnValue('FULL_TABLE_NAME');
currentSql = insertStatements.getColumnValue('INSERT_SQL');
var insertStatement = snowflake.createStatement( {
sqlText: currentSql,
binds: [ SNOWBALL_TABLE ]
} );
var insertResult = insertStatement.execute();
tablesAnalysed.push(tableName);
}
return { result: "SUCCESS", analysedTables: tablesAnalysed };
}
catch (err) {
return {
error: err,
analysedTables: tablesAnalysed,
sql: currentSql
};
}
$$;

Prepare WHERE Clause string for given values

I have the following details to prepare string as shown below in the expected result.
Given:
DECLARE #VValue VARCHAR(MAX) = 'John Dee|Mak Don'
DECLARE #VColumns VARCHAR(MAX) = 'FName|LName'
DECLARE #VCondition VARCHAR(500) = 'OR'
DECLARE #VPattern VARCHAR(MAX) = 'Start|Exact'
DECLARE #VCheck VARCHAR(MAX) = '1|0'
DECLARE #String VARCHAR(MAX) = ''
Expected result:
(
ISNULL(PATINDEX(''John%'', FName), ''0'') > 0 AND
ISNULL(PATINDEX(''Dee%'', FName), ''0'') > 0
)
OR
(
ISNULL(PATINDEX(''Mak'', LName), ''0'') +
ISNULL(PATINDEX(''Don'', LName), ''0'') > 0
)
My attempt:
SET #VCondition = '|'+#VCondition;
SET #String = REPLACE('('+REPLACE(STUFF((SELECT N') '+DS3.Item+' (' + NCHAR(10) +
STUFF((SELECT
CASE WHEN DS4.Item = 'Start' AND DS5.Item = 0
THEN
N' + ' +NCHAR(10)+
N'ISNULL(PATINDEX('''''+DSn.Item +'%'''''+ N',' + DSc.Item + N'),''''0'''')'
WHEN DS4.Item = 'Start' AND DS5.Item = 1
THEN
N' > 0 AND ' +NCHAR(10)+
N'ISNULL(PATINDEX('''''+DSn.Item +'%'''''+ N',' + DSc.Item + N'),''''0'''')'
ELSE
''
END
+
CASE WHEN DS4.Item = 'Exact' AND DS5.Item = 0
THEN
N' + ' +NCHAR(10)+
N'ISNULL(PATINDEX('''''+DSn.Item +''''''+ N',' + DSc.Item + N'),''''0'''')'
WHEN DS4.Item = 'Exact' AND DS5.Item = 1
THEN
N' > 0 AND ' +NCHAR(10)+
N'ISNULL(PATINDEX('''''+DSn.Item +''''''+ N',' + DSc.Item + N'),''''0'''')'
ELSE ''
END
FROM dbo.f_split(DS1.Item,' ') DSn
CROSS APPLY dbo.f_split(DS2.Item,',') DSc
ORDER BY DSc.id, DSn.id
FOR XML PATH(N'')),1,4,N'')
FROM dbo.udf_split (#VValue,'|') DS1
CROSS APPLY dbo.udf_split (#VColumns,'|') DS2
CROSS APPLY dbo.udf_split (#VCondition,'|') DS3
CROSS APPLY dbo.udf_split (#VPattern,'|') DS4
CROSS APPLY dbo.udf_split (#VCheck, '|') DS5
WHERE DS1.id = DS2.id AND DS2.ID = DS3.ID AND DS3.ID = DS4.ID AND DS4.ID = DS5.ID
ORDER BY DS1.id
FOR XML PATH(N'')),1,9,N''),'>','>') + N') )','&gt;','>');
PRINT(#String);
But I'm getting the following result:
( AND
ISNULL(PATINDEX(''John%'', FName), ''0'') > 0 AND
ISNULL(PATINDEX(''Dee%'', FName), ''0'')) OR (
ISNULL(PATINDEX(''Mak'', LName), ''0'') +
ISNULL(PATINDEX(''Don'', LName), ''0'')) )
After many attempt, following does the job done.
Query:
DECLARE #VValue VARCHAR(MAX) = 'John Dee|Mak Don'
DECLARE #VColumns VARCHAR(MAX) = 'FName|LName'
DECLARE #VCondition VARCHAR(500) = 'OR'
DECLARE #VPattern VARCHAR(MAX) = 'Start|Exact'
DECLARE #VCheck VARCHAR(MAX) = '1|0'
DECLARE #String VARCHAR(MAX) = ''
SET #VCondition = '|'+#VCondition;
SET #String = REPLACE('('+STUFF((SELECT N' > 0 ) '+DS3.Item+' (' + NCHAR(10) +
REPLACE(STUFF((SELECT
CASE WHEN DS4.Item = 'Start' AND DS5.Item = 0
THEN
N' + ' +NCHAR(10)+
N'ISNULL(PATINDEX('''''+DSn.Item +'%'''''+ N',' + DSc.Item + N'),''''0'''')'
WHEN DS4.Item = 'Start' AND DS5.Item = 1
THEN
N' > 0 AND ' +NCHAR(10)+
N'ISNULL(PATINDEX('''''+DSn.Item +'%'''''+ N',' + DSc.Item + N'),''''0'''')'
ELSE
''
END
+
CASE WHEN DS4.Item = 'Exact' AND DS5.Item = 0
THEN
N' + ' +NCHAR(10)+
N'ISNULL(PATINDEX('''''+DSn.Item +''''''+ N',' + DSc.Item + N'),''''0'''')'
WHEN DS4.Item = 'Exact' AND DS5.Item = 1
THEN
N' > 0 AND ' +NCHAR(10)+
N'ISNULL(PATINDEX('''''+DSn.Item +''''''+ N',' + DSc.Item + N'),''''0'''')'
ELSE ''
END
FROM dbo.f_split(DS1.Item,' ') DSn
CROSS APPLY dbo.f_split(DS2.Item,',') DSc
ORDER BY DSc.id, DSn.id
FOR XML PATH(N'')),1,11,N''),'>','>')
FROM dbo.udf_split (#VValue,'|') DS1
CROSS APPLY dbo.udf_split (#VColumns,'|') DS2
CROSS APPLY dbo.udf_split (#VCondition,'|') DS3
CROSS APPLY dbo.udf_split (#VPattern,'|') DS4
CROSS APPLY dbo.udf_split (#VCheck, '|') DS5
WHERE DS1.id = DS2.id AND DS2.ID = DS3.ID AND DS3.ID = DS4.ID AND DS4.ID = DS5.ID
ORDER BY DS1.id
FOR XML PATH(N'')),1,13,N''),'>','>') + N' > 0 ) ';
PRINT(#String);
Output:
(
ISNULL(PATINDEX(''John%'',FName),''0'') > 0 AND
ISNULL(PATINDEX(''Dee%'',FName),''0'') > 0
)
OR
(
ISNULL(PATINDEX(''Mak'',LName),''0'') +
ISNULL(PATINDEX(''Don'',LName),''0'') > 0
)

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

LINQ to SQL - Dead Lock on Insert

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).

sql server query concat string with '-'

In my SQL query, I'm trying to concatenate two strings in my select clause. Here's the expected results:
col A col B Result
null null &
null '' &
null XYZ XYC
'' null &
'' '' &
'' XYZ XYC
ABC null ABC
ABC '' ABC
ABC XYZ ABC-XYC
My challenge is this - how do I get the 'dash' to show up for the last scenario and not the others?
Here's my attempt:
DECLARE #ColA as varchar(10)
DECLARE #ColB as varchar(10)
set #ColA = null; set #ColB = null; select '&' as 'Expected', COALESCE(NULLIF(COALESCE(#ColA, '') + COALESCE(#ColB, ''), ''), '&') as 'Actual'
set #ColA = null; set #ColB = ''; select '&' as 'Expected', COALESCE(NULLIF(COALESCE(#ColA, '') + COALESCE(#ColB, ''), ''), '&') as 'Actual'
set #ColA = null; set #ColB = 'XYC'; select 'XYC' as 'Expected', COALESCE(NULLIF(COALESCE(#ColA, '') + COALESCE(#ColB, ''), ''), '&') as 'Actual'
set #ColA = ''; set #ColB = null; select '&' as 'Expected', COALESCE(NULLIF(COALESCE(#ColA, '') + COALESCE(#ColB, ''), ''), '&') as 'Actual'
set #ColA = ''; set #ColB = ''; select '&' as 'Expected', COALESCE(NULLIF(COALESCE(#ColA, '') + COALESCE(#ColB, ''), ''), '&') as 'Actual'
set #ColA = ''; set #ColB = 'XYC'; select 'XYC' as 'Expected', COALESCE(NULLIF(COALESCE(#ColA, '') + COALESCE(#ColB, ''), ''), '&') as 'Actual'
set #ColA = 'ABC';set #ColB = null; select 'ABC' as 'Expected', COALESCE(NULLIF(COALESCE(#ColA, '') + COALESCE(#ColB, ''), ''), '&') as 'Actual'
set #ColA = 'ABC';set #ColB = ''; select 'ABC' as 'Expected', COALESCE(NULLIF(COALESCE(#ColA, '') + COALESCE(#ColB, ''), ''), '&') as 'Actual'
set #ColA = 'ABC';set #ColB = 'XYC'; select 'ABC-XYC' as 'Expected', COALESCE(NULLIF(COALESCE(#ColA, '') + COALESCE(#ColB, ''), ''), '&') as 'Actual'
Do you think I have to do one giant case when? I have many columns like this, and that would make it unbearable to read.
Thanks!
UPDATE: if I use a case when, then my select looks like this, which seems to work, but is going to be a pain.
select
case when (#ColA is not null and #ColA <> '') and
(#ColB is not null and #ColB <> '')
then #ColA + '-' + #ColB
else COALESCE(NULLIF(COALESCE(#ColA, '') + COALESCE(#ColB, ''), ''), '&')
end
really hoping someone has some suggestions for improvement!
Could you use a searched case function like this?
Select Case When isnull(ColA, '') + isnull(ColB, '') == '' Then '&'
When isnull(ColA, '') <> '' and isnull(ColB, '') <> '' Then ColA + '-' + ColB
Else isnull(ColA, '') + isnull(ColB, '')
From Table1
I think this answer isn't a whole lot different than one of the other ones, but you might try this:
SELECT
CASE
WHEN NULLIF(ColA,'') <> '' AND NULLIF(ColB,'') <> ''
THEN ColA + '-' + ColB
WHEN NULLIF(ColA,'') <> '' OR NULLIF(ColB,'') <> ''
THEN COALESCE(ColA,ColB)
ELSE '&'
END

Resources