{"id":667,"date":"2012-11-16T01:14:53","date_gmt":"2012-11-15T17:14:53","guid":{"rendered":"http:\/\/blog.infinity.idv.tw\/index.php\/2012\/11\/16\/sql-server-%e8%87%aa%e5%8b%95%e5%88%a4%e5%88%a5%e7%b4%a2%e5%bc%95%e6%98%af%e5%90%a6%e9%9c%80%e8%a6%81%e9%87%8d%e5%bb%bat-sql%e8%88%87%e9%87%8d%e5%bb%ba%e7%b4%a2%e5%bc%95%e6%96%b9%e5%bc%8f\/"},"modified":"2012-11-16T01:14:53","modified_gmt":"2012-11-15T17:14:53","slug":"sql-server-%e8%87%aa%e5%8b%95%e5%88%a4%e5%88%a5%e7%b4%a2%e5%bc%95%e6%98%af%e5%90%a6%e9%9c%80%e8%a6%81%e9%87%8d%e5%bb%bat-sql%e8%88%87%e9%87%8d%e5%bb%ba%e7%b4%a2%e5%bc%95%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"http:\/\/blog.infinity.idv.tw\/?p=667","title":{"rendered":"SQL Server \u81ea\u52d5\u5224\u5225\u7d22\u5f15\u662f\u5426\u9700\u8981\u91cd\u5efaT-SQL\u8207\u91cd\u5efa\u7d22\u5f15\u65b9\u5f0f"},"content":{"rendered":"<p>\u53c3\u8003\u7db2\u5740\uff1ahttp:\/\/blog.miniasp.com\/post\/2009\/01\/18\/Let-SQL-Server-Tell-You-Which-Indexes-to-Rebuild-or-Reorganize.aspx<\/p>\n<p>\u81ea\u52d5\u67e5\u8a62\u8207\u5efa\u8b70\u7d22\u5f15\u91cd\u5efa\u4e4bT-SQL\u8a9e\u6cd5\uff1a<\/p>\n<p>SELECT &#8216;ALTER INDEX [&#8216; + ix.name + &#8216;] ON [&#8216; + s.name + &#8216;].[&#8216; + t.name + &#8216;] &#8216; +<\/p>\n<p>CASE<\/p>\n<p>WHEN ps.avg_fragmentation_in_percent &gt; 15<\/p>\n<p>THEN &#8216;REBUILD&#8217;<\/p>\n<p>ELSE &#8216;REORGANIZE&#8217;<\/p>\n<p>END +<\/p>\n<p>CASE<\/p>\n<p>WHEN pc.partition_count &gt; 1<\/p>\n<p>THEN &#8216; PARTITION = &#8216; + CAST(ps.partition_number AS nvarchar(MAX))<\/p>\n<p>ELSE &#8221;<\/p>\n<p>END,<\/p>\n<p>avg_fragmentation_in_percent<\/p>\n<p>FROM sys.indexes AS ix<\/p>\n<p>INNER JOIN sys.tables t<\/p>\n<p>ON t.object_id = ix.object_id<\/p>\n<p>INNER JOIN sys.schemas s<\/p>\n<p>ON t.schema_id = s.schema_id<\/p>\n<p>INNER JOIN<\/p>\n<p>(SELECT object_id ,<\/p>\n<p>index_id ,<\/p>\n<p>avg_fragmentation_in_percent,<\/p>\n<p>partition_number<\/p>\n<p>FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL)<\/p>\n<p>) ps<\/p>\n<p>ON t.object_id = ps.object_id<\/p>\n<p>AND ix.index_id = ps.index_id<\/p>\n<p>INNER JOIN<\/p>\n<p>(SELECT object_id,<\/p>\n<p>index_id ,<\/p>\n<p>COUNT(DISTINCT partition_number) AS partition_count<\/p>\n<p>FROM sys.partitions<\/p>\n<p>GROUP BY object_id,<\/p>\n<p>index_id<\/p>\n<p>) pc<\/p>\n<p>ON t.object_id = pc.object_id<\/p>\n<p>AND ix.index_id = pc.index_id<\/p>\n<p>WHERE ps.avg_fragmentation_in_percent &gt; 10<\/p>\n<p>AND ix.name IS NOT NULL<\/p>\n<p>\u67e5\u8a62\u65b9\u5f0f\uff1a<\/p>\n<p>\u57f7\u884c SQL Server Management Studio Express \u9078\u6587\u5b57\u65b9\u5f0f\u5448\u73fe\u67e5\u8a62<\/p>\n<p><a href=\"http:\/\/blog.infinity.idv.tw\/blog-img\/SQLServerTSQL_116D\/clip_image002.jpg\"><img loading=\"lazy\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"clip_image002\" border=\"0\" alt=\"clip_image002\" src=\"http:\/\/blog.infinity.idv.tw\/blog-img\/SQLServerTSQL_116D\/clip_image002_thumb.jpg\" width=\"815\" height=\"367\" \/><\/a><\/p>\n<p>\u67e5\u8a62\u5f8c\u5efa\u8b70 \u7d22\u5f15\u91cd\u5efa\u7684\u5217\u8868\u8207\u788e\u7247\u72c0\u6cc1(\u6b64\u70ba\u6c92\u6709\u6587\u5b57\u6a21\u5f0f\u5448\u73fe\u67e5\u8a62\u7d50\u679c \u4e0d\u597d\u901a\u901a\u8907\u88fd)<\/p>\n<p><a href=\"http:\/\/blog.infinity.idv.tw\/blog-img\/SQLServerTSQL_116D\/clip_image003.jpg\"><img loading=\"lazy\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"clip_image003\" border=\"0\" alt=\"clip_image003\" src=\"http:\/\/blog.infinity.idv.tw\/blog-img\/SQLServerTSQL_116D\/clip_image003_thumb.jpg\" width=\"503\" height=\"400\" \/><\/a><\/p>\n<p>\u6587\u5b57\u6a21\u5f0f\u67e5\u8a62\u51fa\u4f86\u5f8c\u7528 alt + \u6ed1\u9f20 \u53ef\u505a\u5340\u584a\u9078\u53d6\u6a21\u5f0f<\/p>\n<p><a href=\"http:\/\/blog.infinity.idv.tw\/blog-img\/SQLServerTSQL_116D\/clip_image005.jpg\"><img loading=\"lazy\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"clip_image005\" border=\"0\" alt=\"clip_image005\" src=\"http:\/\/blog.infinity.idv.tw\/blog-img\/SQLServerTSQL_116D\/clip_image005_thumb.jpg\" width=\"585\" height=\"313\" \/><\/a><\/p>\n<p>\u628a\u526a\u4e0b\u7684\u6307\u4ee4\u53e6\u958b\u67e5\u8a62\u5c31\u53ef\u4ee5\u958b\u59cb\u91cd\u5efa\u4e86\u3002<\/p>\n<p><a href=\"http:\/\/blog.infinity.idv.tw\/blog-img\/SQLServerTSQL_116D\/clip_image007.jpg\"><img loading=\"lazy\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"clip_image007\" border=\"0\" alt=\"clip_image007\" src=\"http:\/\/blog.infinity.idv.tw\/blog-img\/SQLServerTSQL_116D\/clip_image007_thumb.jpg\" width=\"760\" height=\"350\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53c3\u8003\u7db2\u5740\uff1ahttp:\/\/blog.miniasp.com\/post\/2009\/01\/18\/Let-SQL-Server-Tell-You-Which-Indexes-to-Rebuild-or-Reorganize.aspx<\/p>\n<p>\u81ea\u52d5\u67e5\u8a62\u8207\u5efa\u8b70\u7d22\u5f15\u91cd\u5efa\u4e4bT-SQL\u8a9e\u6cd5\uff1a<\/p>\n<p>SELECT &#8216;ALTER INDEX [&#8216; + ix.name + &#8216;] ON [&#8216; + s.name + &#8216;].[&#8216; + t.name + &#8216;] &#8216; +<\/p>\n<p>CASE<\/p>\n<p>WHEN ps.avg_fragmentation_in_percent &gt; 15<\/p>\n<p>THEN &#8216;REBUILD&#8217;<\/p>\n<p>ELSE &#8216;REORGANIZE&#8217;<\/p>\n<p>END +<\/p>\n<p>CASE<\/p>\n<p>WHEN pc.partition_count &gt; 1<\/p>\n<p>THEN &#8216; PARTITION = &#8216; + CAST(ps.partition_number AS nvarchar(MAX))<\/p>\n<p>ELSE &#8221;<\/p>\n<p>END,<\/p>\n<p>avg_fragmentation_in_percent<\/p>\n<p>FROM sys.indexes AS ix<\/p>\n<p>INNER JOIN sys.tables t<\/p>\n<p>ON t.object_id = ix.object_id<\/p>\n<p>INNER JOIN sys.schemas s<\/p>\n<p>ON t.schema_id = s.schema_id<\/p>\n<p>INNER JOIN<\/p>\n<p>(SELECT object_id [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[27],"tags":[432],"_links":{"self":[{"href":"http:\/\/blog.infinity.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/667"}],"collection":[{"href":"http:\/\/blog.infinity.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.infinity.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.infinity.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.infinity.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=667"}],"version-history":[{"count":0,"href":"http:\/\/blog.infinity.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/667\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.infinity.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.infinity.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=667"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.infinity.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}