{"id":59,"date":"2026-07-25T09:00:00","date_gmt":"2026-07-25T01:00:00","guid":{"rendered":"https:\/\/fsdata.site\/?p=59"},"modified":"2026-07-23T23:04:15","modified_gmt":"2026-07-23T15:04:15","slug":"sql-server-%e6%80%a7%e8%83%bd%e4%bc%98%e5%8c%96%ef%bc%9a10-%e4%b8%aa%e5%ae%9e%e6%88%98%e6%8a%80%e5%b7%a7%ef%bc%88%e9%99%84%e8%84%9a%e6%9c%ac%ef%bc%89","status":"publish","type":"post","link":"https:\/\/fsdata.site\/?p=59","title":{"rendered":"SQL Server \u6027\u80fd\u4f18\u5316\uff1a10 \u4e2a\u5b9e\u6218\u6280\u5de7\uff08\u9644\u811a\u672c\uff09"},"content":{"rendered":"<h2>\u4e00\u3001\u5148\u8bf4\u7ed3\u8bba\uff1a\u5927\u90e8\u5206\u6162\u67e5\u8be2\u90fd\u80fd\u9760\u7d22\u5f15\u89e3\u51b3<\/h2>\n<p>\u5728 SQL Server \u7684\u8fd0\u7ef4\u91cc\uff0c80% \u7684\u6027\u80fd\u95ee\u9898\u6765\u81ea\u7f3a\u5931\u6216\u9519\u8bef\u7684\u7d22\u5f15\u3002\u672c\u6587\u6574\u7406\u4e86 10 \u4e2a\u5b9e\u6218\u4e2d\u6700\u5e38\u7528\u3001\u89c1\u6548\u6700\u5feb\u7684\u4f18\u5316\u6280\u5de7\uff0c\u5168\u90e8\u57fa\u4e8e\u771f\u5b9e\u751f\u4ea7\u73af\u5883\u3002<\/p>\n<h2>\u4e8c\u3001\u7d22\u5f15\u7bc7<\/h2>\n<h3>1. \u7528\u6267\u884c\u8ba1\u5212\u627e\u7f3a\u5931\u7d22\u5f15<\/h3>\n<p>\u5728 SSMS \u91cc\u6253\u5f00&#8221;\u663e\u793a\u5b9e\u9645\u6267\u884c\u8ba1\u5212&#8221;\uff0c\u8dd1\u4e00\u6761\u6162\u67e5\u8be2\uff0cSQL Server \u4f1a\u5728\u8ba1\u5212\u91cc\u76f4\u63a5\u63d0\u793a <em>Missing Index<\/em>\u3002\u7167\u7740\u5efa\u8bae\u5efa\uff0c\u5f80\u5f80\u7acb\u7aff\u89c1\u5f71\u3002<\/p>\n<pre><code>-- \u67e5\u770b\u7cfb\u7edf\u4e2d\u88ab\u5efa\u8bae\u7684\u7d22\u5f15\nSELECT * FROM sys.dm_db_missing_index_details;\nSELECT * FROM sys.dm_db_missing_index_groups;\n<\/code><\/pre>\n<h3>2. \u590d\u5408\u7d22\u5f15\u7684\u5217\u987a\u5e8f<\/h3>\n<p>\u628a<strong>\u7b49\u503c\u67e5\u8be2\u7684\u5217\u653e\u524d\u9762\uff0c\u8303\u56f4\u67e5\u8be2\u7684\u5217\u653e\u540e\u9762<\/strong>\u3002\u6bd4\u5982 <code>WHERE status=1 AND create_time&gt;'2026-01-01'<\/code>\uff0c\u7d22\u5f15\u5e94\u662f <code>(status, create_time)<\/code>\uff0c\u53cd\u8fc7\u6765\u5c31\u8d70\u4e0d\u5168\u3002<\/p>\n<h3>3. \u907f\u514d\u7d22\u5f15\u5931\u6548\u7684\u51fd\u6570\u5305\u88f9<\/h3>\n<p>\u5728 WHERE \u91cc\u5bf9\u5217\u5957\u51fd\u6570\u4f1a\u5bfc\u81f4\u7d22\u5f15\u5931\u6548\uff1a<\/p>\n<pre><code>-- \u9519\u8bef\uff1a\u7d22\u5f15\u7528\u4e0d\u4e0a\nSELECT * FROM orders WHERE CONVERT(varchar, create_time, 23)='2026-07-01';\n-- \u6b63\u786e\uff1a\u8303\u56f4\u67e5\u8be2\nSELECT * FROM orders WHERE create_time &gt;= '2026-07-01' AND create_time &lt; '2026-07-02';\n<\/code><\/pre>\n<h2>\u4e09\u3001\u67e5\u8be2\u5199\u6cd5\u7bc7<\/h2>\n<h3>4. \u5c11\u7528 SELECT *<\/h3>\n<p>\u53ea\u53d6\u9700\u8981\u7684\u5217\uff0c\u65e2\u80fd\u51cf\u5c11 IO\uff0c\u53c8\u6709\u53ef\u80fd\u547d\u4e2d<strong>\u8986\u76d6\u7d22\u5f15<\/strong>\uff08index only scan\uff09\uff0c\u8fde\u56de\u8868\u90fd\u7701\u4e86\u3002<\/p>\n<h3>5. \u8b66\u60d5 implicit conversion\uff08\u9690\u5f0f\u8f6c\u6362\uff09<\/h3>\n<p>\u5b57\u6bb5\u662f varchar \u4f46\u4f20\u5165 nvarchar \u53c2\u6570\uff0c\u6216\u53cd\u4e4b\uff0c\u4f1a\u89e6\u53d1\u5168\u8868\u626b\u63cf\u3002\u786e\u4fdd\u53c2\u6570\u7c7b\u578b\u4e0e\u5217\u7c7b\u578b\u4e00\u81f4\u3002<\/p>\n<h3>6. \u5206\u9875\u7528 OFFSET FETCH \u66ff\u4ee3\u8001\u5199\u6cd5<\/h3>\n<pre><code>-- SQL Server 2012+ \u63a8\u8350\nSELECT id, name FROM users\nORDER BY id\nOFFSET 10000 ROWS FETCH NEXT 20 ROWS ONLY;\n<\/code><\/pre>\n<h2>\u56db\u3001\u7edf\u8ba1\u4fe1\u606f\u4e0e\u7ef4\u62a4<\/h2>\n<h3>7. \u66f4\u65b0\u7edf\u8ba1\u4fe1\u606f<\/h3>\n<p>\u6570\u636e\u5927\u5e45\u53d8\u52a8\u540e\uff0c\u65e7\u7edf\u8ba1\u4fe1\u606f\u4f1a\u8ba9\u4f18\u5316\u5668\u9009\u9519\u6267\u884c\u8ba1\u5212\uff1a<\/p>\n<pre><code>UPDATE STATISTICS table_name WITH FULLSCAN;\n<\/code><\/pre>\n<h3>8. \u5b9a\u671f\u91cd\u5efa\/\u91cd\u7ec4\u7d22\u5f15<\/h3>\n<p>\u788e\u7247\u7387 &gt;30% \u91cd\u5efa\uff0c5%-30% \u91cd\u7ec4\uff1a<\/p>\n<pre><code>ALTER INDEX IX_xxx ON table_name REBUILD;\nALTER INDEX IX_xxx ON table_name REORGANIZE;\n<\/code><\/pre>\n<h2>\u4e94\u3001\u67b6\u6784\u7bc7<\/h2>\n<h3>9. \u5927\u8868\u52a0\u7d22\u5f15\u8981\u5728\u4f4e\u5cf0\u671f<\/h3>\n<p>\u7ebf\u4e0a\u5927\u8868\u5efa\u7d22\u5f15\u4f1a\u9501\u8868\uff0cSQL Server \u4f01\u4e1a\u7248\u652f\u6301 ONLINE=ON\uff0c\u6807\u51c6\u7248\u8bf7\u52a1\u5fc5\u5728\u7ef4\u62a4\u7a97\u53e3\u64cd\u4f5c\u3002<\/p>\n<h3>10. \u51b7\u70ed\u6570\u636e\u5206\u79bb<\/h3>\n<p>\u628a\u51e0\u5e74\u524d\u7684\u5386\u53f2\u8ba2\u5355\u8fc1\u5230\u5f52\u6863\u8868\/\u5f52\u6863\u5e93\uff0c\u4e3b\u8868\u4f53\u79ef\u4e0b\u53bb\uff0c\u6240\u6709\u67e5\u8be2\u90fd\u53d8\u5feb\u3002<\/p>\n<h2>\u7ed3\u8bed<\/h2>\n<p>\u4f18\u5316\u4e0d\u662f\u4e00\u8e74\u800c\u5c31\uff0c\u5efa\u8bae\u5efa\u4e00\u4e2a<strong>\u6162\u67e5\u8be2\u76d1\u63a7<\/strong>\uff1a\u5b9a\u671f\u6293 <code>sys.dm_exec_query_stats<\/code> \u91cc\u8017\u65f6 TOP 20 \u7684\u8bed\u53e5\uff0c\u9010\u4e2a\u51fb\u7834\u3002\u6301\u7eed\u505a\uff0c\u7cfb\u7edf\u81ea\u7136\u7a33\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6574\u7406 SQL Server \u6700\u5e38\u7528\u89c1\u6548\u6700\u5feb\u7684 10 \u4e2a\u4f18\u5316\u6280\u5de7\uff1a\u7d22\u5f15\u3001\u6267\u884c\u8ba1\u5212\u3001\u7edf\u8ba1\u4fe1\u606f\u3001\u51b7\u70ed\u5206\u79bb\uff0c\u9644\u53ef\u76f4\u63a5\u6284\u7684 T-SQL\u3002<\/p>\n","protected":false},"author":2,"featured_media":101,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-59","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/posts\/59","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/fsdata.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=59"}],"version-history":[{"count":1,"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions"}],"predecessor-version":[{"id":102,"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions\/102"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/media\/101"}],"wp:attachment":[{"href":"https:\/\/fsdata.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=59"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fsdata.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=59"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fsdata.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}