{"id":2326,"date":"2012-11-05T10:05:46","date_gmt":"2012-11-05T10:05:46","guid":{"rendered":"http:\/\/enjoyasp.net\/?p=2326"},"modified":"2012-11-05T10:36:44","modified_gmt":"2012-11-05T10:36:44","slug":"%e5%86%85%e5%ad%98%e7%ae%a1%e7%90%86","status":"publish","type":"post","link":"https:\/\/enjoyasp.net\/index.php\/2012\/11\/05\/%e5%86%85%e5%ad%98%e7%ae%a1%e7%90%86\/","title":{"rendered":"\u5185\u5b58\u7ba1\u7406"},"content":{"rendered":"<pre class=\"brush:sql;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;\">\r\n<\/pre>\n<div>\n<pre class=\"brush:sql;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;\">\r\n\u4e00\u3001\u5185\u5b58<\/pre>\n<\/div>\n<div>\n\t1.\u67e5\u770b\u6574\u4f53\u5185\u5b58\u4f7f\u7528\u60c5\u51b5<\/div>\n<div>\n\tselect&nbsp;<\/div>\n<div>\n\ttype,<\/div>\n<div>\n\tsum(virtual_memory_reserved_kb)\/1024 as [VM Reserved],<\/div>\n<div>\n\tsum(virtual_memory_committed_kb) as [VM Committed],<\/div>\n<div>\n\tsum(awe_allocated_kb) as [AWE Allocated],<\/div>\n<div>\n\tsum(shared_memory_reserved_kb) as [SM Reserved],&nbsp;<\/div>\n<div>\n\tsum(shared_memory_committed_kb) as [SM Committed],<\/div>\n<div>\n\tsum(multi_pages_kb) as [MultiPage Allocator],<\/div>\n<div>\n\tsum(single_pages_kb) as [SinlgePage Allocator]<\/div>\n<div>\n\tfrom&nbsp;<\/div>\n<div>\n\tsys.dm_os_memory_clerks&nbsp;<\/div>\n<div>\n\tgroup by type<\/div>\n<div>\n\torder by [VM Reserved] DESC&nbsp;<\/div>\n<div>\n\t&nbsp;<\/div>\n<div>\n\t\u5176\u4e2d\uff0cMEMORYCLERK_SQLBUFFERPOOL\u5c31\u662f\u8868\u6570\u636e\uff0c\u6267\u884c\u8ba1\u5212\u7684\u7f13\u5b58\uff0c\u5360\u7528\u7684\u6700\u5927\u3002<\/div>\n<div>\n\t\u5206\u6790MEMORYCLERK_SQLBUFFERPOOL\u6709\u54ea\u4e9b\u6570\u636e\uff0c\u91c7\u7528\u5982\u4e0b\u65b9\u6cd5\uff1a<\/div>\n<div>\n\t&nbsp;<\/div>\n<div>\n\t1)\u4e0b\u9762\u7684\u8fd9\u7ec4\u8bed\u53e5\uff0c\u5c31\u53ef\u4ee5\u6253\u5370\u51fa\u5f53\u524d\u5185\u5b58\u91cc\u7f13\u5b58\u7684\u6240\u6709\u9875\u9762\u7684\u7edf\u8ba1\u4fe1\u606f\u3002<\/div>\n<div>\n\tdeclare @name nvarchar(100)<\/div>\n<div>\n\tdeclare @cmd nvarchar(1000)<\/div>\n<div>\n\tdeclare dbnames cursor for<\/div>\n<div>\n\tselect name from master.dbo.sysdatabases<\/div>\n<div>\n\topen dbnames<\/div>\n<div>\n\tfetch next from dbnames into @name<\/div>\n<div>\n\twhile @@fetch_status = 0<\/div>\n<div>\n\tbegin<\/div>\n<div>\n\tset @cmd = &#39;select b.database_id, db=db_name(b.database_id),p.object_id,p.index_id,buffer_count=count(*) from &#39; + @name + &#39;.sys.allocation_units a, &#39;<\/div>\n<div>\n\t+ @name + &#39;.sys.dm_os_buffer_descriptors b, &#39; + @name + &#39;.sys.partitions p<\/div>\n<div>\n\twhere a.allocation_unit_id = b.allocation_unit_id<\/div>\n<div>\n\tand a.container_id = p.hobt_id<\/div>\n<div>\n\tand b.database_id = db_id(&#39;&#39;&#39; + @name + &#39;&#39;&#39;)<\/div>\n<div>\n\tgroup by b.database_id,p.object_id, p.index_id<\/div>\n<div>\n\torder by b.database_id, buffer_count desc&#39;<\/div>\n<div>\n\texec (@cmd)<\/div>\n<div>\n\tfetch next from dbnames into @name<\/div>\n<div>\n\tend<\/div>\n<div>\n\tclose dbnames<\/div>\n<div>\n\tdeallocate dbnames<\/div>\n<div>\n\tgo<\/div>\n<div>\n\t&nbsp;<\/div>\n<div>\n\t2) \u5728\u4e00\u6761\u8bed\u53e5\u7b2c\u4e00\u6b21\u6267\u884c\u524d\u540e\u5404\u8fd0\u884c\u4e00\u904d\u4e0a\u9762\u7684\u811a\u672c\uff0c\u5c31\u80fd\u591f\u77e5\u9053\u8fd9\u53e5\u8bdd\u8981\u8bfb\u5165\u591a\u5c11\u6570\u636e\u5230\u5185\u5b58\u91cc\u3002<\/div>\n<div>\n\t\u4f8b\u5982\u5982\u679c\u8fd0\u884c\u4e0b\u9762\u7684\u811a\u672c\uff1a<\/div>\n<div>\n\tdbcc dropcleanbuffers<\/div>\n<div>\n\tgo<\/div>\n<div>\n\t&#8212;-Copy the previous scripts here<\/div>\n<div>\n\tGo<\/div>\n<div>\n\tuse adventureworks<\/div>\n<div>\n\tgo<\/div>\n<div>\n\tselect * from person.address<\/div>\n<div>\n\tgo<\/div>\n<div>\n\t&#8212;-Copy the previous scripts again here<\/div>\n<div>\n\tGo<\/div>\n<div>\n\t&nbsp;<\/div>\n<div>\n\t3) \u7528\u4e0b\u9762\u7684\u67e5\u8be2\u53ef\u4ee5\u5f97\u5230\u5404\u79cd\u5bf9\u8c61\u5404\u5360\u4e86\u591a\u5c11\u5185\u5b58\uff1a<\/div>\n<div>\n\tselect objtype, sum(size_in_bytes) as sum_size_in_bytes, count(bucketid) as cache_counts<\/div>\n<div>\n\tfrom sys.dm_exec_cached_plans<\/div>\n<div>\n\tgroup by objtype<\/div>\n<div>\n\t&nbsp;<\/div>\n<div>\n\t4) \u5982\u679c\u60f3\u8981\u5206\u6790\u5177\u4f53\u5b58\u50a8\u4e86\u54ea\u4e9b\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u8bed\u53e5\u3002\u4f46\u662f\u8981\u6ce8\u610f\u628a\u7ed3\u679c\u96c6\u8f93\u51fa\u5230\u4e00\u4e2a\u6587\u4ef6\u91cc\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u67e5\u8be2\u7684\u7ed3\u679c\u5728\u4e00\u4e2a\u751f\u4ea7\u670d\u52a1\u5668\u4e0a\u4f1a\u5f88\u5927\u7684\u3002\u5982\u679c\u8981\u8f93\u51fa\u5230Management Studio\u91cc\uff0c\u5bf9\u8fd0\u884c\u8fd9\u4e2a\u67e5\u8be2\u7684\u90a3\u53f0\u673a\u5668\u7684\u8d44\u6e90\u4f1a\u6709\u4e89\u7528\uff0c\u8fdb\u800c\u5f71\u54cd\u5230\u540c\u4e00\u53f0\u673a\u5668\u4e0a\u7684SQL Server\u8fd0\u884c\u3002<\/div>\n<div>\n\tSELECT usecounts, refcounts, size_in_bytes, cacheobjtype, objtype, text&nbsp;<\/div>\n<div>\n\tFROM sys.dm_exec_cached_plans cp<\/div>\n<div>\n\tCROSS APPLY sys.dm_exec_sql_text(plan_handle)&nbsp;<\/div>\n<div>\n\tORDER BY objtype DESC;<\/div>\n<div>\n\tGO<\/div>\n<div>\n\t&nbsp;<\/div>\n<div>\n\t\u4e8c \u4f7f\u7528DMV\u5206\u6790SQL Server\u542f\u52a8\u4ee5\u6765\u505aread\u6700\u591a\u7684\u8bed\u53e5<\/div>\n<div>\n\t1. \u6309\u7167\u7269\u7406\u8bfb\u7684\u9875\u9762\u6570\u6392\u5e8f\uff0c\u524d50\u540d\u3002<\/div>\n<div>\n\tSELECT TOP 50<\/div>\n<div>\n\tqs.total_physical_reads,qs.execution_count,<\/div>\n<div>\n\t&nbsp; &nbsp; &nbsp; &nbsp; qs.total_physical_reads \/qs.execution_count as [Avg IO],<\/div>\n<div>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SUBSTRING(qt.text,qs.statement_start_offset\/2,&nbsp;<\/div>\n<div>\n\t(case when qs.statement_end_offset = -1&nbsp;<\/div>\n<div>\n\tthen len(convert(nvarchar(max), qt.text)) * 2&nbsp;<\/div>\n<div>\n\telse qs.statement_end_offset end -qs.statement_start_offset)\/2)&nbsp;<\/div>\n<div>\n\tas query_text,<\/div>\n<div>\n\tqt.dbid, dbname=db_name(qt.dbid),<\/div>\n<div>\n\tqt.objectid,<\/div>\n<div>\n\tqs.sql_handle,<\/div>\n<div>\n\tqs.plan_handle<\/div>\n<div>\n\tFROM sys.dm_exec_query_stats qs<\/div>\n<div>\n\tcross apply sys.dm_exec_sql_text(qs.sql_handle) as qt<\/div>\n<div>\n\tORDER BY qs.total_physical_reads desc<\/div>\n<div>\n\t&nbsp;<\/div>\n<div>\n\t2. \u6309\u7167\u903b\u8f91\u8bfb\u7684\u9875\u9762\u6570\u6392\u5e8f\uff0c\u524d50\u540d\u3002<\/div>\n<div>\n\tSELECT TOP 50<\/div>\n<div>\n\tqs.total_logical_reads,qs.execution_count,<\/div>\n<div>\n\t&nbsp; &nbsp; &nbsp; &nbsp; qs.total_logical_reads \/qs.execution_count as [Avg IO],<\/div>\n<div>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SUBSTRING(qt.text,qs.statement_start_offset\/2,&nbsp;<\/div>\n<div>\n\t(case when qs.statement_end_offset = -1&nbsp;<\/div>\n<div>\n\tthen len(convert(nvarchar(max), qt.text)) * 2&nbsp;<\/div>\n<div>\n\telse qs.statement_end_offset end -qs.statement_start_offset)\/2)&nbsp;<\/div>\n<div>\n\tas query_text,<\/div>\n<div>\n\tqt.dbid, dbname=db_name(qt.dbid),<\/div>\n<div>\n\tqt.objectid,<\/div>\n<div>\n\tqs.sql_handle,<\/div>\n<div>\n\tqs.plan_handle<\/div>\n<div>\n\tFROM sys.dm_exec_query_stats qs<\/div>\n<div>\n\tcross apply sys.dm_exec_sql_text(qs.sql_handle) as qt<\/div>\n<div>\n\tORDER BY qs.total_logical_reads desc<\/div>\n<div>\n\t&nbsp;<\/div>\n<div>\n\t3. \u4f7f\u7528SQL Trace\u6587\u4ef6\u6765\u5206\u6790\u67d0\u4e00\u6bb5\u65f6\u95f4\u5185\u505aread\u6700\u591a\u7684\u8bed\u53e5\u3002<\/div>\n<div>\n\t\u4f8b\u5982\u73b0\u5728\u5728c:\\sample\u76ee\u5f55\u4e0b\u6536\u96c6\u4e86\u4e00\u4e2a\u95ee\u9898\u65f6\u6bb5\u7684trace\u6587\u4ef6\uff0c\u53ebA.trc\u3002\u7b2c\u4e00\u6b65\u8981\u5c06\u91cc\u9762\u6240\u6709\u7684\u5b58\u50a8\u8fc7\u7a0b\u548c\u6279\u547d\u4ee4\u6267\u884c\u5b8c\u6210\u7684\u8bb0\u5f55\u4fdd\u5b58\u5230SQL Server\u91cc\u3002<\/div>\n<div>\n\tselect * into Sample<\/div>\n<div>\n\tfrom fn_trace_gettable(&#39;c:\\sample\\a.trc&#39;,default)<\/div>\n<div>\n\twhere eventclass in (10, 12)<\/div>\n<div>\n\t&nbsp;<\/div>\n<div>\n\t\u8bed\u53e5\u6267\u884c\u5b8c\u4e86\u4ee5\u540e\uff0c\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u67e5\u8be2\u770b\u770b\u91cc\u9762\u7684\u6570\u636e\u957f\u4ec0\u4e48\u6837\u3002<\/div>\n<div>\n\tSelect top 1000 textdata, databaseId, HostName, ApplicationName, LoginName, SPID,<\/div>\n<div>\n\tStarttime, EndTime, Duration, reads, writes, CPU&nbsp;<\/div>\n<div>\n\tfrom sample<\/div>\n<div>\n\t&nbsp;<\/div>\n<div>\n\ta. \u627e\u5230\u662f\u54ea\u53f0\u5ba2\u6237\u7aef\u670d\u52a1\u5668\u4e0a\u7684\u54ea\u4e2a\u5e94\u7528\u53d1\u8fc7\u6765\u7684\u8bed\u53e5\uff0c\u4ece\u6574\u4f53\u4e0a\u8bb2\u5728\u6570\u636e\u5e93\u4e0a\u5f15\u8d77\u7684\u8bfb\u6700\u591a\u3002<\/div>\n<div>\n\tselect databaseId,HostName,ApplicationName, sum(reads)<\/div>\n<div>\n\tfrom sample<\/div>\n<div>\n\tgroup by databaseId,HostName,ApplicationName<\/div>\n<div>\n\torder by sum(reads) desc<\/div>\n<div>\n\t&nbsp;<\/div>\n<div>\n\tb. \u6309\u7167\u4f5c\u7684reads\u4ece\u5927\u5230\u5c0f\u6392\u5e8f\uff0c\u6700\u5927\u76841000\u4e2a\u8bed\u53e5\u3002<\/div>\n<div>\n\tselect top 1000 textdata, databaseId, HostName, ApplicationName, LoginName, SPID,<\/div>\n<div>\n\tStarttime, EndTime, Duration, reads, writes, CPU&nbsp;<\/div>\n<div>\n\tfrom sample<\/div>\n<div>\n\torder by reads desc<\/div>\n<div>\n\t&nbsp;<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5185\u5b58 1.\u67e5\u770b\u6574\u4f53\u5185\u5b58\u4f7f\u7528\u60c5\u51b5 select&nbsp; type, sum(virtual_memory [&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-2326","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\/2326","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=2326"}],"version-history":[{"count":0,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/2326\/revisions"}],"wp:attachment":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/media?parent=2326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/categories?post=2326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/tags?post=2326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}