{"id":374,"date":"2014-11-15T12:48:23","date_gmt":"2014-11-15T12:48:23","guid":{"rendered":"ad972f10e0800b49d76fed33a21f6698"},"modified":"2014-11-15T12:48:23","modified_gmt":"2014-11-15T12:48:23","slug":"","status":"publish","type":"post","link":"https:\/\/www.xiaobo.li\/notes\/archives\/374","title":{"rendered":"SQL Server\u6b7b\u9501"},"content":{"rendered":"<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\u5176\u5b9e\u6240\u6709\u7684\u6b7b\u9501\u6700\u6df1\u5c42\u7684\u539f\u56e0\u5c31\u662f\u4e00\u4e2a\uff1a\u8d44\u6e90\u7ade\u4e89<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\"><strong>\u8868\u73b0\u4e00\uff1a<\/strong><\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\u3000\u3000\u4e00\u4e2a\u7528\u6237A \u8bbf\u95ee\u8868A(\u9501\u4f4f\u4e86\u8868A)\uff0c\u7136\u540e\u53c8\u8bbf\u95ee\u8868B\uff0c\u53e6\u4e00\u4e2a\u7528\u6237B \u8bbf\u95ee\u8868B(\u9501\u4f4f\u4e86\u8868B)\uff0c\u7136\u540e\u4f01\u56fe\u8bbf\u95ee\u8868A\uff0c\u8fd9\u65f6\u7528\u6237A\u7531\u4e8e\u7528\u6237B\u5df2\u7ecf\u9501\u4f4f\u8868B\uff0c\u5b83\u5fc5\u987b\u7b49\u5f85\u7528\u6237B\u91ca\u653e\u8868B\uff0c\u624d\u80fd\u7ee7\u7eed\uff0c\u597d\u4e86\u4ed6\u8001\u4eba\u5bb6\u5c31\u53ea\u597d\u8001\u8001\u5b9e\u5b9e\u5728\u8fd9\u7b49\u4e86\uff0c\u540c\u6837\u7528\u6237B\u8981\u7b49\u7528\u6237A\u91ca\u653e\u8868A\u624d\u80fd\u7ee7\u7eed\u8fd9\u5c31\u6b7b\u9501\u4e86\u3002<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\u3000\u3000\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\u3000\u3000\u8fd9\u79cd\u6b7b\u9501\u662f\u7531\u4e8e\u4f60\u7684\u7a0b\u5e8f\u7684BUG\u4ea7\u751f\u7684\uff0c\u9664\u4e86\u8c03\u6574\u4f60\u7684\u7a0b\u5e8f\u7684\u903b\u8f91\u522b\u65e0\u4ed6\u6cd5<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\u3000\u3000\u4ed4\u7ec6\u5206\u6790\u4f60\u7a0b\u5e8f\u7684\u903b\u8f91\uff1a<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\u3000\u30001\uff1a\u5c3d\u91cf\u907f\u514d\u540c\u65f6\u9501\u5b9a\u4e24\u4e2a\u8d44\u6e90<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\u3000\u30002: \u5fc5\u987b\u540c\u65f6\u9501\u5b9a\u4e24\u4e2a\u8d44\u6e90\u65f6\uff0c\u8981\u4fdd\u8bc1\u5728\u4efb\u4f55\u65f6\u523b\u90fd\u5e94\u8be5\u6309\u7167\u76f8\u540c\u7684\u987a\u5e8f\u6765\u9501\u5b9a\u8d44\u6e90.<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\"><strong>\u8868\u73b0\u4e8c\uff1a<\/strong><\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\u3000\u3000\u7528\u6237A\u8bfb\u4e00\u6761\u7eaa\u5f55\uff0c\u7136\u540e\u4fee\u6539\u8be5\u6761\u7eaa\u5f55\u3002\u8fd9\u662f\u7528\u6237B\u4fee\u6539\u8be5\u6761\u7eaa\u5f55\uff0c\u8fd9\u91cc\u7528\u6237A\u7684\u4e8b\u52a1\u91cc\u9501\u7684\u6027\u8d28\u7531\u5171\u4eab\u9501\u4f01\u56fe\u4e0a\u5347\u5230\u72ec\u5360\u9501(for update),\u800c\u7528\u6237B\u91cc\u7684\u72ec\u5360\u9501\u7531\u4e8eA\u6709\u5171\u4eab\u9501\u5b58\u5728\u6240\u4ee5\u5fc5\u987b\u7b49A\u91ca\u653e\u6389\u5171\u4eab\u9501\uff0c\u800cA\u7531\u4e8eB\u7684\u72ec\u5360\u9501\u800c\u65e0\u6cd5\u4e0a\u5347\u7684\u72ec\u5360\u9501\u4e5f\u5c31\u4e0d\u53ef\u80fd\u91ca\u653e\u5171\u4eab\u9501\uff0c\u4e8e\u662f\u51fa\u73b0\u4e86\u6b7b\u9501\u3002<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\u3000\u3000\u8fd9\u79cd\u6b7b\u9501\u6bd4\u8f83\u9690\u853d\uff0c\u4f46\u5176\u5b9e\u5728\u7a0d\u5927\u70b9\u7684\u9879\u76ee\u4e2d\u7ecf\u5e38\u53d1\u751f\u3002<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\u3000\u3000\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\u3000\u3000\u8ba9\u7528\u6237A\u7684\u4e8b\u52a1\uff08\u5373\u5148\u8bfb\u540e\u5199\u7c7b\u578b\u7684\u64cd\u4f5c),\u5728select \u65f6\u5c31\u662f\u7528Update lock<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\u3000\u3000\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select * from table1 with(updlock) where ....<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\n<div style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\"><strong>--------------------------------------------------------------------------------<\/strong><\/div>\n<p><strong style=\"font-size:14px;font-family:Arial;font-variant:normal;white-space:normal;text-transform:none;word-spacing:0px;color:#333333;font-style:normal;text-align:left;letter-spacing:normal;line-height:26px;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\"> <\/p>\n<p>\u63a5\u4e0a\u9762\u6587\u7ae0\uff0c\u7ee7\u7eed\u63a2\u8ba8\u6570\u636e\u5e93\u6b7b\u9501\u95ee\u9898&nbsp;<\/p>\n<p><\/strong> <\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\u6b7b\u9501\uff0c\u7b80\u800c\u8a00\u4e4b\uff0c\u4e24\u4e2a\u6216\u8005\u591a\u4e2atrans\uff0c\u540c\u65f6\u8bf7\u6c42\u5bf9\u65b9\u6b63\u5728\u8bf7\u6c42\u7684\u67d0\u4e2a\u5bf9\u8c61\uff0c\u5bfc\u81f4\u53cc\u65b9\u4e92\u76f8\u7b49\u5f85\u3002\u7b80\u5355\u7684\u4f8b\u5b50\u5982\u4e0b\uff1a<br \/>\n&nbsp;&nbsp; trans1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; trans2<br \/>\n&nbsp;&nbsp; ------------------------------------------------------------------------------------------------<br \/>\n&nbsp;&nbsp; 1.IDBConnection.BeginTransaction&nbsp;&nbsp;&nbsp; 1.IDBConnection.BeginTransaction<br \/>\n&nbsp;&nbsp; 2.update table A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; 2.update table B<br \/>\n&nbsp;&nbsp;<span class=\"Apple-converted-space\">&nbsp;<\/span><span style=\"color:#ff0000;\">3.update table B<\/span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color:#ff0000;\">3.update table A<br \/>\n<\/span>&nbsp;&nbsp; 4.IDBConnection.Commit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;4.IDBConnection.Commit<span class=\"Apple-converted-space\">&nbsp;<\/span><br \/>\n&nbsp;&nbsp; \u90a3\u4e48\uff0c\u5f88\u5bb9\u6613\u770b\u5230\uff0c\u5982\u679ctrans1\u548ctrans2\uff0c\u5206\u522b\u5230\u8fbe\u4e86step3\uff0c\u90a3\u4e48trans1\u4f1a\u8bf7\u6c42\u5bf9\u4e8eB\u7684X\u9501\uff0ctrans2\u4f1a\u8bf7\u6c42\u5bf9\u4e8eA\u7684X\u9501\uff0c\u800c\u4e8c\u8005\u7684\u9501\u5728step2\u4e0a\u5df2\u7ecf\u88ab\u5bf9\u65b9\u5206\u522b\u6301\u6709\u4e86\u3002\u7531\u4e8e\u5f97\u4e0d\u5230\u9501\uff0c\u540e\u9762\u7684Commit\u65e0\u6cd5\u6267\u884c\uff0c\u8fd9\u6837\u53cc\u65b9\u5f00\u59cb\u6b7b\u9501\u3002<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; \u597d\uff0c\u6211\u4eec\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u6765\u89e3\u91ca\u4e00\u4e0b\uff0c\u5e94\u8be5\u5982\u4f55\u89e3\u51b3\u6b7b\u9501\u95ee\u9898\u3002<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\n&nbsp;&nbsp; -- Batch #1<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\n&nbsp;&nbsp; CREATE DATABASE deadlocktest<br \/>\n&nbsp;&nbsp; GO<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\n&nbsp;&nbsp; USE deadlocktest<br \/>\n&nbsp;&nbsp; SET NOCOUNT ON<br \/>\n&nbsp;&nbsp; DBCC TRACEON (1222, -1)<br \/>\n&nbsp;&nbsp; -- \u5728SQL2005\u4e2d\uff0c\u589e\u52a0\u4e86\u4e00\u4e2a\u65b0\u7684dbcc\u53c2\u6570\uff0c\u5c31\u662f1222\uff0c\u539f\u6765\u57282000\u4e0b\uff0c\u6211\u4eec\u77e5\u9053\uff0c\u53ef\u4ee5\u6267\u884cdbcc&nbsp;&nbsp;&nbsp;<span class=\"Apple-converted-space\">&nbsp;<\/span><br \/>\n&nbsp;&nbsp; -- traceon(1204,3605,-1)\u770b\u5230\u6240\u6709\u7684\u6b7b\u9501\u4fe1\u606f\u3002SqlServer 2005\u4e2d\uff0c\u5bf9\u4e8e1204\u8fdb\u884c\u4e86\u589e\u5f3a\uff0c\u8fd9\u5c31\u662f1222\u3002<br \/>\n&nbsp;&nbsp; GO&nbsp;&nbsp;<span class=\"Apple-converted-space\">&nbsp;<\/span><br \/>\n&nbsp;&nbsp;<span class=\"Apple-converted-space\">&nbsp;<\/span><br \/>\n&nbsp;&nbsp; IF OBJECT_ID ('t1') IS NOT NULL DROP TABLE t1<br \/>\n&nbsp;&nbsp; IF OBJECT_ID ('p1') IS NOT NULL DROP PROC p1<br \/>\n&nbsp;&nbsp; IF OBJECT_ID ('p2') IS NOT NULL DROP PROC p2<br \/>\n&nbsp;&nbsp; GO<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; CREATE TABLE t1 (c1 int, c2 int, c3 int, c4 char(5000))<br \/>\n&nbsp;&nbsp; GO<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; DECLARE @x int<br \/>\n&nbsp;&nbsp; SET @x = 1<br \/>\n&nbsp;&nbsp; WHILE (@x &lt;= 1000) BEGIN<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; INSERT INTO t1 VALUES (@x*2, @x*2, @x*2, @x*2)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SET @x = @x + 1<br \/>\n&nbsp;&nbsp; END<br \/>\n&nbsp;&nbsp; GO<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; CREATE CLUSTERED INDEX cidx ON t1 (c1)<br \/>\n&nbsp;&nbsp; CREATE NONCLUSTERED INDEX idx1 ON t1 (c2)<br \/>\n&nbsp;&nbsp; GO<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; CREATE PROC p1 @p1 int AS SELECT c2, c3 FROM t1 WHERE c2 BETWEEN @p1 AND @p1+1<br \/>\n&nbsp;&nbsp; GO<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; CREATE PROC p2 @p1 int AS<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UPDATE t1 SET c2 = c2+1 WHERE c1 = @p1<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UPDATE t1 SET c2 = c2-1 WHERE c1 = @p1<br \/>\n&nbsp;&nbsp; GO<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; \u4e0a\u8ff0sql\u521b\u5efa\u4e00\u4e2adeadlock\u7684\u793a\u8303\u6570\u636e\u5e93\uff0c\u63d2\u5165\u4e861000\u6761\u6570\u636e\uff0c\u5e76\u5728\u8868t1\u4e0a\u5efa\u7acb\u4e86c1\u5217\u7684\u805a\u96c6\u7d22\u5f15\uff0c\u548cc2\u5217\u7684\u975e\u805a\u96c6\u7d22\u5f15\u3002\u53e6\u5916\u521b\u5efa\u4e86\u4e24\u4e2asp\uff0c\u5206\u522b\u662f\u4ecet1\u4e2dselect\u6570\u636e\u548cupdate\u6570\u636e\u3002<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; \u597d\uff0c\u6253\u5f00\u4e00\u4e2a\u65b0\u7684\u67e5\u8be2\u7a97\u53e3\uff0c\u6211\u4eec\u5f00\u59cb\u6267\u884c\u4e0b\u9762\u7684query\uff1a<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\n&nbsp;&nbsp; -- Batch #2<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; USE deadlocktest<br \/>\n&nbsp;&nbsp; SET NOCOUNT ON<br \/>\n&nbsp;&nbsp; WHILE (1=1) EXEC p2 4<br \/>\n&nbsp;&nbsp; GO<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; \u5f00\u59cb\u6267\u884c\u540e\uff0c\u7136\u540e\u6211\u4eec\u6253\u5f00\u7b2c\u4e09\u4e2a\u67e5\u8be2\u7a97\u53e3\uff0c\u6267\u884c\u4e0b\u9762\u7684query\uff1a<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\n&nbsp;&nbsp; -- Batch #3<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; USE deadlocktest<br \/>\n&nbsp;&nbsp; SET NOCOUNT ON<br \/>\n&nbsp;&nbsp; CREATE TABLE #t1 (c2 int, c3 int)<br \/>\n&nbsp;&nbsp; GO<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; WHILE (1=1) BEGIN<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; INSERT INTO #t1 EXEC p1 4<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TRUNCATE TABLE #t1<br \/>\n&nbsp;&nbsp; END<br \/>\n&nbsp;&nbsp; GO<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; \u5f00\u59cb\u6267\u884c\uff0c\u54c8\u54c8\uff0c\u5f88\u5feb\uff0c\u6211\u4eec\u770b\u5230\u4e86\u8fd9\u6837\u7684\u9519\u8bef\u4fe1\u606f\uff1a<br \/>\n&nbsp;&nbsp; Msg 1205, Level 13, State 51, Procedure p1, Line 4<br \/>\n&nbsp;&nbsp; Transaction (Process ID 54) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; spid54\u53d1\u73b0\u4e86\u6b7b\u9501\u3002<br \/>\n&nbsp;&nbsp; \u90a3\u4e48\uff0c\u6211\u4eec\u8be5\u5982\u4f55\u89e3\u51b3\u5b83\uff1f<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; \u5728SqlServer 2005\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u4e48\u505a\uff1a<br \/>\n&nbsp;&nbsp; 1.\u5728trans3\u7684\u7a97\u53e3\u4e2d\uff0c\u9009\u62e9EXEC p1 4\uff0c\u7136\u540eright click\uff0c\u770b\u5230\u4e86\u83dc\u5355\u4e86\u5417\uff1f\u9009\u62e9Analyse Query in Database Engine Tuning Advisor\u3002<br \/>\n&nbsp;&nbsp; 2.\u6ce8\u610f\u53f3\u9762\u7684\u7a97\u53e3\u4e2d\uff0cwordload\u6709\u4e09\u4e2a\u9009\u62e9\uff1a\u8d1f\u8f7d\u6587\u4ef6\u3001\u8868\u3001\u67e5\u8be2\u8bed\u53e5\uff0c\u56e0\u4e3a\u6211\u4eec\u9009\u62e9\u4e86\u67e5\u8be2\u8bed\u53e5\u7684\u65b9\u5f0f\uff0c\u6240\u4ee5\u5c31\u4e0d\u9700\u8981\u4fee\u6539\u8fd9\u4e2aradio option\u4e86\u3002<br \/>\n&nbsp;&nbsp; 3.\u70b9\u5de6\u4e0a\u89d2\u7684Start Analysis\u6309\u94ae<br \/>\n&nbsp;&nbsp; 4.\u62bd\u6839\u70df\uff0c\u56de\u6765\u540e\u770b\u7ed3\u679c\u5427\uff01\u51fa\u73b0\u4e86\u4e00\u4e2a\u5206\u6790\u7ed3\u679c\u7a97\u53e3\uff0c\u5176\u4e2d\uff0c\u5728Index Recommendations\u4e2d\uff0c\u6211\u4eec\u53d1\u73b0\u4e86\u4e00\u6761\u4fe1\u606f\uff1a\u5927\u610f\u662f\uff0c\u5728\u8868t1\u4e0a\u589e\u52a0\u4e00\u4e2a\u975e\u805a\u96c6\u7d22\u5f15\u7d22\u5f15\uff1at2+t1\u3002<br \/>\n&nbsp;&nbsp; 5.\u5728\u5f53\u524d\u7a97\u53e3\u7684\u4e0a\u65b9\u83dc\u5355\u4e0a\uff0c\u9009\u62e9Action\u83dc\u5355\uff0c\u9009\u62e9Apply Recommendations\uff0c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u521b\u5efa\u8fd9\u4e2a\u7d22\u5f15\u3002<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; \u91cd\u65b0\u8fd0\u884cbatch #3\uff0c\u5475\u5475\uff0c\u6b7b\u9501\u6ca1\u6709\u4e86\u3002<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\u4e3a\u4ec0\u4e48\u4f1a\u6b7b\u9501\u5462\uff1f\u518d\u56de\u987e\u4e00\u4e0b\u4e24\u4e2asp\u7684\u5199\u6cd5\uff1a<br \/>\n&nbsp;&nbsp; CREATE PROC p1 @p1 int AS<span class=\"Apple-converted-space\">&nbsp;<\/span><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SELECT c2, c3 FROM t1 WHERE c2 BETWEEN @p1 AND @p1+1<br \/>\n&nbsp;&nbsp; GO<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\n&nbsp;&nbsp; CREATE PROC p2 @p1 int AS<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UPDATE t1 SET c2 = c2+1 WHERE c1 = @p1<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UPDATE t1 SET c2 = c2-1 WHERE c1 = @p1<br \/>\n&nbsp;&nbsp; GO<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; \u5f88\u5947\u602a\u5427\uff01p1\u6ca1\u6709insert\uff0c\u6ca1\u6709delete\uff0c\u6ca1\u6709update\uff0c\u53ea\u662f\u4e00\u4e2aselect\uff0cp2\u624d\u662fupdate\u3002\u8fd9\u4e2a\u548c\u6211\u4eec\u524d\u9762\u8bf4\u8fc7\u7684\uff0ctrans1\u91cc\u9762updata A\uff0cupdate B\uff1btrans2\u91cc\u9762upate B\uff0cupdate A\uff0c\u6839\u672c\u4e0d\u8d34\u8fb9\u554a\uff01<br \/>\n&nbsp;&nbsp; \u90a3\u4e48\uff0c\u4ec0\u4e48\u5bfc\u81f4\u4e86\u6b7b\u9501\uff1f<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; \u9700\u8981\u4ece\u4e8b\u4ef6\u65e5\u5fd7\u4e2d\uff0c\u770bsql\u7684\u6b7b\u9501\u4fe1\u606f\uff1a<br \/>\n&nbsp;&nbsp; Spid X is running this query (line 2 of proc [p1], inputbuffer \u201c\u2026 EXEC p1 4 \u2026\u201d):<span class=\"Apple-converted-space\">&nbsp;<\/span><br \/>\n&nbsp;&nbsp; SELECT c2, c3 FROM t1 WHERE c2 BETWEEN @p1 AND @p1+1<br \/>\n&nbsp;&nbsp; Spid Y is running this query (line 2 of proc [p2], inputbuffer \u201cEXEC p2 4\u201d):<span class=\"Apple-converted-space\">&nbsp;<\/span><br \/>\n&nbsp;&nbsp; UPDATE t1 SET c2 = c2+1 WHERE c1 = @p1<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=\"Apple-converted-space\">&nbsp;<\/span><br \/>\n&nbsp;&nbsp; The SELECT is waiting for a Shared KEY lock on index t1.cidx.&nbsp; The UPDATE holds a conflicting X lock.<span class=\"Apple-converted-space\">&nbsp;<\/span><br \/>\n&nbsp;&nbsp; The UPDATE is waiting for an eXclusive KEY lock on index t1.idx1.&nbsp; The SELECT holds a conflicting S lock.<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; \u9996\u5148\uff0c\u6211\u4eec\u770b\u770bp1\u7684\u6267\u884c\u8ba1\u5212\u3002\u600e\u4e48\u770b\u5462\uff1f\u53ef\u4ee5\u6267\u884cset statistics profile on\uff0c\u8fd9\u53e5\u5c31\u53ef\u4ee5\u4e86\u3002\u4e0b\u9762\u662fp1\u7684\u6267\u884c\u8ba1\u5212<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\n&nbsp;&nbsp; SELECT c2, c3 FROM t1 WHERE c2 BETWEEN @p1 AND @p1+1<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |--Nested Loops(Inner Join, OUTER REFERENCES:([Uniq1002], [t1].[c1]))<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |--Index Seek(OBJECT:([t1].[idx1]), SEEK:([t1].[c2] &gt;= [@p1] AND [t1].[c2] &lt;= [@p1]+(1)) ORDERED FORWARD)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |--Clustered Index Seek(OBJECT:([t1].[cidx]), SEEK:([t1].[c1]=[t1].[c1] AND [Uniq1002]=[Uniq1002]) LOOKUP ORDERED FORWARD)<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; \u6211\u4eec\u770b\u5230\u4e86\u4e00\u4e2anested loops\uff0c\u7b2c\u4e00\u884c\uff0c\u5229\u7528\u7d22\u5f15t1.c2\u6765\u8fdb\u884cseek\uff0cseek\u51fa\u6765\u7684\u90a3\u4e2arowid\uff0c\u5728\u7b2c\u4e8c\u884c\u4e2d\uff0c\u7528\u6765\u901a\u8fc7\u805a\u96c6\u7d22\u5f15\u6765\u67e5\u627e\u6574\u884c\u7684\u6570\u636e\u3002\u8fd9\u662f\u4ec0\u4e48\uff1f\u5c31\u662fbookmark lookup\u554a\uff01\u4e3a\u4ec0\u4e48\uff1f\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u7684c2\u3001c3\u4e0d\u80fd\u5b8c\u5168\u7684\u88ab\u7d22\u5f15t1.c1\u5e26\u51fa\u6765\uff0c\u6240\u4ee5\u9700\u8981\u4e66\u7b7e\u67e5\u627e\u3002<br \/>\n&nbsp;&nbsp; \u597d\uff0c\u6211\u4eec\u63a5\u7740\u770bp2\u7684\u6267\u884c\u8ba1\u5212\u3002<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\n&nbsp;&nbsp; UPDATE t1 SET c2 = c2+1 WHERE c1 = @p1<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |--Clustered Index Update(OBJECT:([t1].[cidx]), OBJECT:([t1].[idx1]), SET:([t1].[c2] = [Expr1004]))<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |--Compute Scalar(DEFINE:([Expr1013]=[Expr1013]))<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |--Compute Scalar(DEFINE:([Expr1004]=[t1].[c2]+(1), [Expr1013]=CASE WHEN CASE WHEN ...<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |--Top(ROWCOUNT est 0)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |--Clustered Index Seek(OBJECT:([t1].[cidx]), SEEK:([t1].[c1]=[@p1]) ORDERED FORWARD)<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; \u901a\u8fc7\u805a\u96c6\u7d22\u5f15\u7684seek\u627e\u5230\u4e86\u4e00\u884c\uff0c\u7136\u540e\u5f00\u59cb\u66f4\u65b0\u3002\u8fd9\u91cc\u6ce8\u610f\u7684\u662f\uff0cupdate\u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u7533\u8bf7\u4e00\u4e2a\u9488\u5bf9clustered index\u7684X\u9501\u7684\u3002<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; \u5b9e\u9645\u4e0a\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u5c31\u660e\u767d\u4e86\u4e3a\u4ec0\u4e48update\u4f1a\u5bf9select\u4ea7\u751f\u6b7b\u9501\u3002update\u7684\u65f6\u5019\uff0c\u4f1a\u7533\u8bf7\u4e00\u4e2a\u9488\u5bf9clustered index\u7684X\u9501\uff0c\u8fd9\u6837\u5c31\u963b\u585e\u4f4f\u4e86\uff08\u6ce8\u610f\uff0c\u4e0d\u662f\u6b7b\u9501\uff01\uff09select\u91cc\u9762\u6700\u540e\u7684\u90a3\u4e2aclustered index seek\u3002\u6b7b\u9501\u7684\u53e6\u4e00\u534a\u5728\u54ea\u91cc\u5462\uff1f\u6ce8\u610f\u6211\u4eec\u7684select\u8bed\u53e5\uff0cc2\u5b58\u5728\u4e8e\u7d22\u5f15idx1\u4e2d\uff0cc1\u662f\u4e00\u4e2a\u805a\u96c6\u7d22\u5f15cidx\u3002\u95ee\u9898\u5c31\u5728\u8fd9\u91cc\uff01\u6211\u4eec\u5728p2\u4e2d\u66f4\u65b0\u4e86c2\u8fd9\u4e2a\u503c\uff0c\u6240\u4ee5sqlserver\u4f1a\u81ea\u52a8\u66f4\u65b0\u5305\u542bc2\u5217\u7684\u975e\u805a\u96c6\u7d22\u5f15\uff1aidx1\u3002\u800cidx1\u5728\u54ea\u91cc\uff1f\u5c31\u5728\u6211\u4eec\u521a\u624d\u7684select\u8bed\u53e5\u4e2d\u3002\u800c\u5bf9\u8fd9\u4e2a\u7d22\u5f15\u5217\u7684\u66f4\u6539\uff0c\u610f\u5473\u7740\u7d22\u5f15\u96c6\u5408\u7684\u67d0\u4e2a\u884c\u6216\u8005\u67d0\u4e9b\u884c\uff0c\u9700\u8981\u91cd\u65b0\u6392\u5217\uff0c\u800c\u91cd\u65b0\u6392\u5217\uff0c\u9700\u8981\u4e00\u4e2aX\u9501\u3002<br \/>\n&nbsp;&nbsp; SO\u2026\u2026\u2026\uff0c\u95ee\u9898\u5c31\u8fd9\u6837\u88ab\u53d1\u73b0\u4e86\u3002<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; \u603b\u7ed3\u4e00\u4e0b\uff0c\u5c31\u662f\u8bf4\uff0c\u67d0\u4e2aquery\u4f7f\u7528\u975e\u805a\u96c6\u7d22\u5f15\u6765select\u6570\u636e\uff0c\u90a3\u4e48\u5b83\u4f1a\u5728\u975e\u805a\u96c6\u7d22\u5f15\u4e0a\u6301\u6709\u4e00\u4e2aS\u9501\u3002\u5f53\u6709\u4e00\u4e9bselect\u7684\u5217\u4e0d\u5728\u8be5\u7d22\u5f15\u4e0a\uff0c\u5b83\u9700\u8981\u6839\u636erowid\u627e\u5230\u5bf9\u5e94\u7684\u805a\u96c6\u7d22\u5f15\u7684\u90a3\u884c\uff0c\u7136\u540e\u627e\u5230\u5176\u4ed6\u6570\u636e\u3002\u800c\u6b64\u65f6\uff0c\u7b2c\u4e8c\u4e2a\u7684\u67e5\u8be2\u4e2d\uff0cupdate\u6b63\u5728\u805a\u96c6\u7d22\u5f15\u4e0a\u5fd9\u4e4e\uff1a\u5b9a\u4f4d\u3001\u52a0\u9501\u3001\u4fee\u6539\u7b49\u3002\u4f46\u56e0\u4e3a\u6b63\u5728\u4fee\u6539\u7684\u67d0\u4e2a\u5217\uff0c\u662f\u53e6\u5916\u4e00\u4e2a\u975e\u805a\u96c6\u7d22\u5f15\u7684\u67d0\u4e2a\u5217\uff0c\u6240\u4ee5\u6b64\u65f6\uff0c\u5b83\u9700\u8981\u540c\u65f6\u66f4\u6539\u90a3\u4e2a\u975e\u805a\u96c6\u7d22\u5f15\u7684\u4fe1\u606f\uff0c\u8fd9\u5c31\u9700\u8981\u5728\u90a3\u4e2a\u975e\u805a\u96c6\u7d22\u5f15\u4e0a\uff0c\u52a0\u7b2c\u4e8c\u4e2aX\u9501\u3002select\u5f00\u59cb\u7b49\u5f85update\u7684X\u9501\uff0cupdate\u5f00\u59cb\u7b49\u5f85select\u7684S\u9501\uff0c\u6b7b\u9501\uff0c\u5c31\u8fd9\u6837\u53d1\u751f\u9e1f\u3002<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; \u90a3\u4e48\uff0c\u4e3a\u4ec0\u4e48\u6211\u4eec\u589e\u52a0\u4e86\u4e00\u4e2a\u975e\u805a\u96c6\u7d22\u5f15\uff0c\u6b7b\u9501\u5c31\u6d88\u5931\u9e1f\uff1f\u6211\u4eec\u770b\u4e00\u4e0b\uff0c\u6309\u7167\u4e0a\u6587\u4e2d\u81ea\u52a8\u589e\u52a0\u7684\u7d22\u5f15\u4e4b\u540e\u7684\u6267\u884c\u8ba1\u5212\uff1a<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\n&nbsp;&nbsp; SELECT c2, c3 FROM t1 WHERE c2 BETWEEN @p1 AND @p1+1<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |--Index Seek(OBJECT:([deadlocktest].[dbo].[t1].[_dta_index_t1_7_2073058421__K2_K1_3]), SEEK:([deadlocktest].[dbo].[t1].[c2] &gt;= [@p1] AND [deadlocktest].[dbo].[t1].[c2] &lt;= [@p1]+(1)) ORDERED FORWARD)<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; \u54e6\uff0c\u5bf9\u4e8eclustered index\u7684\u9700\u6c42\u6ca1\u6709\u4e86\uff0c\u56e0\u4e3a\u589e\u52a0\u7684\u8986\u76d6\u7d22\u5f15\u5df2\u7ecf\u8db3\u591f\u628a\u6240\u6709\u7684\u4fe1\u606f\u90fdselect\u51fa\u6765\u3002\u5c31\u8fd9\u4e48\u7b80\u5355\u3002<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; \u5b9e\u9645\u4e0a\uff0c\u5728sqlserver 2005\u4e2d\uff0c\u5982\u679c\u7528profiler\u6765\u6293eventid\uff1a1222\uff0c\u90a3\u4e48\u4f1a\u51fa\u73b0\u4e00\u4e2a\u6b7b\u9501\u7684\u56fe\uff0c\u5f88\u76f4\u89c2\u7684\u8bf4\u3002<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp; \u4e0b\u9762\u7684\u65b9\u6cd5\uff0c\u6709\u52a9\u4e8e\u5c06\u6b7b\u9501\u51cf\u81f3\u6700\u5c11\uff08\u8be6\u7ec6\u60c5\u51b5\uff0c\u8bf7\u770bSQLServer\u8054\u673a\u5e2e\u52a9\uff0c\u641c\u7d22\uff1a<span style=\"color:#ff0000;\">\u5c06\u6b7b\u9501\u51cf\u81f3\u6700\u5c11<\/span>\u5373\u53ef\u3002<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; . \u6309\u540c\u4e00\u987a\u5e8f\u8bbf\u95ee\u5bf9\u8c61\u3002&nbsp;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;. \u907f\u514d\u4e8b\u52a1\u4e2d\u7684\u7528\u6237\u4ea4\u4e92\u3002&nbsp;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; . \u4fdd\u6301\u4e8b\u52a1\u7b80\u77ed\u5e76\u5904\u4e8e\u4e00\u4e2a\u6279\u5904\u7406\u4e2d\u3002&nbsp;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; . \u4f7f\u7528\u8f83\u4f4e\u7684\u9694\u79bb\u7ea7\u522b\u3002&nbsp;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;. \u4f7f\u7528\u57fa\u4e8e\u884c\u7248\u672c\u63a7\u5236\u7684\u9694\u79bb\u7ea7\u522b\u3002&nbsp;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; . \u5c06 READ_COMMITTED_SNAPSHOT \u6570\u636e\u5e93\u9009\u9879\u8bbe\u7f6e\u4e3a ON\uff0c\u4f7f\u5f97\u5df2\u63d0\u4ea4\u8bfb\u4e8b\u52a1\u4f7f\u7528\u884c\u7248\u672c\u63a7\u5236\u3002&nbsp;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; . \u4f7f\u7528\u5feb\u7167\u9694\u79bb\u3002&nbsp;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; . \u4f7f\u7528\u7ed1\u5b9a\u8fde\u63a5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\u5176\u5b9e\u6240\u6709\u7684\u6b7b\u9501\u6700\u6df1\u5c42\u7684\u539f\u56e0\u5c31\u662f\u4e00\u4e2a\uff1a\u8d44\u6e90\u7ade\u4e89<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\"><strong>\u8868\u73b0\u4e00\uff1a<\/strong><\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\u3000\u3000\u4e00\u4e2a\u7528\u6237A \u8bbf\u95ee\u8868A(\u9501\u4f4f\u4e86\u8868A)\uff0c\u7136\u540e\u53c8\u8bbf\u95ee\u8868B\uff0c\u53e6\u4e00\u4e2a\u7528\u6237B \u8bbf\u95ee\u8868B(\u9501\u4f4f\u4e86\u8868B)\uff0c\u7136\u540e\u4f01\u56fe\u8bbf\u95ee\u8868A\uff0c\u8fd9\u65f6\u7528\u6237A\u7531\u4e8e\u7528\u6237B\u5df2\u7ecf\u9501\u4f4f\u8868B\uff0c\u5b83\u5fc5\u987b\u7b49\u5f85\u7528\u6237B\u91ca\u653e\u8868B\uff0c\u624d\u80fd\u7ee7\u7eed\uff0c\u597d\u4e86\u4ed6\u8001\u4eba\u5bb6\u5c31\u53ea\u597d\u8001\u8001\u5b9e\u5b9e\u5728\u8fd9\u7b49\u4e86\uff0c\u540c\u6837\u7528\u6237B\u8981\u7b49\u7528\u6237A\u91ca\u653e\u8868A\u624d\u80fd\u7ee7\u7eed\u8fd9\u5c31\u6b7b\u9501\u4e86\u3002<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\u3000\u3000\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\u3000\u3000\u8fd9\u79cd\u6b7b\u9501\u662f\u7531\u4e8e\u4f60\u7684\u7a0b\u5e8f\u7684BUG\u4ea7\u751f\u7684\uff0c\u9664\u4e86\u8c03\u6574\u4f60\u7684\u7a0b\u5e8f\u7684\u903b\u8f91\u522b\u65e0\u4ed6\u6cd5<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\u3000\u3000\u4ed4\u7ec6\u5206\u6790\u4f60\u7a0b\u5e8f\u7684\u903b\u8f91\uff1a<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\u3000\u30001\uff1a\u5c3d\u91cf\u907f\u514d\u540c\u65f6\u9501\u5b9a\u4e24\u4e2a\u8d44\u6e90<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\u3000\u30002: \u5fc5\u987b\u540c\u65f6\u9501\u5b9a\u4e24\u4e2a\u8d44\u6e90\u65f6\uff0c\u8981\u4fdd\u8bc1\u5728\u4efb\u4f55\u65f6\u523b\u90fd\u5e94\u8be5\u6309\u7167\u76f8\u540c\u7684\u987a\u5e8f\u6765\u9501\u5b9a\u8d44\u6e90.<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">&nbsp;<\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\"><strong>\u8868\u73b0\u4e8c\uff1a<\/strong><\/p>\n<p style=\"white-space:normal;text-transform:none;word-spacing:0px;color:#333333;text-align:left;font:14px\/26px Arial;letter-spacing:normal;background-color:#ffffff;text-indent:0px;-webkit-text-stroke-width:0px;\">\u3000\u3000\u7528\u6237A\u8bfb\u4e00\u6761\u7eaa\u5f55\uff0c\u7136...<\/p>\n<p> <a href=\"https:\/\/www.xiaobo.li\/notes\/archives\/374\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[288],"tags":[172],"class_list":["post-374","post","type-post","status-publish","format-standard","hentry","category-db","tag-sqlserver"],"_links":{"self":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/374","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/comments?post=374"}],"version-history":[{"count":0,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/374\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/media?parent=374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/categories?post=374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/tags?post=374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}