{"id":1730,"date":"2011-05-16T00:39:16","date_gmt":"2011-05-16T00:39:16","guid":{"rendered":"http:\/\/enjoyasp.net\/?p=1730"},"modified":"2011-09-01T23:26:30","modified_gmt":"2011-09-01T23:26:30","slug":"%e7%b4%a2%e5%bc%95%e7%bb%b4%e6%8a%a42-%e7%b4%a2%e5%bc%95%e7%8a%b6%e6%80%81%e6%9f%a5%e7%9c%8b%ef%bc%9asys-dm_db_index_usage_stats","status":"publish","type":"post","link":"https:\/\/enjoyasp.net\/index.php\/2011\/05\/16\/%e7%b4%a2%e5%bc%95%e7%bb%b4%e6%8a%a42-%e7%b4%a2%e5%bc%95%e7%8a%b6%e6%80%81%e6%9f%a5%e7%9c%8b%ef%bc%9asys-dm_db_index_usage_stats\/","title":{"rendered":"\u7d22\u5f15\u7ef4\u62a42-\u7d22\u5f15\u72b6\u6001\u67e5\u770b\uff1asys.dm_db_index_usage_stats"},"content":{"rendered":"<pre escaped=\"true\" lang=\"tsql\">\r\nsys.dm_db_index_usage_stats\r\n\u52a8\u6001\u7ba1\u7406\u89c6\u56fe sys.dm_db_index_usage_stats \u8be5\u89c6\u56fe\u8fd4\u56de\u4e0d\u540c\u7c7b\u578b\u7d22\u5f15\u64cd\u4f5c\u7684\u8ba1\u6570\u4ee5\u53ca \u4e0a\u6b21\u6267\u884c\u6bcf\u79cd\u64cd\u4f5c\u7684\u65f6\u95f4\u3002\r\n\u53ea\u8981\u542f\u52a8 SQL Server (MSSQLSERVER) \u670d\u52a1\uff0c\u8ba1\u6570\u5668\u5c31\u521d\u59cb\u5316\u4e3a\u7a7a\u3002\u800c\u4e14\uff0c\u5f53\u5206\u79bb\u6216\u5173\u95ed\u6570\u636e\u5e93\u65f6\uff08\u4f8b\u5982\uff0c \u7531\u4e8e AUTO_CLOSE \u8bbe\u7f6e\u4e3a ON\uff09\uff0c\u4fbf\u4f1a\u5220\u9664\u4e0e\u8be5\u6570\u636e\u5e93\u5173\u8054\u7684\u6240\u6709\u884c\u3002\r\n\u8fd9\u4e2a\u89c6\u56fe\u6700\u5173\u952e\u7684\u5c31\u662f\u6700\u5173\u952e\u7684\u5c31\u662f\u8fd94\u4e2a\u5b57\u6bb5\uff1a\r\nuser_seeks \u901a\u8fc7\u7528\u6237\u67e5\u8be2\u6267\u884c\u7684\u641c\u7d22\u6b21\u6570,\u5c31\u662f\u5229\u7528\u805a\u96c6\u7d22\u5f15\u7684\u6b21\u6570\u3002\r\nuser_scans \u901a\u8fc7\u7528\u6237\u67e5\u8be2\u6267\u884c\u7684\u626b\u63cf\u6b21\u6570\uff0c\u5c31\u662f\u6ca1\u6709\u5229\u7528\u4efb\u4f55\u7d22\u5f15\u7684\u6b21\u6570\uff0c\u9010\u884c\u626b\u63cf\u6700\u6162\u7684\u90a3\u79cd\u3002\r\nuser_lookups \u901a\u8fc7\u7528\u6237\u67e5\u8be2\u6267\u884c\u7684\u67e5\u627e\u6b21\u6570\uff0c\u5c31\u662f\u5229\u7528\u975e\u805a\u96c6\u7d22\u5f15\u7684\u6b21\u6570\u3002\r\nuser_updates \u901a\u8fc7\u7528\u6237\u67e5\u8be2\u6267\u884c\u7684\u66f4\u65b0\u6b21\u6570\uff0c\u5c31\u662f\u66f4\u65b0\u524d\u627e\u5230\u8fd9\u6761\u6570\u636e\u65f6\u5229\u7528\u7684\u7d22\u5f15\u7684\u6b21\u6570 \u3002\u56e0\u4e3a\u66f4\u65b0\u662f\u5148\u67e5\u8be2\u5230\u9700\u8981\u66f4\u65b0\u7684\u6570\u636e\uff0c\u7136\u540e\u6267\u884c\u66f4\u65b0\u547d\u4ee4\u6240\u4ee5\u66f4\u65b0\u7684\u65f6\u5019\u8fd9\u4e2auser_updates\u5b57\u6bb5\u548c user_seeks\u3001user_lookups\u3001user_scans\u5b57\u6bb5\u4e2d\u7684\u4e00\u4e2a\u90fd\u4f1a\u52a0\u4e00\u3002\r\n\r\n--\u67e5\u8be2\u4ece\u672a\u88ab\u4f7f\u7528\u7684\u7d22\u5f15\r\nSELECT t.name,ix.name,ddius.user_seeks,ddius.user_scans,ddius.user_lookups,ddius.user_updates\r\nFROM sys.dm_db_index_usage_stats ddius\r\nJOIN sys.tables t ON ddius.[object_id] = t.[object_id]\r\nJOIN sys.indexes ix ON ix.[object_id] = ddius.[object_id] AND ix.index_id = ddius.index_id\r\nWHERE ddius.database_id = DB_ID('brm_lvjian')\r\nand last_user_seek is null\r\n     and last_user_scan is null\r\n     and last_user_lookup is null\r\n     and last_user_update is not null\r\n\r\n\r\n\r\n\u7d22\u5f15\u4e0e\u9875\u5173\u7cfb\r\n--\u67e5\u8be2\u7d22\u5f15\u4f7f\u7528\u7684\u9875\u6570\uff0c\u6bcf\u9875\u662f8K,\u9875\u6570\u4e58\u4ee58\uff0c\u5373\u4e3a\u7a7a\u95f4\u4f7f\u7528\u6570\r\nSELECT t.name,i.name,sp.in_row_data_page_count,sp.in_row_used_page_count,sp.in_row_reserved_page_count\r\nFROM sys.tables t\r\nJOIN sys.indexes i ON t.[object_id] = i.[object_id]\r\nJOIN sys.dm_db_partition_stats sp ON sp.[object_id] = i.[object_id] AND sp.index_id = i.index_id\r\nORDER BY t.name,i.name\r\n\r\n\u67e5\u8be2\u8868\u5360\u7528\u7a7a\u95f4\uff0c\u6d89\u53ca\u7684\u7d22\u5f15\u4f7f\u7528\u7a7a\u95f4\r\nSELECT a3.name AS [Schema \u540d\u79f0],\r\n\ta2.name AS [\u8868\u540d\u79f0],\r\n\ta1.rows as \u8bb0\u5f55\u6761\u6570,\r\n\t(a1.reserved + ISNULL(a4.reserved,0))* 8 AS [\u4fdd\u7559\u7a7a\u95f4(K)],\r\n\ta1.data * 8 AS [\u6570\u636e\u4f7f\u7528\u7a7a\u95f4(k)],\r\n\t(CASE WHEN (a1.used + ISNULL(a4.used,0)) > a1.data \r\n\tTHEN (a1.used + ISNULL(a4.used,0)) - a1.data \r\n\tELSE 0 END) * 8 AS [\u7d22\u5f15\u4f7f\u7528\u7a7a\u95f4(k)],\r\n\t(CASE WHEN (a1.reserved + ISNULL(a4.reserved,0)) > a1.used \r\n\tTHEN (a1.reserved + ISNULL(a4.reserved,0)) - a1.used \r\n\tELSE 0 END) * 8 AS [\u672a\u7528\u7a7a\u95f4(k)],\r\n\ta1.data * 8*1024\/(CASE WHEN a1.Rows=0 THEN 1 ELSE a1.Rows END) \u5e73\u5747\u6bcf\u6761\u8bb0\u5f55\u957f\u5ea6\r\n\tFROM\r\n\t\t(\r\n\t\tSELECT\r\n\t\tps.object_id,\r\n\t\tSUM (\r\n\t\tCASE\r\n\t\tWHEN (ps.index_id < 2) THEN row_count\r\n\t\tELSE 0\r\n\t\tEND\r\n\t\t) AS [rows],\r\n\t\tSUM (ps.reserved_page_count) AS reserved,\r\n\t\tSUM (\r\n\t\tCASE\r\n\t\tWHEN (ps.index_id < 2) THEN \r\n\t\t(ps.in_row_data_page_count + ps.lob_used_page_count + ps.row_overflow_used_page_count)\r\n\t\tELSE (ps.lob_used_page_count + ps.row_overflow_used_page_count)\r\n\t\tEND\r\n\t\t) AS data,\r\n\t\tSUM (ps.used_page_count) AS used\r\n\t\tFROM sys.dm_db_partition_stats ps\r\n\t\tGROUP BY ps.object_id) AS a1\r\n\t\tLEFT OUTER JOIN\r\n\t\t\t(\r\n\t\t\tSELECT\r\n\t\t\tit.parent_id,\r\n\t\t\tSUM(ps.reserved_page_count) AS reserved,\r\n\t\t\tSUM(ps.used_page_count) AS used\r\n\t\t\tFROM sys.dm_db_partition_stats ps\r\n\t\t\tINNER JOIN sys.internal_tables it ON (it.object_id = ps.object_id)\r\n\t\t\tWHERE it.internal_type IN (202,204)\r\n\t\t\tGROUP BY it.parent_id\r\n\t\t\t) AS a4 ON (a4.parent_id = a1.object_id)\r\n\tINNER JOIN sys.all_objects a2  ON ( a1.object_id = a2.object_id )\r\n\tINNER JOIN sys.schemas a3 ON (a2.schema_id = a3.schema_id)\r\n\tWHERE a2.type <> N'S' and a2.type <> N'IT'\r\n\tORDER BY [\u4fdd\u7559\u7a7a\u95f4(K)] DESC\r\n\r\n\r\n\r\n\r\n--\u6253\u5f003604\uff0c\u4fe1\u606f\u53d1\u9001\u5230\u63a7\u5236\u53f0\r\nDBCC TRACEON(3604)\r\n\r\n--\u67e5\u770b\u8868\u7d22\u5f15\u5bf9\u5e94\u7684\u9875\u6570\u660e\u7ec6\r\nDBCC IND ('brm_lvjian_0817', 'frmuser', -1);\r\n\r\n--\u67e5\u770b\u9875\u4e0a\u7684\u6570\u636e\u4fe1\u606f\r\nDBCC PAGE (brm_lvjian_0817, 1, 409259, 3);\r\n\r\n\r\n\u6ce8\uff1a\r\nDBCC PAGE\r\n(\r\n    ['database name'|database id], -- can be the actual name or id of the database\r\n    file number, -- the file number where the page is found\r\n    page number, -- the page number within the file\r\n    print option = [0|1|2|3] -- display option; each option provides differing levels of information\r\n)\r\n\r\nDBCC IND\r\n(\r\n    ['database name'|database id], -- the database to use\r\n    table name, -- the table name to list results\r\n    index id, -- an index_id from sys.indexes; -1 shows all indexes and IAMs, -2 just show IAMs\r\n)\r\n\r\n\u53c2\u8003\uff1ahttp:\/\/www.sqlskills.com\/blogs\/paul\/post\/Inside-the-Storage-Engine-Using-DBCC-PAGE-and-DBCC-IND-to-find-out-if-page-splits-ever-roll-back.aspx\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>sys.dm_db_index_usage_stats \u52a8\u6001\u7ba1\u7406\u89c6\u56fe sys.dm_db_index_usag [&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-1730","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\/1730","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=1730"}],"version-history":[{"count":0,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/1730\/revisions"}],"wp:attachment":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/media?parent=1730"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/categories?post=1730"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/tags?post=1730"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}