{"id":138237,"date":"2024-10-31T13:18:24","date_gmt":"2024-10-31T05:18:24","guid":{"rendered":"https:\/\/server.hk\/cnblog\/138237\/"},"modified":"2024-10-31T13:18:25","modified_gmt":"2024-10-31T05:18:25","slug":"sql-server%e6%80%a7%e8%83%bd%e8%aa%bf%e5%84%aa%e4%ba%8b%e7%89%a9%e7%9a%84%e5%af%a6%e9%9a%9b%e6%93%8d%e4%bd%9c%e6%ad%a5%e9%a9%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/138237\/","title":{"rendered":"SQL Server\u6027\u80fd\u8abf\u512a\u4e8b\u7269\u7684\u5be6\u969b\u64cd\u4f5c\u6b65\u9a5f"},"content":{"rendered":"<h1 id=\"sql-server%e6%80%a7%e8%83%bd%e8%aa%bf%e5%84%aa%e4%ba%8b%e7%89%a9%e7%9a%84%e5%af%a6%e9%9a%9b%e6%93%8d%e4%bd%9c%e6%ad%a5%e9%a9%9f-bPBZuhNKRk\">SQL Server\u6027\u80fd\u8abf\u512a\u4e8b\u7269\u7684\u5be6\u969b\u64cd\u4f5c\u6b65\u9a5f<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0cSQL Server\u4f5c\u70ba\u4e00\u500b\u95dc\u9375\u7684\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u627f\u64d4\u8457\u5927\u91cf\u7684\u6578\u64da\u8655\u7406\u4efb\u52d9\u3002\u96a8\u8457\u6578\u64da\u91cf\u7684\u589e\u9577\uff0c\u6027\u80fd\u8abf\u512a\u8b8a\u5f97\u5c24\u70ba\u91cd\u8981\u3002\u672c\u6587\u5c07\u63a2\u8a0eSQL Server\u6027\u80fd\u8abf\u512a\u7684\u5be6\u969b\u64cd\u4f5c\u6b65\u9a5f\uff0c\u5e6b\u52a9\u7528\u6236\u63d0\u5347\u6578\u64da\u5eab\u7684\u6548\u7387\u548c\u97ff\u61c9\u901f\u5ea6\u3002<\/p>\n<h2 id=\"1-%e7%a2%ba%e5%ae%9a%e6%80%a7%e8%83%bd%e7%93%b6%e9%a0%b8-bPBZuhNKRk\">1. \u78ba\u5b9a\u6027\u80fd\u74f6\u9838<\/h2>\n<p>\u5728\u9032\u884c\u6027\u80fd\u8abf\u512a\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u78ba\u5b9a\u7cfb\u7d71\u7684\u6027\u80fd\u74f6\u9838\u3002\u53ef\u4ee5\u4f7f\u7528SQL Server\u7684\u52d5\u614b\u7ba1\u7406\u8996\u5716\uff08DMVs\uff09\u4f86\u7372\u53d6\u7cfb\u7d71\u7684\u904b\u884c\u72c0\u614b\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u67e5\u8a62\u53ef\u4ee5\u5e6b\u52a9\u8b58\u5225\u9ad8CPU\u4f7f\u7528\u7387\u7684\u67e5\u8a62\uff1a<\/p>\n<pre><code>SELECT TOP 10\n    total_worker_time\/1000 AS [CPU Time (ms)],\n    execution_count AS [Execution Count],\n    total_worker_time AS [Total CPU Time],\n    total_elapsed_time\/1000 AS [Total Elapsed Time (ms)],\n    (total_worker_time \/ execution_count) \/ 1000 AS [Avg CPU Time (ms)],\n    (total_elapsed_time \/ execution_count) \/ 1000 AS [Avg Elapsed Time (ms)],\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 END\n        - qs.statement_start_offset)\/2) + 1) AS [Query Text]\nFROM sys.dm_exec_query_stats AS qs\nCROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS qt\nORDER BY total_worker_time DESC;<\/code><\/pre>\n<h2 id=\"2-%e5%84%aa%e5%8c%96%e6%9f%a5%e8%a9%a2-bPBZuhNKRk\">2. \u512a\u5316\u67e5\u8a62<\/h2>\n<p>\u67e5\u8a62\u6027\u80fd\u662f\u5f71\u97ff\u6574\u9ad4\u7cfb\u7d71\u6027\u80fd\u7684\u4e3b\u8981\u56e0\u7d20\u4e4b\u4e00\u3002\u512a\u5316\u67e5\u8a62\u53ef\u4ee5\u5f9e\u4ee5\u4e0b\u5e7e\u500b\u65b9\u9762\u5165\u624b\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u7d22\u5f15\uff1a<\/strong>\u78ba\u4fdd\u67e5\u8a62\u4e2d\u4f7f\u7528\u7684\u5217\u6709\u9069\u7576\u7684\u7d22\u5f15\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8a62\u4f86\u6aa2\u67e5\u7d22\u5f15\u7684\u4f7f\u7528\u60c5\u6cc1\uff1a<\/li>\n<pre><code>SELECT\n        OBJECT_NAME(i.object_id) AS [Table Name],\n        i.name AS [Index Name],\n        i.type_desc AS [Index Type],\n        p.rows AS [Row Count]\n    FROM sys.indexes AS i\n    INNER JOIN sys.partitions AS p ON i.object_id = p.object_id AND i.index_id = p.index_id\n    WHERE i.is_primary_key = 0 AND i.is_unique = 0;<\/code><\/pre>\n<li><strong>\u907f\u514dSELECT *\uff1a<\/strong>\u5728\u67e5\u8a62\u4e2d\u660e\u78ba\u6307\u5b9a\u6240\u9700\u7684\u5217\uff0c\u907f\u514d\u4f7f\u7528SELECT *\uff0c\u4ee5\u6e1b\u5c11\u4e0d\u5fc5\u8981\u7684\u6578\u64da\u50b3\u8f38\u3002<\/li>\n<li><strong>\u4f7f\u7528JOIN\u800c\u975e\u5b50\u67e5\u8a62\uff1a<\/strong>\u5728\u53ef\u80fd\u7684\u60c5\u6cc1\u4e0b\uff0c\u4f7f\u7528JOIN\u4f86\u66ff\u4ee3\u5b50\u67e5\u8a62\uff0c\u9019\u6a23\u53ef\u4ee5\u63d0\u9ad8\u67e5\u8a62\u7684\u6548\u7387\u3002<\/li>\n<\/ul>\n<h2 id=\"3-%e8%aa%bf%e6%95%b4%e6%95%b8%e6%93%9a%e5%ba%ab%e9%85%8d%e7%bd%ae-bPBZuhNKRk\">3. \u8abf\u6574\u6578\u64da\u5eab\u914d\u7f6e<\/h2>\n<p>SQL Server\u7684\u914d\u7f6e\u5c0d\u6027\u80fd\u6709\u8457\u91cd\u8981\u5f71\u97ff\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5efa\u8b70\u7684\u914d\u7f6e\u8abf\u6574\uff1a<\/p>\n<ul>\n<li><strong>\u5167\u5b58\u914d\u7f6e\uff1a<\/strong>\u78ba\u4fddSQL Server\u6709\u8db3\u5920\u7684\u5167\u5b58\u53ef\u7528\u3002\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u67e5\u8a62\u6aa2\u67e5\u7576\u524d\u7684\u5167\u5b58\u4f7f\u7528\u60c5\u6cc1\uff1a<\/li>\n<pre><code>SELECT\n        physical_memory_in_use_kb\/1024 AS [Memory Used (MB)],\n        large_page_allocations_kb\/1024 AS [Large Page Allocations (MB)],\n        locked_page_allocations_kb\/1024 AS [Locked Page Allocations (MB)],\n        virtual_address_space_reserved_kb\/1024 AS [Virtual Address Space Reserved (MB)],\n        virtual_address_space_committed_kb\/1024 AS [Virtual Address Space Committed (MB)],\n        virtual_address_space_available_kb\/1024 AS [Virtual Address Space Available (MB)]\n    FROM sys.dm_os_process_memory;<\/code><\/pre>\n<li><strong>\u6700\u5927\u5167\u5b58\u8a2d\u7f6e\uff1a<\/strong>\u6839\u64da\u7cfb\u7d71\u7684\u7e3d\u5167\u5b58\uff0c\u5408\u7406\u8a2d\u7f6eSQL Server\u7684\u6700\u5927\u5167\u5b58\uff0c\u4ee5\u907f\u514d\u5f71\u97ff\u5176\u4ed6\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/li>\n<\/ul>\n<h2 id=\"4-%e7%9b%a3%e6%8e%a7%e5%92%8c%e7%b6%ad%e8%ad%b7-bPBZuhNKRk\">4. \u76e3\u63a7\u548c\u7dad\u8b77<\/h2>\n<p>\u6027\u80fd\u8abf\u512a\u662f\u4e00\u500b\u6301\u7e8c\u7684\u904e\u7a0b\uff0c\u5b9a\u671f\u76e3\u63a7\u548c\u7dad\u8b77\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002\u53ef\u4ee5\u4f7f\u7528SQL Server\u7684\u6027\u80fd\u76e3\u63a7\u5de5\u5177\u4f86\u8ddf\u8e2a\u7cfb\u7d71\u7684\u6027\u80fd\u6307\u6a19\uff0c\u4e26\u6839\u64da\u9700\u8981\u9032\u884c\u8abf\u6574\u3002\u6b64\u5916\uff0c\u5b9a\u671f\u9032\u884c\u6578\u64da\u5eab\u7684\u788e\u7247\u6574\u7406\u548c\u7d71\u8a08\u4fe1\u606f\u66f4\u65b0\u4e5f\u662f\u63d0\u9ad8\u6027\u80fd\u7684\u91cd\u8981\u6b65\u9a5f\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-bPBZuhNKRk\">\u7e3d\u7d50<\/h2>\n<p>SQL Server\u6027\u80fd\u8abf\u512a\u662f\u4e00\u500b\u7cfb\u7d71\u6027\u548c\u6301\u7e8c\u7684\u904e\u7a0b\uff0c\u901a\u904e\u78ba\u5b9a\u6027\u80fd\u74f6\u9838\u3001\u512a\u5316\u67e5\u8a62\u3001\u8abf\u6574\u6578\u64da\u5eab\u914d\u7f6e\u4ee5\u53ca\u5b9a\u671f\u76e3\u63a7\u548c\u7dad\u8b77\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u5347\u6578\u64da\u5eab\u7684\u6027\u80fd\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u80fd\u6578\u64da\u8655\u7406\u7684\u4f01\u696d\u4f86\u8aaa\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4e5f\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u90fd\u80fd\u70ba\u4f01\u696d\u63d0\u4f9b\u7a69\u5b9a\u548c\u9ad8\u6548\u7684\u6578\u64da\u8655\u7406\u80fd\u529b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u638c\u63e1SQL Server\u6027\u80fd\u8abf\u512a\u7684\u5be6\u969b\u64cd\u4f5c\u6b65\u9a5f\uff0c\u63d0\u5347\u6578\u64da\u5eab\u6548\u7387\uff0c\u512a\u5316\u67e5\u8a62\u901f\u5ea6\uff0c\u78ba\u4fdd\u7cfb\u7d71\u7a69\u5b9a\u904b\u884c\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-138237","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/138237","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=138237"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/138237\/revisions"}],"predecessor-version":[{"id":138238,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/138237\/revisions\/138238"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=138237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=138237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=138237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}