{"id":170045,"date":"2024-11-06T20:41:21","date_gmt":"2024-11-06T12:41:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/170045\/"},"modified":"2024-11-06T20:41:22","modified_gmt":"2024-11-06T12:41:22","slug":"%e5%85%a7%e7%99%bc%e7%94%9f%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e5%95%8f%e9%a1%8cmssql-%e6%9c%80%e8%bf%911%e5%b0%8f%e6%99%82%e5%85%a7%e7%99%bc%e7%94%9f%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e6%8c%91","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/170045\/","title":{"rendered":"\u5167\u767c\u751f\u7684\u6578\u64da\u5eab\u554f\u984cMSSQL: \u6700\u8fd11\u5c0f\u6642\u5167\u767c\u751f\u7684\u6578\u64da\u5eab\u6311\u6230\uff08mssql \u6700\u8fd1\u4e00\u5c0f\u6642\uff09"},"content":{"rendered":"<h1 id=\"%e5%85%a7%e7%99%bc%e7%94%9f%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e5%95%8f%e9%a1%8cmssql-%e6%9c%80%e8%bf%911%e5%b0%8f%e6%99%82%e5%85%a7%e7%99%bc%e7%94%9f%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e6%8c%91-oyBrSnknGK\">\u5167\u767c\u751f\u7684\u6578\u64da\u5eab\u554f\u984cMSSQL: \u6700\u8fd11\u5c0f\u6642\u5167\u767c\u751f\u7684\u6578\u64da\u5eab\u6311\u6230<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u7a69\u5b9a\u6027\u548c\u6027\u80fd\u81f3\u95dc\u91cd\u8981\u3002\u7279\u5225\u662f\u5c0d\u65bc\u4f7f\u7528 Microsoft SQL Server (MSSQL) \u7684\u4f01\u696d\u4f86\u8aaa\uff0c\u4efb\u4f55\u5c0f\u7684\u554f\u984c\u90fd\u53ef\u80fd\u5c0e\u81f4\u91cd\u5927\u7684\u696d\u52d9\u4e2d\u65b7\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u6700\u8fd1\u4e00\u5c0f\u6642\u5167\u53ef\u80fd\u767c\u751f\u7684\u6578\u64da\u5eab\u6311\u6230\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u89e3\u6c7a\u65b9\u6848\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2 id=\"mssql-%e6%95%b8%e6%93%9a%e5%ba%ab%e5%b8%b8%e8%a6%8b%e5%95%8f%e9%a1%8c-oyBrSnknGK\">MSSQL \u6578\u64da\u5eab\u5e38\u898b\u554f\u984c<\/h2>\n<p>MSSQL \u6578\u64da\u5eab\u5728\u904b\u884c\u904e\u7a0b\u4e2d\u53ef\u80fd\u6703\u9047\u5230\u591a\u7a2e\u554f\u984c\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u6311\u6230\uff1a<\/p>\n<ul>\n<li><strong>\u6027\u80fd\u4e0b\u964d\uff1a<\/strong>\u96a8\u8457\u6578\u64da\u91cf\u7684\u589e\u52a0\uff0c\u67e5\u8a62\u6027\u80fd\u53ef\u80fd\u6703\u4e0b\u964d\uff0c\u5c0e\u81f4\u61c9\u7528\u7a0b\u5e8f\u97ff\u61c9\u8b8a\u6162\u3002<\/li>\n<li><strong>\u6b7b\u9396\uff1a<\/strong>\u7576\u5169\u500b\u6216\u591a\u500b\u9032\u7a0b\u4e92\u76f8\u7b49\u5f85\u5c0d\u65b9\u91cb\u653e\u8cc7\u6e90\u6642\uff0c\u6703\u767c\u751f\u6b7b\u9396\uff0c\u9019\u6703\u5c0e\u81f4\u7cfb\u7d71\u7121\u6cd5\u7e7c\u7e8c\u904b\u884c\u3002<\/li>\n<li><strong>\u9023\u63a5\u554f\u984c\uff1a<\/strong>\u6578\u64da\u5eab\u9023\u63a5\u6578\u904e\u591a\u6216\u914d\u7f6e\u4e0d\u7576\u53ef\u80fd\u5c0e\u81f4\u9023\u63a5\u8d85\u6642\u6216\u62d2\u7d55\u670d\u52d9\u3002<\/li>\n<li><strong>\u6578\u64da\u640d\u58de\uff1a<\/strong>\u786c\u4ef6\u6545\u969c\u6216\u4e0d\u7576\u64cd\u4f5c\u53ef\u80fd\u5c0e\u81f4\u6578\u64da\u640d\u58de\uff0c\u5f71\u97ff\u6578\u64da\u7684\u5b8c\u6574\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e6%9c%80%e8%bf%91%e4%b8%80%e5%b0%8f%e6%99%82%e5%85%a7%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e6%8c%91%e6%88%b0-oyBrSnknGK\">\u6700\u8fd1\u4e00\u5c0f\u6642\u5167\u7684\u6578\u64da\u5eab\u6311\u6230<\/h2>\n<p>\u5728\u6700\u8fd1\u4e00\u5c0f\u6642\u5167\uff0cMSSQL \u6578\u64da\u5eab\u53ef\u80fd\u9762\u81e8\u4ee5\u4e0b\u5177\u9ad4\u6311\u6230\uff1a<\/p>\n<h3 id=\"1-%e6%9f%a5%e8%a9%a2%e6%80%a7%e8%83%bd%e5%95%8f%e9%a1%8c-oyBrSnknGK\">1. \u67e5\u8a62\u6027\u80fd\u554f\u984c<\/h3>\n<p>\u5982\u679c\u5728\u6700\u8fd1\u4e00\u5c0f\u6642\u5167\u767c\u73fe\u67e5\u8a62\u57f7\u884c\u6642\u9593\u7570\u5e38\u589e\u9577\uff0c\u53ef\u80fd\u662f\u7531\u65bc\u7d22\u5f15\u7f3a\u5931\u6216\u4e0d\u7576\u7684\u67e5\u8a62\u8a08\u5283\u3002\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8a62\u53ef\u4ee5\u6aa2\u67e5\u6700\u8fd1\u7684\u67e5\u8a62\u6027\u80fd\uff1a<\/p>\n<pre><code>SELECT TOP 10\n    qs.total_elapsed_time \/ qs.execution_count AS [Avg Time],\n    qs.execution_count,\n    qs.total_elapsed_time,\n    qs.total_logical_reads,\n    qs.total_worker_time,\n    SUBSTRING(qt.text, (qs.statement_start_offset\/2)+1,\n    ((CASE qs.statement_end_offset\n        WHEN -1 THEN DATALENGTH(qt.text)\n        ELSE qs.statement_end_offset\n    END - qs.statement_start_offset)\/2) + 1) AS [Query Text]\nFROM\n    sys.dm_exec_query_stats AS qs\nCROSS APPLY\n    sys.dm_exec_sql_text(qs.sql_handle) AS qt\nWHERE\n    qs.creation_time &gt; DATEADD(HOUR, -1, GETDATE())\nORDER BY\n    [Avg Time] DESC;<\/code><\/pre>\n<h3 id=\"2-%e6%ad%bb%e9%8e%96%e6%aa%a2%e6%b8%ac-oyBrSnknGK\">2. \u6b7b\u9396\u6aa2\u6e2c<\/h3>\n<p>\u6b7b\u9396\u662f MSSQL \u4e2d\u7684\u4e00\u500b\u5e38\u898b\u554f\u984c\uff0c\u7279\u5225\u662f\u5728\u9ad8\u4e26\u767c\u74b0\u5883\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8a62\u4f86\u6aa2\u67e5\u6700\u8fd1\u7684\u6b7b\u9396\u4e8b\u4ef6\uff1a<\/p>\n<pre><code>SELECT\n    event_data.value('(event\/@timestamp)[1]', 'datetime') AS [Timestamp],\n    event_data.value('(event\/data[@name=\"database_id\"]\/value)[1]', 'int') AS [DatabaseID],\n    event_data.value('(event\/data[@name=\"object_id\"]\/value)[1]', 'int') AS [ObjectID],\n    event_data.value('(event\/data[@name=\"transaction_id\"]\/value)[1]', 'int') AS [TransactionID]\nFROM\n    (SELECT CAST(event_data AS XML) AS event_data\n     FROM sys.fn_xe_file_target_read_file('C:PathToYour*.xel', NULL, NULL, NULL)\n     WHERE event_data.value('(event\/@name)[1]', 'varchar(50)') = 'xml_deadlock_report') AS x\nORDER BY [Timestamp] DESC;<\/code><\/pre>\n<h3 id=\"3-%e9%80%a3%e6%8e%a5%e5%95%8f%e9%a1%8c-oyBrSnknGK\">3. \u9023\u63a5\u554f\u984c<\/h3>\n<p>\u5982\u679c\u767c\u73fe\u9023\u63a5\u6578\u904e\u591a\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8a62\u4f86\u6aa2\u67e5\u7576\u524d\u7684\u9023\u63a5\u72c0\u614b\uff1a<\/p>\n<pre><code>SELECT\n    DB_NAME(dbid) AS [Database],\n    COUNT(dbid) AS [Connections]\nFROM\n    sys.sysprocesses\nGROUP BY\n    dbid\nORDER BY\n    [Connections] DESC;<\/code><\/pre>\n<h2 id=\"%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88%e8%88%87%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-oyBrSnknGK\">\u89e3\u6c7a\u65b9\u6848\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u70ba\u4e86\u6709\u6548\u61c9\u5c0d\u9019\u4e9b\u6311\u6230\uff0c\u4f01\u696d\u61c9\u8003\u616e\u4ee5\u4e0b\u6700\u4f73\u5be6\u8e10\uff1a<\/p>\n<ul>\n<li><strong>\u5b9a\u671f\u76e3\u63a7\uff1a<\/strong>\u4f7f\u7528\u6027\u80fd\u76e3\u63a7\u5de5\u5177\u5b9a\u671f\u6aa2\u67e5\u6578\u64da\u5eab\u6027\u80fd\uff0c\u53ca\u6642\u767c\u73fe\u554f\u984c\u3002<\/li>\n<li><strong>\u512a\u5316\u67e5\u8a62\uff1a<\/strong>\u5c0d\u65bc\u6027\u80fd\u4e0b\u964d\u7684\u67e5\u8a62\uff0c\u61c9\u9032\u884c\u512a\u5316\uff0c\u78ba\u4fdd\u4f7f\u7528\u9069\u7576\u7684\u7d22\u5f15\u3002<\/li>\n<li><strong>\u914d\u7f6e\u6b7b\u9396\u6aa2\u6e2c\uff1a<\/strong>\u555f\u7528\u6b7b\u9396\u6aa2\u6e2c\u529f\u80fd\uff0c\u4e26\u5b9a\u671f\u6aa2\u67e5\u6b7b\u9396\u5831\u544a\u3002<\/li>\n<li><strong>\u8cc7\u6e90\u7ba1\u7406\uff1a<\/strong>\u5408\u7406\u914d\u7f6e\u6578\u64da\u5eab\u7684\u8cc7\u6e90\uff0c\u907f\u514d\u904e\u591a\u7684\u9023\u63a5\u5c0e\u81f4\u6027\u80fd\u4e0b\u964d\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-oyBrSnknGK\">\u7e3d\u7d50<\/h2>\n<p>\u5728\u4f7f\u7528 MSSQL \u6578\u64da\u5eab\u7684\u904e\u7a0b\u4e2d\uff0c\u4e86\u89e3\u548c\u61c9\u5c0d\u6700\u8fd1\u4e00\u5c0f\u6642\u5167\u53ef\u80fd\u767c\u751f\u7684\u6578\u64da\u5eab\u6311\u6230\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u901a\u904e\u5b9a\u671f\u76e3\u63a7\u548c\u512a\u5316\u67e5\u8a62\uff0c\u4f01\u696d\u53ef\u4ee5\u6709\u6548\u6e1b\u5c11\u6027\u80fd\u554f\u984c\u548c\u6b7b\u9396\u7684\u767c\u751f\u3002\u82e5\u60a8\u9700\u8981\u9032\u4e00\u6b65\u7684\u652f\u6301\u6216\u89e3\u6c7a\u65b9\u6848\uff0c\u8acb\u8003\u616e\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u670d\u52d9\uff0c\u63d0\u4f9b\u7a69\u5b9a\u7684\u6578\u64da\u5eab\u74b0\u5883\u548c\u5c08\u696d\u7684\u6280\u8853\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u8a0e\u6700\u8fd11\u5c0f\u6642\u5167MSSQL\u6578\u64da\u5eab\u9762\u81e8\u7684\u6311\u6230\u8207\u554f\u984c\uff0c\u63d0\u4f9b\u89e3\u6c7a\u65b9\u6848\u8207\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-170045","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/170045","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=170045"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/170045\/revisions"}],"predecessor-version":[{"id":170046,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/170045\/revisions\/170046"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=170045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=170045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=170045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}