{"id":1723,"date":"2011-05-15T01:57:01","date_gmt":"2011-05-15T01:57:01","guid":{"rendered":"http:\/\/enjoyasp.net\/?p=1723"},"modified":"2011-06-30T08:06:35","modified_gmt":"2011-06-30T08:06:35","slug":"%e7%b4%a2%e5%bc%95%e7%9a%84%e7%bb%b4%e6%8a%a4","status":"publish","type":"post","link":"https:\/\/enjoyasp.net\/index.php\/2011\/05\/15\/%e7%b4%a2%e5%bc%95%e7%9a%84%e7%bb%b4%e6%8a%a4\/","title":{"rendered":"\u7d22\u5f15\u7ef4\u62a41-\u7d22\u5f15\u788e\u7247\u6574\u7406\uff1a.dm_db_index_physical_stats"},"content":{"rendered":"<pre escaped=\"true\" lang=\"tsql\">\r\n \u7d22\u5f15\u788e\u7247\uff1a1,\u5f53\u7d22\u5f15\u5305\u542b\u7684\u9875\u4e2d\u7684\u903b\u8f91\u987a\u5e8f\u4e0e\u6570\u636e\u6587\u4ef6\u4e2d\u7684\u7269\u7406\u6392\u5e8f\u4e0d\u5339\u914d\u65f6\uff0c\u5c31\u5b58\u5728\u788e\u7247\uff0c\u788e\u7247\u975e\u5e38\u591a\u7684\u7d22\u5f15\u4f1a\u964d\u4f4e\u67e5\u8be2\u7684\u6027\u80fd\uff0c\u5bfc\u81f4\u5e94\u7528\u7a0b\u5e8f\u54cd\u5e94\u7f13\u6162\uff0c\u53ef\u901a\u8fc7\u91cd\u65b0\u7ec4\u7ec7\u7d22\u5f15\u6216\u91cd\u65b0\u751f\u6210\u7d22\u5f15\u6765\u4fee\u590d\u7d22\u5f15\u788e\u7247\u3002\r\n\r\n2,\u5f53\u7d22\u5f15\u9875\u4e0d\u5728\u903b\u8f91\u987a\u5e8f\u4e0a\u65f6\u5c31\u4f1a\u4ea7\u751f\u5916\u90e8\u788e\u7247\u3002\u7d22\u5f15\u521b\u5efa\u65f6\uff0c\u7d22\u5f15\u952e\u6309\u7167\u903b\u8f91\u987a \u5e8f\u653e\u5728\u4e00\u7ec4\u7d22\u5f15\u9875\u4e0a\u3002\u5f53\u65b0\u6570\u636e\u63d2\u5165\u7d22\u5f15\u65f6\uff0c\u65b0\u7684\u952e\u53ef\u80fd\u653e\u5728\u5b58\u5728\u7684\u952e\u4e4b\u95f4\u3002\u4e3a \u4e86\u8ba9\u65b0\u7684\u952e\u6309\u7167\u6b63\u786e\u7684\u987a\u5e8f\u63d2\u5165\uff0c \u53ef\u80fd\u4f1a\u521b\u5efa\u65b0\u7684\u7d22\u5f15\u9875\u6765\u5b58\u50a8\u9700\u8981\u79fb\u52a8\u7684\u90a3\u4e9b \u5b58\u5728\u7684\u952e\u3002\u8fd9\u4e9b\u65b0\u7684\u7d22\u5f15\u9875\u901a\u5e38\u7269\u7406\u4e0a\u4e0d\u4f1a\u548c\u90a3\u4e9b\u88ab\u79fb\u52a8\u7684\u952e\u539f\u6765\u6240\u5728\u7684\u9875\u76f8 \u90bb\u3002\u521b\u5efa\u65b0\u9875\u7684\u8fc7\u7a0b\u4f1a\u5f15\u8d77\u7d22\u5f15\u9875\u504f\u79bb\u903b\u8f91\u987a\u5e8f\u3002\r\n\r\n 3,\u5728\u6709\u7279\u5b9a\u641c\u7d22\u6216\u8005\u8fd4\u56de\u65e0\u5e8f\u7ed3\u679c\u96c6\u7684\u67e5\u8be2\u7684\u60c5\u51b5\u4e0b\uff0c \u504f\u79bb\u987a\u5e8f\u7684\u7d22\u5f15\u9875\u4e0d\u4f1a\u5f15\u8d77 \u95ee\u9898\u3002 \u5bf9\u4e8e\u8fd4\u56de\u6709\u5e8f\u7ed3\u679c\u96c6\u7684\u67e5\u8be2\uff0c \u641c\u7d22\u90a3\u4e9b\u65e0\u5e8f\u7684\u7d22\u5f15\u9875\u9700\u8981\u8fdb\u884c\u989d\u5916\u7684\u5904\u7406\u3002\u9020\u6210\u6027\u80fd\u4f4e\u4e0b\u3002\r\n\u5373\uff1a\u5728\u521d\u5efa\u65f6\u7d22\u5f15\u662f\u6709\u5e8f\u7684\uff0c\u4f46\u4e0d\u65ad\u7684update,insert,delete\u64cd\u4f5c\u4f7f\u5f97\u7d22\u5f15\u9875\u53d8\u5f97\u5c40\u90e8\u65e0\u5e8f\uff0c\u4f46\u8981\u6c42\u8fd4\u56de\u6709\u5e8f\u7684\u7ed3\u679c\u96c6\u65f6\uff0c\u4e0d\u5f97\u4e0d\u6d6a\u8d39\u989d\u5916\u65f6\u95f4\u8fdb\u884c\u6392\u5e8f\u3002\u800c\u7d22\u5f15\u7ef4\u62a4\u7684\u8fc7\u7a0b\u5c31\u662f\u5c06\u6392\u5e8f\u5f52\u4e3a\u6709\u5e8f\u7684\u8fc7\u7a0b\r\n\r\n\r\n \u7d22\u5f15\u72b6\u51b5\u68c0\u67e5:dm_db_index_physical_stats\r\n \u5f53avg_fragmentation_in_percent\u788e\u7247\u7387 &lt;5%\u65f6\uff0c\u662f\u6b63\u5e38\u7ea7\u522b\r\n \u5f53avg_fragmentation_in_percent\u788e\u7247\u7387 &gt;=5% AND &lt;=30%\u65f6\uff0c\u662f\u8f7b\u5ea6\u7ea7\u522b\uff0c\u9700\u8981\u91cd\u65b0\u7ec4\u7ec7\u7d22\u5f15\r\n \u5f53avg_fragmentation_in_percent\u788e\u7247\u7387 &gt;30%\u65f6\uff0c\u662f\u4e25\u91cd\u7ea7\u522b\uff0c\u9700\u8981\u91cd\u5efa\u7d22\u5f15\r\n alert index \u7d22\u5f15\u540d\u79f0 ON \u67b6\u6784\u540d\u79f0.\u8868\u540d reorganize\/rebulid partition = \u5206\u533a\u53f7\r\n sys.objects:\u5b58\u50a8\u8868\u3001\u89c6\u56fe\u3001\u5b58\u50a8\u8fc7\u7a0b\r\n\r\n\u67e5\u770b\uff1a\r\nSELECT *\r\nFROM SYS.dm_db_index_physical_stats(DB_ID(), object_id('bmdcustomer'),NULL,NULL,'LIMITED') DDIPS\r\n\r\n1\uff0c\u7d22\u5f15\u72b6\u6001\u68c0\u67e5\u4e0e\u7ef4\u62a4\r\nSELECT s.name schema_name,o.name OBJECT_name,ix.name INDEX_name,DDIPS.avg_fragmentation_in_percent \u788e\u7247\u7387,\r\n\tCASE WHEN avg_fragmentation_in_percent &gt; 30 THEN '\u4e25\u91cd\u788e\u7247\u7ea7\u522b\uff0c\u7d22\u5f15\u9700\u8981\u91cd\u5efa'\r\n\t     WHEN avg_fragmentation_in_percent &gt;= 5 AND avg_fragmentation_in_percent &lt; 30 THEN '\u8f7b\u5ea6\u788e\u7247\u7ea7\u522b\uff0c\u7d22\u5f15\u9700\u8981\u91cd\u65b0\u7ec4\u7ec7' \r\n\t\t ELSE '\u6b63\u5e38\u72b6\u6001' END \u63d0\u793a\r\n\tFROM SYS.dm_db_index_physical_stats(DB_ID(),NULL,NULL,NULL,'LIMITED') DDIPS\r\n\tJOIN SYS.objects o ON DDIPS.[object_id] = O.[object_id] --\u5f97\u5230\u8868\u540d\r\n\tJOIN SYS.schemas s ON S.[schema_id] = O.[schema_id] --\u5f97\u5230\u8868\u7684\u67b6\u6784\u540d\r\n\tJOIN SYS.indexes IX ON DDIPS.[object_id] = IX.[object_id] AND DDIPS.index_id = IX.index_id --\u5f97\u5230\u7d22\u5f15\u540d\u79f0\r\nWHERE ddips.[object_id] = object_id('bdcustomerallocate') AND s.[schema_id] = SCHEMA_ID('dbo')  \r\n\r\n\/*\r\n* 1,\u91cd\u65b0\u7ec4\u7ec7\u7d22\u5f15\uff1aalter index IX_bdCustomerAllocate ON dbo.bdCustomerAllocate reorganize\r\n* 2,\u91cd\u5efa\u7d22\u5f15\uff1aalter index PK_bdCustomerAllocate ON dbo.bdCustomerAllocate rebulid\r\n* *\/\r\n\r\n2\uff0c\u7d22\u5f15\u72b6\u6001\u6279\u91cf\u7ef4\u62a4\r\n\/*\u7d22\u5f15\u7ef4\u62a4:\u68c0\u67e5\u6570\u636e\u5e93\u4e2d\u6240\u6709\u7d22\u5f15\uff0c\u4fee\u590d\u6709\u95ee\u9898\u7684\u7d22\u5f15\r\n* alert index \u7d22\u5f15\u540d\u79f0 ON \u67b6\u6784\u540d\u79f0.\u8868\u540d reorganize\/rebulid partition = \u5206\u533a\u53f7\r\n* sys.partitions:\u6570\u636e\u5e93\u4e2d\u6240\u6709\u8868\u548c\u7d22\u5f15\u7684\u6bcf\u4e2a\u5206\u533a\u5728\u8868\u4e2d\u5404\u5bf9\u5e94\u4e00\u884c\r\n* sys.objects:\u5b58\u50a8\u8868\u3001\u89c6\u56fe\u3001\u5b58\u50a8\u8fc7\u7a0b\r\n* \r\n* *\/\r\nDECLARE cur_dx CURSOR FOR\r\n\r\nWITH PT AS(\r\nSELECT p.[object_id],p.index_id, COUNT(1) partition_count\r\nFROM SYS.partitions p\r\nGROUP BY p.[object_id],p.index_id),\r\n\r\nDIX AS(\r\n\tSELECT DDIPS.[object_id],DDIPS.index_id,DDIPS.partition_number,DDIPS.avg_fragmentation_in_percent,\r\n\to.name OBJECT_name,s.name schema_name,ix.name INDEX_name,pt.partition_count,\r\n\tCASE WHEN avg_fragmentation_in_percent &gt; 30 THEN '\u4e25\u91cd\u788e\u7247\u7ea7\u522b\uff0c\u7d22\u5f15\u9700\u8981\u91cd\u5efa'\r\n\t     WHEN avg_fragmentation_in_percent &gt;= 5 AND avg_fragmentation_in_percent &lt; 30 THEN '\u8f7b\u5ea6\u788e\u7247\u7ea7\u522b\uff0c\u7d22\u5f15\u9700\u8981\u91cd\u65b0\u7ec4\u7ec7' END warning\r\n\t\r\n\tFROM SYS.dm_db_index_physical_stats(DB_ID(),NULL,NULL,NULL,'LIMITED') DDIPS\r\n\tJOIN SYS.objects o ON DDIPS.[object_id] = O.[object_id] --\u5f97\u5230\u8868\u540d\r\n\tJOIN SYS.schemas s ON S.[schema_id] = O.[schema_id] --\u5f97\u5230\u8868\u7684\u67b6\u6784\u540d\r\n\tJOIN SYS.indexes IX ON DDIPS.[object_id] = IX.[object_id] AND DDIPS.index_id = IX.index_id --\u5f97\u5230\u7d22\u5f15\u540d\u79f0\r\n\tJOIN PT ON DDIPS.[object_id] = PT.object_id AND DDIPS.index_id = pt.index_id --\u5f97\u5230\u5206\u533a\u53f7\r\n\tWHERE ddips.avg_fragmentation_in_percent &gt; 5 AND DDIPS.index_id &gt; 0\t\r\n\tAND ddips.[object_id]=OBJECT_ID('bdcustomerallocate')\r\n),\r\nDIXSQL AS(\r\n\tSELECT ' ALTER INDEX ' + QUOTENAME(INDEX_NAME)\r\n\t+ ' ON ' + QUOTENAME(schema_name) + '.' + QUOTENAME(OBJECT_NAME)\r\n\t+ CASE WHEN avg_fragmentation_in_percent &lt; 30 THEN ' REORGANIZE '\r\n\t  ELSE ' REBUILD ' END \r\n\t+ CASE WHEN partition_count &gt; 1\r\n\tTHEN ' PARTITION =  ' + CONVERT(NVARCHAR(20),partition_number)\r\n\tELSE '' END  SQL\r\n\tFROM DIX\r\n) \r\nSELECT * FROM DIXSQL\r\n\r\nDECLARE @sql NVARCHAR(max)\r\nOPEN cur_dx\r\nFETCH cur_dx INTO @sql\r\nWHILE @@FETCH_STATUS = 0\r\nBEGIN\r\n\tEXEC sp_executesql @sql\r\n\tRAISERROR('Executed:%s',10,1,@sql) WITH NOWAIT\r\n\tFETCH cur_dx INTO @sql\t\r\nEND \r\nCLOSE cur_dx\r\nDEALLOCATE cur_dx\r\n\r\n--ALTER INDEX [PK_bdCustomerAllocate] ON [dbo].[bdCustomerAllocate]   REBUILD <\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7d22\u5f15\u788e\u7247\uff1a1,\u5f53\u7d22\u5f15\u5305\u542b\u7684\u9875\u4e2d\u7684\u903b\u8f91\u987a\u5e8f\u4e0e\u6570\u636e\u6587\u4ef6\u4e2d\u7684\u7269\u7406\u6392\u5e8f\u4e0d\u5339\u914d\u65f6\uff0c\u5c31\u5b58\u5728\u788e\u7247\uff0c\u788e\u7247\u975e\u5e38\u591a\u7684\u7d22\u5f15\u4f1a\u964d\u4f4e\u67e5\u8be2 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-1723","post","type-post","status-publish","format-standard","hentry","category-sql-server"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/1723","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/comments?post=1723"}],"version-history":[{"count":0,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/1723\/revisions"}],"wp:attachment":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/media?parent=1723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/categories?post=1723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/tags?post=1723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}