{"id":40,"date":"2025-09-25T09:03:21","date_gmt":"2025-09-25T01:03:21","guid":{"rendered":"https:\/\/www.sv0.cn\/?p=40"},"modified":"2025-09-25T11:08:33","modified_gmt":"2025-09-25T03:08:33","slug":"__trashed-3","status":"publish","type":"post","link":"https:\/\/www.sv0.cn\/?p=40","title":{"rendered":"SQL Server \u6027\u80fd\u6392\u67e5\u811a\u672c"},"content":{"rendered":"<h2>\u6458\u8981<\/h2>\n<p>\u672c\u6587\u4ef6\u5305\u542b\u4e00\u7cfb\u5217\u7528\u4e8e SQL Server \u6027\u80fd\u6392\u67e5\u7684 SQL \u811a\u672c\uff0c\u4e3b\u8981\u7528\u4e8e\u5206\u6790\u67e5\u8be2\u6267\u884c\u7edf\u8ba1\u4fe1\u606f\uff0c\u8bc6\u522b\u9ad8 CPU \u6d88\u8017\u3001\u9891\u7e41\u6267\u884c\u3001\u9ad8 I\/O \u7b49\u6027\u80fd\u74f6\u9888\u3002<\/p>\n<p><!--more--><\/p>\n<hr>\n<h2>1. \u6700\u6d88\u8017 CPU \u8d44\u6e90\u7684\u67e5\u8be2\u8ba1\u5212<\/h2>\n<pre><code class=\"language-sql\">SELECT TOP 10 \n    total_worker_time,        -- \u603bCPU\u5de5\u4f5c\u65f6\u95f4\uff08\u5fae\u79d2\uff09\n    last_worker_time,         -- \u4e0a\u6b21\u6267\u884c\u7684CPU\u65f6\u95f4\n    max_worker_time,          -- \u6700\u5927\u5355\u6b21CPU\u65f6\u95f4\n    min_worker_time,          -- \u6700\u5c0f\u5355\u6b21CPU\u65f6\u95f4\n    SUBSTRING(st.text, (qs.statement_start_offset\/2) + 1,\n        ((CASE statement_end_offset \n          WHEN -1 THEN DATALENGTH(st.text)\n          ELSE qs.statement_end_offset END \n         - qs.statement_start_offset)\/2) + 1) as statement_text  -- \u63d0\u53d6\u7684SQL\u8bed\u53e5\u6587\u672c\nFROM sys.dm_exec_query_stats as qs\nCROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) as st\nORDER BY max_worker_time DESC\n<\/code><\/pre>\n<p><strong>\u7528\u9014\u8bf4\u660e\uff1a<\/strong> \u8bc6\u522b\u5355\u6b21\u6267\u884cCPU\u6d88\u8017\u6700\u9ad8\u7684\u67e5\u8be2\u3002<br \/>\n<strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong> <code>max_worker_time<\/code>\u53cd\u6620\u7684\u662f\u5355\u6b21\u6267\u884c\u7684\u5cf0\u503cCPU\u6d88\u8017\u3002<\/p>\n<hr>\n<h2>2. \u6700\u9891\u7e41\u4f7f\u7528\u7684\u67e5\u8be2\u8ba1\u5212<\/h2>\n<pre><code class=\"language-sql\">SELECT TOP 10 \n    creation_time,            -- \u67e5\u8be2\u8ba1\u5212\u7f16\u8bd1\u65f6\u95f4\n    last_execution_time,      -- \u6700\u540e\u6267\u884c\u65f6\u95f4\n    execution_count,          -- \u603b\u6267\u884c\u6b21\u6570\n    SUBSTRING(st.text, (qs.statement_start_offset\/2) + 1,\n        ((CASE statement_end_offset \n          WHEN -1 THEN DATALENGTH(st.text)\n          ELSE qs.statement_end_offset END \n         - qs.statement_start_offset)\/2) + 1) as statement_text  -- \u63d0\u53d6\u7684SQL\u8bed\u53e5\u6587\u672c\nFROM sys.dm_exec_query_stats as qs\nCROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) as st\nORDER BY execution_count DESC\n<\/code><\/pre>\n<p><strong>\u7528\u9014\u8bf4\u660e\uff1a<\/strong> \u627e\u51fa\u6267\u884c\u6700\u9891\u7e41\u7684\u67e5\u8be2\uff0c\u53ef\u80fd\u9700\u4f18\u5316\u7f13\u5b58\u6216\u7d22\u5f15\u3002<br \/>\n<strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong> \u9ad8\u9891\u7387\u67e5\u8be2\u5373\u4f7f\u5355\u6b21\u6d88\u8017\u4f4e\uff0c\u7d2f\u8ba1\u5f71\u54cd\u4e5f\u53ef\u80fd\u5f88\u5927\u3002<\/p>\n<hr>\n<h2>3. \u5355\u6b21\u6267\u884c\u5e73\u5747\u8d44\u6e90\u6d88\u8017\u6700\u9ad8\u7684\u67e5\u8be2<\/h2>\n<pre><code class=\"language-sql\">SELECT Top 10 \n    plan_generation_num,      -- \u8ba1\u5212\u751f\u6210\u7f16\u53f7\uff08\u91cd\u65b0\u7f16\u8bd1\u6b21\u6570\uff09\n    creation_time,            -- \u8ba1\u5212\u521b\u5efa\u65f6\u95f4\n    total_elapsed_time\/execution_count as avg,  -- \u5e73\u5747\u6267\u884c\u65f6\u95f4\uff08\u5fae\u79d2\uff09\n    last_execution_time,      -- \u6700\u540e\u6267\u884c\u65f6\u95f4\n    execution_count,          -- \u6267\u884c\u6b21\u6570\n    total_worker_time,        -- \u603bCPU\u65f6\u95f4\n    total_physical_reads,     -- \u7269\u7406\u8bfb\u53d6\u6b21\u6570\n    total_logical_reads,      -- \u903b\u8f91\u8bfb\u53d6\u6b21\u6570\n    total_logical_writes,     -- \u903b\u8f91\u5199\u5165\u6b21\u6570\n    total_elapsed_time,       -- \u603b\u6267\u884c\u65f6\u95f4\n    SUBSTRING(st.text, (qs.statement_start_offset\/2) + 1,\n        ((CASE statement_end_offset \n          WHEN -1 THEN DATALENGTH(st.text)\n          ELSE qs.statement_end_offset END \n         - qs.statement_start_offset)\/2) + 1) as statement_text  -- SQL\u8bed\u53e5\u6587\u672c\nFROM sys.dm_exec_query_stats as qs\nCROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) as st \nORDER BY avg desc\n<\/code><\/pre>\n<p><strong>\u7528\u9014\u8bf4\u660e\uff1a<\/strong> \u8bc6\u522b\u5355\u6b21\u6267\u884c\u5e73\u5747\u8d44\u6e90\u6d88\u8017\u6700\u9ad8\u7684\u67e5\u8be2\u3002<br \/>\n<strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong> <code>avg<\/code>\u5b57\u6bb5\u7efc\u5408\u53cd\u6620\u4e86\u67e5\u8be2\u7684\u5355\u6b21\u6027\u80fd\u8868\u73b0\u3002<\/p>\n<hr>\n<h2>4. \u7d2f\u8ba1\u8d44\u6e90\u6d88\u8017\u6700\u9ad8\u7684\u6267\u884c\u8ba1\u5212<\/h2>\n<pre><code class=\"language-sql\">SELECT Top 10 \n    plan_generation_num,      -- \u8ba1\u5212\u751f\u6210\u7f16\u53f7\n    creation_time,            -- \u521b\u5efa\u65f6\u95f4\n    last_execution_time,      -- \u6700\u540e\u6267\u884c\u65f6\u95f4\n    execution_count,          -- \u6267\u884c\u6b21\u6570\n    total_worker_time,        -- \u603bCPU\u65f6\u95f4\n    total_physical_reads,     -- \u7269\u7406\u8bfb\u53d6\u603b\u6b21\u6570\n    total_logical_reads,      -- \u903b\u8f91\u8bfb\u53d6\u603b\u6b21\u6570\n    total_logical_writes,     -- \u903b\u8f91\u5199\u5165\u603b\u6b21\u6570\n    total_elapsed_time,       -- \u603b\u6267\u884c\u65f6\u95f4\n    SUBSTRING(st.text, (qs.statement_start_offset\/2) + 1,\n        ((CASE statement_end_offset \n          WHEN -1 THEN DATALENGTH(st.text)\n          ELSE qs.statement_end_offset END \n         - qs.statement_start_offset)\/2) + 1) as statement_text  -- SQL\u8bed\u53e5\u6587\u672c\nFROM sys.dm_exec_query_stats as qs\nCROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) as st \nORDER BY total_elapsed_time desc\n<\/code><\/pre>\n<p><strong>\u7528\u9014\u8bf4\u660e\uff1a<\/strong> \u627e\u51fa\u7d2f\u8ba1\u8d44\u6e90\u6d88\u8017\u6700\u5927\u7684\u67e5\u8be2\u3002<br \/>\n<strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong> \u5173\u6ce8\u9ad8\u7d2f\u8ba1\u6d88\u8017\u7684\u67e5\u8be2\u5bf9\u7cfb\u7edf\u6574\u4f53\u6027\u80fd\u7684\u5f71\u54cd\u3002<\/p>\n<hr>\n<h2>5. \u6267\u884c\u65f6\u95f4\u6700\u957f\u768410\u6761SQL\uff08\u8be6\u7ec6\u7248\uff09<\/h2>\n<pre><code class=\"language-sql\">SELECT top 10    \n    (total_elapsed_time \/ execution_count)\/1000 N'\u5e73\u5747\u65f6\u95f4ms',    -- \u5e73\u5747\u6267\u884c\u65f6\u95f4\uff08\u6beb\u79d2\uff09\n    total_elapsed_time\/1000 N'\u603b\u82b1\u8d39\u65f6\u95f4ms',                     -- \u603b\u6267\u884c\u65f6\u95f4\uff08\u6beb\u79d2\uff09\n    total_worker_time\/1000 N'\u6240\u7528\u7684CPU\u603b\u65f6\u95f4ms',                 -- \u603bCPU\u65f6\u95f4\uff08\u6beb\u79d2\uff09\n    total_physical_reads N'\u7269\u7406\u8bfb\u53d6\u603b\u6b21\u6570',                       -- \u7269\u7406\u8bfb\u53d6\u6b21\u6570\n    total_logical_reads\/execution_count N'\u6bcf\u6b21\u903b\u8f91\u8bfb\u6b21\u6570',        -- \u5e73\u5747\u903b\u8f91\u8bfb\u53d6\u6b21\u6570\n    total_logical_reads N'\u903b\u8f91\u8bfb\u53d6\u603b\u6b21\u6570',                        -- \u903b\u8f91\u8bfb\u53d6\u603b\u6b21\u6570\n    total_logical_writes N'\u903b\u8f91\u5199\u5165\u603b\u6b21\u6570',                       -- \u903b\u8f91\u5199\u5165\u603b\u6b21\u6570\n    execution_count N'\u6267\u884c\u6b21\u6570',                                  -- \u6267\u884c\u6b21\u6570\n    creation_time N'\u8bed\u53e5\u7f16\u8bd1\u65f6\u95f4',                                -- \u7f16\u8bd1\u65f6\u95f4\n    last_execution_time N'\u4e0a\u6b21\u6267\u884c\u65f6\u95f4',                          -- \u6700\u540e\u6267\u884c\u65f6\u95f4\n    SUBSTRING(st.text, (qs.statement_start_offset\/2) + 1,        \n        ((CASE statement_end_offset \n          WHEN -1 THEN DATALENGTH(st.text) \n          ELSE qs.statement_end_offset END \n         - qs.statement_start_offset)\/2) + 1) N'\u6267\u884c\u8bed\u53e5',        -- SQL\u8bed\u53e5\u6587\u672c\n    qp.query_plan                                               -- \u67e5\u8be2\u6267\u884c\u8ba1\u5212\nFROM sys.dm_exec_query_stats AS qs   \nCROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st   \nCROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp    \nWHERE SUBSTRING(st.text, (qs.statement_start_offset\/2) + 1,    \n        ((CASE statement_end_offset \n          WHEN -1 THEN DATALENGTH(st.text) \n          ELSE qs.statement_end_offset END \n         - qs.statement_start_offset)\/2) + 1) not like '%fetch%'  -- \u8fc7\u6ee4\u6761\u4ef6\nORDER BY total_elapsed_time \/ execution_count DESC;\n<\/code><\/pre>\n<p><strong>\u7528\u9014\u8bf4\u660e\uff1a<\/strong> \u5168\u9762\u5206\u6790\u6267\u884c\u65f6\u95f4\u6700\u957f\u7684\u67e5\u8be2\uff0c\u5305\u542b\u6267\u884c\u8ba1\u5212\u3002<br \/>\n<strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\u5305\u542b<code>query_plan<\/code>\u5b57\u6bb5\uff0c\u53ef\u67e5\u770b\u5177\u4f53\u6267\u884c\u8ba1\u5212<\/li>\n<li>WHERE\u6761\u4ef6\u53ef\u81ea\u5b9a\u4e49\u7b5b\u9009\uff08\u5982\uff1a<code>like '%user%'<\/code>\u67e5\u627e\u5305\u542buser\u7684SQL\uff09<\/li>\n<\/ul>\n<hr>\n<h2>6. \u6267\u884c\u6700\u6162\u7684SQL\u8bed\u53e5<\/h2>\n<pre><code class=\"language-sql\">SELECT TOP 10\n    (total_elapsed_time \/ execution_count)\/1000 N'\u5e73\u5747\u65f6\u95f4ms',    -- \u5e73\u5747\u6267\u884c\u65f6\u95f4\n    total_elapsed_time\/1000 N'\u603b\u82b1\u8d39\u65f6\u95f4ms',                     -- \u603b\u6267\u884c\u65f6\u95f4\n    total_worker_time\/1000 N'\u6240\u7528\u7684CPU\u603b\u65f6\u95f4ms',                 -- \u603bCPU\u65f6\u95f4\n    total_physical_reads N'\u7269\u7406\u8bfb\u53d6\u603b\u6b21\u6570',                       -- \u7269\u7406\u8bfb\u53d6\u6b21\u6570\n    total_logical_reads\/execution_count N'\u6bcf\u6b21\u903b\u8f91\u8bfb\u6b21\u6570',        -- \u5e73\u5747\u903b\u8f91\u8bfb\n    total_logical_reads N'\u903b\u8f91\u8bfb\u53d6\u603b\u6b21\u6570',                        -- \u903b\u8f91\u8bfb\u603b\u6b21\u6570\n    total_logical_writes N'\u903b\u8f91\u5199\u5165\u603b\u6b21\u6570',                       -- \u903b\u8f91\u5199\u603b\u6b21\u6570\n    execution_count N'\u6267\u884c\u6b21\u6570',                                  -- \u6267\u884c\u6b21\u6570\n    SUBSTRING(st.text, (qs.statement_start_offset\/2) + 1,        \n        ((CASE statement_end_offset  \n          WHEN -1 THEN DATALENGTH(st.text)  \n          ELSE qs.statement_end_offset END  \n         - qs.statement_start_offset)\/2) + 1) N'\u6267\u884c\u8bed\u53e5',        -- SQL\u8bed\u53e5\n    creation_time N'\u8bed\u53e5\u7f16\u8bd1\u65f6\u95f4',                                -- \u7f16\u8bd1\u65f6\u95f4\n    last_execution_time N'\u4e0a\u6b21\u6267\u884c\u65f6\u95f4'                           -- \u6700\u540e\u6267\u884c\u65f6\u95f4\nFROM sys.dm_exec_query_stats AS qs \nCROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st  \nWHERE SUBSTRING(st.text, (qs.statement_start_offset\/2) + 1,    \n        ((CASE statement_end_offset  \n          WHEN -1 THEN DATALENGTH(st.text)  \n          ELSE qs.statement_end_offset END  \n         - qs.statement_start_offset)\/2) + 1) not like 'fetch%'   -- \u8fc7\u6ee4\u6761\u4ef6\nORDER BY total_elapsed_time \/ execution_count DESC;\n<\/code><\/pre>\n<p><strong>\u7528\u9014\u8bf4\u660e\uff1a<\/strong> \u4e13\u95e8\u9488\u5bf9\u6267\u884c\u901f\u5ea6\u6162\u7684\u67e5\u8be2\u8fdb\u884c\u5206\u6790\u3002<br \/>\n<strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong> \u8fc7\u6ee4\u6761\u4ef6\u53ef\u6839\u636e\u9700\u8981\u8c03\u6574\u3002<\/p>\n<hr>\n<h2>7. \u6700\u8017\u65f6\u7684\u524dN\u6761T-SQL\u8bed\u53e5\uff08\u6309\u8ba1\u5212\u5206\u7ec4\uff09<\/h2>\n<pre><code class=\"language-sql\">-- \u7ed9N\u8d4b\u521d\u503c\u4e3a30    \ndeclare @n int set @n=30     \n    \n;with maco as     \n(       \n    select top (@n)    \n        plan_handle,                    -- \u6267\u884c\u8ba1\u5212\u53e5\u67c4\n        sum(total_worker_time) as total_worker_time,    -- \u603bCPU\u65f6\u95f4\n        sum(execution_count) as execution_count,        -- \u603b\u6267\u884c\u6b21\u6570\n        count(1) as sql_count                          -- \u5305\u542b\u7684SQL\u8bed\u53e5\u6570\u91cf\n    from sys.dm_exec_query_stats \n    group by plan_handle    \n    order by sum(total_worker_time) desc    \n)    \nselect  t.text,                         -- \u5b8c\u6574SQL\u6587\u672c\n        a.total_worker_time,            -- \u603bCPU\u65f6\u95f4\n        a.execution_count,              -- \u6267\u884c\u6b21\u6570\n        a.sql_count                     -- SQL\u8bed\u53e5\u6570\u91cf\nfrom    maco a    \n        cross apply sys.dm_exec_sql_text(plan_handle) t\n<\/code><\/pre>\n<p><strong>\u7528\u9014\u8bf4\u660e\uff1a<\/strong> \u6309\u6267\u884c\u8ba1\u5212\u5206\u7ec4\u5206\u6790\uff0c\u8bc6\u522b\u76f8\u5173SQL\u7684\u6574\u4f53\u6027\u80fd\u5f71\u54cd\u3002<br \/>\n<strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong> \u901a\u8fc7\u4fee\u6539<code>@n<\/code>\u503c\u63a7\u5236\u8fd4\u56de\u7ed3\u679c\u6570\u91cf\u3002<\/p>\n<hr>\n<h2>8. \u5e73\u5747\u8017CPU\u6700\u591a\u7684SQL\uff08SQL Server 2005+\uff09<\/h2>\n<pre><code class=\"language-sql\">SELECT TOP 5 \n    total_worker_time \/ execution_count AS [Avg CPU Time],  -- \u5e73\u5747CPU\u65f6\u95f4\n    SUBSTRING(st.text, (qs.statement_start_offset\/2)+1,     \n        ((CASE qs.statement_end_offset    \n            WHEN -1 THEN DATALENGTH(st.text)    \n            ELSE qs.statement_end_offset    \n            END - qs.statement_start_offset)\/2) + 1) AS statement_text  -- SQL\u8bed\u53e5\nFROM sys.dm_exec_query_stats AS qs     \nCROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st     \nORDER BY total_worker_time\/execution_count DESC\n<\/code><\/pre>\n<p><strong>\u7528\u9014\u8bf4\u660e\uff1a<\/strong> \u7b80\u5355\u5feb\u901f\u7684CPU\u6d88\u8017\u5206\u6790\u3002<br \/>\n<strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong> \u9002\u7528\u4e8eSQL Server 2005\u53ca\u4ee5\u4e0a\u7248\u672c\u3002<\/p>\n<hr>\n<h2>9. \u5e73\u5747\u8017CPU\u6700\u591a\u7684SQL\uff08SQL Server 2008+\u589e\u5f3a\u7248\uff09<\/h2>\n<pre><code class=\"language-sql\">SELECT TOP 20  \n    total_worker_time\/1000 AS [\u603b\u6d88\u8017CPU \u65f6\u95f4(ms)],          -- \u603bCPU\u65f6\u95f4\uff08\u6beb\u79d2\uff09\n    execution_count [\u8fd0\u884c\u6b21\u6570],                              -- \u6267\u884c\u6b21\u6570\n    qs.total_worker_time\/qs.execution_count\/1000 AS [\u5e73\u5747\u6d88\u8017CPU \u65f6\u95f4(ms)],  -- \u5e73\u5747CPU\u65f6\u95f4\n    last_execution_time AS [\u6700\u540e\u4e00\u6b21\u6267\u884c\u65f6\u95f4],                -- \u6700\u540e\u6267\u884c\u65f6\u95f4\n    min_worker_time \/1000 AS [\u6700\u5c0f\u6267\u884c\u65f6\u95f4(ms)],             -- \u6700\u5c0fCPU\u65f6\u95f4\n    max_worker_time \/1000 AS [\u6700\u5927\u6267\u884c\u65f6\u95f4(ms)],             -- \u6700\u5927CPU\u65f6\u95f4\n    SUBSTRING(qt.text,qs.statement_start_offset\/2+1,   \n        (CASE WHEN qs.statement_end_offset = -1   \n        THEN DATALENGTH(qt.text)   \n        ELSE qs.statement_end_offset END \n        -qs.statement_start_offset)\/2 + 1) AS [\u4f7f\u7528CPU\u7684\u8bed\u6cd5],  -- SQL\u8bed\u53e5\u7247\u6bb5\n    qt.text [\u5b8c\u6574\u8bed\u6cd5],                                      -- \u5b8c\u6574SQL\u6587\u672c\n    qt.dbid, dbname=db_name(qt.dbid),                       -- \u6570\u636e\u5e93ID\u548c\u540d\u79f0\n    qt.objectid,object_name(qt.objectid,qt.dbid) ObjectName  -- \u5bf9\u8c61ID\u548c\u540d\u79f0\nFROM sys.dm_exec_query_stats qs WITH(nolock)  \nCROSS apply sys.dm_exec_sql_text(qs.sql_handle) AS qt  \nWHERE execution_count&gt;1  -- \u53ea\u5206\u6790\u6267\u884c\u8d85\u8fc71\u6b21\u7684\u67e5\u8be2\nORDER BY (qs.total_worker_time\/qs.execution_count\/1000) DESC\n<\/code><\/pre>\n<p><strong>\u7528\u9014\u8bf4\u660e\uff1a<\/strong> \u8be6\u7ec6\u7684CPU\u6d88\u8017\u5206\u6790\uff0c\u5305\u542b\u6570\u636e\u5e93\u548c\u5bf9\u8c61\u4fe1\u606f\u3002<br \/>\n<strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong> \u9700\u8981SQL Server 2008\u53ca\u4ee5\u4e0a\u7248\u672c\u3002<\/p>\n<hr>\n<h2>10. \u603b\u8017CPU\u6700\u591a\u7684SQL<\/h2>\n<pre><code class=\"language-sql\">SELECT TOP 20  \n    total_worker_time\/1000 AS [\u603b\u6d88\u8017CPU \u65f6\u95f4(ms)],          -- \u603bCPU\u65f6\u95f4\n    execution_count [\u8fd0\u884c\u6b21\u6570],                              -- \u6267\u884c\u6b21\u6570\n    qs.total_worker_time\/qs.execution_count\/1000 AS [\u5e73\u5747\u6d88\u8017CPU \u65f6\u95f4(ms)],  -- \u5e73\u5747CPU\u65f6\u95f4\n    last_execution_time AS [\u6700\u540e\u4e00\u6b21\u6267\u884c\u65f6\u95f4],                -- \u6700\u540e\u6267\u884c\u65f6\u95f4\n    max_worker_time \/1000 AS [\u6700\u5927\u6267\u884c\u65f6\u95f4(ms)],             -- \u6700\u5927CPU\u65f6\u95f4\n    SUBSTRING(qt.text,qs.statement_start_offset\/2+1,   \n        (CASE WHEN qs.statement_end_offset = -1   \n        THEN DATALENGTH(qt.text)   \n        ELSE qs.statement_end_offset END \n        -qs.statement_start_offset)\/2 + 1) AS [\u4f7f\u7528CPU\u7684\u8bed\u6cd5],  -- SQL\u8bed\u53e5\u7247\u6bb5\n    qt.text [\u5b8c\u6574\u8bed\u6cd5],                                      -- \u5b8c\u6574SQL\u6587\u672c\n    qt.dbid, dbname=db_name(qt.dbid),                       -- \u6570\u636e\u5e93\u4fe1\u606f\n    qt.objectid,object_name(qt.objectid,qt.dbid) ObjectName  -- \u5bf9\u8c61\u4fe1\u606f\nFROM sys.dm_exec_query_stats qs WITH(nolock)  \nCROSS apply sys.dm_exec_sql_text(qs.sql_handle) AS qt  \nWHERE execution_count&gt;1  -- \u8fc7\u6ee4\u5355\u6b21\u6267\u884c\u7684\u67e5\u8be2\nORDER BY total_worker_time DESC  -- \u6309\u603bCPU\u65f6\u95f4\u6392\u5e8f\n<\/code><\/pre>\n<p><strong>\u7528\u9014\u8bf4\u660e\uff1a<\/strong> \u8bc6\u522b\u7d2f\u8ba1CPU\u6d88\u8017\u6700\u5927\u7684\u67e5\u8be2\u3002<br \/>\n<strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong> \u5173\u6ce8\u9ad8\u7d2f\u8ba1\u6d88\u8017\u5bf9\u7cfb\u7edf\u8d44\u6e90\u7684\u957f\u671f\u5f71\u54cd\u3002<\/p>\n<hr>\n<h2>11. \u5185\u5b58\u5229\u7528\u7387\u67e5\u8be2<\/h2>\n<pre><code class=\"language-sql\">SELECT\n    sqlserver_start_time,                    -- SQL Server\u542f\u52a8\u65f6\u95f4\n    (committed_kb\/1024) AS Total_Server_Memory_MB,        -- \u5df2\u63d0\u4ea4\u5185\u5b58\uff08MB\uff09\n    (committed_target_kb\/1024) AS Target_Server_Memory_MB  -- \u76ee\u6807\u5185\u5b58\uff08MB\uff09\nFROM sys.dm_os_sys_info;\n<\/code><\/pre>\n<p><strong>\u7528\u9014\u8bf4\u660e\uff1a<\/strong> \u67e5\u770bSQL Server\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3002<br \/>\n<strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong> \u5e2e\u52a9\u5224\u65ad\u5185\u5b58\u538b\u529b\u60c5\u51b5\u3002<\/p>\n<hr>\n<h2>\u901a\u7528\u6ce8\u610f\u4e8b\u9879<\/h2>\n<ol>\n<li><strong>\u6743\u9650\u8981\u6c42\uff1a<\/strong> \u9700\u8981<code>VIEW SERVER STATE<\/code>\u6743\u9650\u624d\u80fd\u8bbf\u95eeDMV<\/li>\n<li><strong>\u6570\u636e\u65f6\u6548\u6027\uff1a<\/strong> \u7edf\u8ba1\u4fe1\u606f\u5728SQL Server\u91cd\u542f\u540e\u4f1a\u91cd\u7f6e<\/li>\n<li><strong>\u6027\u80fd\u5f71\u54cd\uff1a<\/strong> \u67e5\u8be2DMV\u5bf9\u6027\u80fd\u5f71\u54cd\u8f83\u5c0f\uff0c\u4f46\u5e94\u907f\u514d\u5728\u751f\u4ea7\u9ad8\u5cf0\u9891\u7e41\u6267\u884c<\/li>\n<li><strong>\u7248\u672c\u517c\u5bb9\u6027\uff1a<\/strong> \u5404\u811a\u672c\u6807\u6ce8\u4e86\u9002\u7528\u7684SQL Server\u7248\u672c<\/li>\n<li><strong>\u7ed3\u679c\u89e3\u8bfb\uff1a<\/strong> \u9ad8\u8d44\u6e90\u6d88\u8017\u4e0d\u4e00\u5b9a\u4ee3\u8868\u6709\u95ee\u9898\uff0c\u9700\u7ed3\u5408\u4e1a\u52a1\u573a\u666f\u5206\u6790<\/li>\n<li><strong>\u8fc7\u6ee4\u6761\u4ef6\uff1a<\/strong> \u53ef\u6839\u636e\u9700\u8981\u4fee\u6539WHERE\u6761\u4ef6\u8fdb\u884c\u9488\u5bf9\u6027\u5206\u6790<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u8981 \u672c\u6587\u4ef6\u5305\u542b\u4e00\u7cfb\u5217\u7528\u4e8e SQL Server \u6027\u80fd\u6392\u67e5\u7684 SQL \u811a\u672c\uff0c\u4e3b\u8981\u7528\u4e8e\u5206\u6790\u67e5\u8be2\u6267\u884c\u7edf\u8ba1\u4fe1\u606f\uff0c\u8bc6\u522b &hellip; <a href=\"https:\/\/www.sv0.cn\/?p=40\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">SQL Server \u6027\u80fd\u6392\u67e5\u811a\u672c<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-40","post","type-post","status-publish","format-standard","hentry","category-good-codes"],"_links":{"self":[{"href":"https:\/\/www.sv0.cn\/index.php?rest_route=\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sv0.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sv0.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sv0.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sv0.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=40"}],"version-history":[{"count":4,"href":"https:\/\/www.sv0.cn\/index.php?rest_route=\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":94,"href":"https:\/\/www.sv0.cn\/index.php?rest_route=\/wp\/v2\/posts\/40\/revisions\/94"}],"wp:attachment":[{"href":"https:\/\/www.sv0.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sv0.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sv0.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}