Hi All,
Need some help in finding out of what is the problem in the query and how can we make the query faster.
On one of the production environment, a DELETE statement was running over 50 mins and blocked more than 100 sessions.
I wasn't able to get the actual plan. However, including estimated plan in the attached zip file.
Below is the SQL statement.
DELETE FROM c_repos_applied_lock
WHERE rowid_lock IN (SELECT c_repos_applied_lock.rowid_lock
FROM c_repos_applied_lock,
c_repos_table
WHERE ( c_repos_applied_lock.job_type_str = 'A' )
AND ( c_repos_table.rowid_table =
c_repos_applied_lock.rowid_table )
AND ( (
Datediff (second, c_repos_applied_lock.create_date,
CURRENT_TIMESTAMP) ) > (
c_repos_table.batch_lock_wait_seconds * 3 ) ))
ran sp_whoisactive during the time of issue and collected some info.

spid 233
<--sql text -->
<?query --DELETE FROM C_REPOS_APPLIED_LOCK WHERE ROWID_LOCK IN (SELECT C_REPOS_APPLIED_LOCK.ROWID_LOCK FROM C_REPOS_APPLIED_LOCK, C_REPOS_TABLE WHERE (C_REPOS_APPLIED_LOCK.JOB_TYPE_STR = 'A') AND (C_REPOS_TABLE.ROWID_TABLE = C_REPOS_APPLIED_LOCK.ROWID_TABLE) AND ((DATEDIFF (second, C_REPOS_APPLIED_LOCK.CREATE_DATE, CURRENT_TIMESTAMP )) > (C_REPOS_TABLE.BATCH_LOCK_WAIT_SECONDS * 3)))--?>
<--lock info>
<Database name="CMX_ORS">
spid = 657 update (victim)
sql text :
<?query --
<--lock info>
<Database name="CMX_ORS">
Questions:
- Why DELETE is slow? It took more than 50 mins and as per app team confirmation, we had to KILL the spid eventually. was it is reads more data? usually, we don't see more than 10-15 rows in this table at any moment of time. I don't know why I see a lot of reads at that time frame. please refer above screenshot.
- From sp_wia output I was expecting 'X' locks on the table as it is a DELETE operation, but I see UPDATE locks. Any reason why update locks for delete operation?
- Any ways we can re-write of optimize this query. this query is there been for a while.. I don't know why they are doing co-related query/sub query instead of direct where condition. not sure why?
Is there away to decrease the execution time of this query? can it be done in smaller chunks for the locks to held for shorter duration? I don't know, I am just doing my brain dump of what to ask.
- The applied lock is kind of a table which used to hold some lock info. this is very dynamic table. A lots of inserts and deletes happen before acquiring and release this lock. Was the load on the table was more I don't know,
how to build a narrative at that time frame?
Please provide some guidance and reasoning on how can we make it faster. if there any specific questions to be asked to the app team, or if more information is needed pls let me know.
Thanks,
Sam


estimated plan

Estimated plan url :
https://www.brentozar.com/pastetheplan/?id=B1uqt4_P3
table structure and indexes
3-table structure and index definitions.txt
Thanks,
Sam