某现场报一个SQL死锁,于是开启了1222跟踪:
dbcc traceon(1222,-1)
一段时间之后拷贝ERROR文件查找相关信息,比较有用的摘录出来如下:
语句一:
select study_iuid,station_aet,modality,accession_no,patient_fk,item_attrs,start_datetime from worklist w WITH(readpast), mwl_item m where w.TAG_STUDY_INSTANCE_UID=m.study_iuid and isread=‘1‘ and (TAG_SPS_STATUS is null or TAG_SPS_STATUS=‘SCHEDULED‘ or TAG_SPS_STATUS=‘Discontinued‘) and TAG_SPS_START_DATE between @P0 and @P1 and not exists (select 1 from mpps b where b.study_iuid=m.study_iuid)
语句二:
INSERT INTO mwl_item (created_time, updated_time, sps_id, start_datetime, station_aet, station_name, modality, perf_physician, perf_phys_fn_sx, perf_phys_gn_sx, perf_phys_i_name, perf_phys_p_name, req_proc_id, accession_no, study_iuid, item_attrs, sps_status, patient_fk) VALUES (@P0, @P1, @P2, @P3, @P4, @P5, @P6, @P7, @P8, @P9, @P10, @P11, @P12, @P13, @P14, @P15, @P16, @P17);
相关的死锁资源如下:
resource-list pagelock fileid=1 pageid=6996 dbid=8 objectname=Worklist.dbo.mwl_item id=lock19825c100 mode=IX associatedObjectId=72057594039697408 owner-list owner id=process984d048 mode=IX waiter-list waiter id=process60e9708 mode=S requestType=wait pagelock fileid=1 pageid=11086 dbid=8 objectname=Worklist.dbo.mwl_item id=lock1b087b100 mode=S associatedObjectId=72057594039697408 owner-list owner id=process60e9708 mode=S waiter-list waiter id=process984d048 mode=IX requestType=wait
可以明显的看到是select语句与insert语句产生了死锁,争用的资源分别6996和11086这两个page。
因此根据死锁的产生原理决定进行以下优化:
1.优化select语句使其尽快完成以减少死锁频率。
2.对select语句使用nolock选项以避免死锁问题。
3.通知开发优化相关代码的执行顺序来避免死锁问题。
最终优化了select语句,其他两条交给开发做修改。
时间: 2024-10-16 19:33:26