<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5493816805450410906</id><updated>2024-09-04T21:29:17.684-03:00</updated><category term="sql server 2005"/><category term="sql server 2008"/><category term="desempenho"/><category term="queries"/><category term="query"/><category term="performance"/><category term="stored procedures"/><category term="buffer pool"/><category term="dmf"/><category term="dmfs"/><category term="dmv"/><category term="dmvs"/><category term="espaço em disco"/><category term="kill"/><category term="memória"/><category term="rede"/><category term="sessões"/><category term="sp_who"/><category term="sp_who2"/><category term="tabelas"/><category term="usuários"/><category term="última execução"/><title type='text'>Tiago Bonamigo - Dicas sobre SQL Server 2005, SQL Server 2008 e SQL Server 2012</title><subtitle type='html'>Blog com dicas para SQL Server 2005, SQL Server 2008 e SQL Server 2012. Principalmente focado em administração, mas também abordando desenvolvimento e módulos como Analysis Services, Reporting Services e Integration Services (SSIS).</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://tiagobonamigo.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5493816805450410906/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://tiagobonamigo.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Tiago</name><uri>http://www.blogger.com/profile/01881296233037002659</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>8</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5493816805450410906.post-7652368150656670062</id><published>2012-08-22T14:19:00.000-03:00</published><updated>2012-08-22T16:54:12.155-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="espaço em disco"/><category scheme="http://www.blogger.com/atom/ns#" term="sql server 2005"/><category scheme="http://www.blogger.com/atom/ns#" term="sql server 2008"/><category scheme="http://www.blogger.com/atom/ns#" term="stored procedures"/><category scheme="http://www.blogger.com/atom/ns#" term="tabelas"/><title type='text'>Como ver o espaço ocupado por cada tabela</title><content type='html'>Olá pessoal! Aí vai uma dica 2 em 1. A primeira dica é de uma stored procedure não documentada que existe desde o SQL Server 6.5 (veeeelho) e permite que se execute uma função, query ou stored procedure por cada tabela existente no banco de dados. A segunda dica é como usar essa stored procedure para ver o tamanho ocupado por cada tabela.&lt;br /&gt;
&lt;br /&gt;
A stored procedure não documentada é a &lt;b&gt;sp_MSforeachtable&lt;/b&gt;, que permite que você rapidamente execute uma função usando as tabelas existentes no banco como parâmetro. Caso você queira saber o count(*) de cada tabela no banco, é possível fazer isso:&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;; font-size: 10pt; line-height: 1.5em;&quot;&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;&quot;&gt;exec&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: maroon;&quot;&gt;sp_MSforeachtable&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39; select &#39;&#39;?&#39;&#39; as Tabela, count(*) as Registros from ? &#39;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
E para utilizar isso para ver o tamanho das tabelas, é só utilizar a procedure &lt;b&gt;sp_spaceused &lt;/b&gt;da seguinte maneira:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; font-size: 13px;&quot;&gt;exec&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; font-size: 13px;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: maroon; font-family: &#39;Courier New&#39;; font-size: 13px;&quot;&gt;sp_MSforeachtable&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; font-size: 13px;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: red; font-family: &#39;Courier New&#39;; font-size: 13px;&quot;&gt;&#39;EXEC sp_spaceused &#39;&#39;?&#39;&#39;&#39;&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
O resultado é apresentado no formato abaixo:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYgvP1aOIW6AbcU9CtOoUpKlb4B9DHsm9mmUbySunzf5ONl3W2MhePN7heD74UaTJu6qsKRHPWVeoa4PmxQDpdEsqqz0kt3rOHFsAbzWf183MgCd-NlE1k7p-hSDhyphenhyphen1Bu1gSe3MYiTOKA/s1600/result_tablesize.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;41&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYgvP1aOIW6AbcU9CtOoUpKlb4B9DHsm9mmUbySunzf5ONl3W2MhePN7heD74UaTJu6qsKRHPWVeoa4PmxQDpdEsqqz0kt3rOHFsAbzWf183MgCd-NlE1k7p-hSDhyphenhyphen1Bu1gSe3MYiTOKA/s320/result_tablesize.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Temos o nome da tabela, número de registros, espaço reservado no datafile, espaço ocupado pelos dados, espaço ocupado pelos índices e o espaço não utilizado, ou seja, a diferença entre o que foi utilizado por dados e índices e o espaço reservado. Métricas úteis em várias situações. O problema é que as queries são executadas individualmente e não é possível fazer alguma ordenação como, por exemplo, listar as tabelas ordenadas decrescentemente por espaço em disco reservado, etc. Mas aproveito e já mostro um script para isso.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;TABLE&lt;/span&gt; #tempTable&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [name] &lt;span style=&quot;color: blue;&quot;&gt;NVARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;128&lt;span style=&quot;color: grey;&quot;&gt;),&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [rows] &lt;span style=&quot;color: blue;&quot;&gt;CHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;11&lt;span style=&quot;color: grey;&quot;&gt;),&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; reserved &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;18&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; data &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;18&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; index_size &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;18&lt;span style=&quot;color: grey;&quot;&gt;),&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; unused &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;18&lt;span style=&quot;color: grey;&quot;&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;insert&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; #tempTable
&lt;span style=&quot;color: blue;&quot;&gt;exec&lt;/span&gt; &lt;span style=&quot;color: maroon;&quot;&gt;sp_MSforeachtable&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;EXEC sp_spaceused &#39;&#39;?&#39;&#39;&#39;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;select&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; [name]&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; [rows]&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: magenta; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;cast&lt;/span&gt;&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;replace&lt;/span&gt;&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;reserved&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39; KB&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; reserved&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: magenta; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;cast&lt;/span&gt;&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;replace&lt;/span&gt;&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;data&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39; KB&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; data&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: magenta; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;cast&lt;/span&gt;&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;replace&lt;/span&gt;&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;index_size&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39; KB&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; index_size&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: magenta; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;cast&lt;/span&gt;&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;replace&lt;/span&gt;&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;unused&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39; KB&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; unused&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;into&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;
#finalTable&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; #tempTable&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: green; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;-- Modifique o
order by abaixo para ter a ordenação desejada&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;select&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: grey;&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;from&lt;/span&gt; #finalTable &lt;span style=&quot;color: blue;&quot;&gt;order&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;by&lt;/span&gt; reserved &lt;span style=&quot;color: blue;&quot;&gt;desc&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;drop&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;table&lt;/span&gt; #finalTable&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;&quot;&gt;drop&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;table&lt;/span&gt; #tempTable&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Acho esse script bem legal para ver índices sobrecarregados e também ver as tabelas mais carregadas quando tenho o primeiro contato com um banco de dados. Com frequência encontro bancos de dados com tantos índices feitos para atender uma consulta em particular que os índices ocupam muito mais espaço que os dados.
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Novamente, agradeço a leitura. O que mais quero desse blog é que ele seja útil, então enviem suas dúvidas, críticas e sugestões. Ah, e se puderem clicar em um dos anúncios na página, não fico nada chateado. Consegui ganhar 80 centavos de dólar no primeiro mês, que maravilha! Abraços a todos!&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;content-fragment social-media-sharing no-wrapper&quot; id=&quot;fragment-52318&quot; style=&quot;background-color: #ced5db; background-image: none; color: #333333; font-family: &#39;Segoe UI&#39;, &#39;Lucida Grande&#39;, Verdana, Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px 0px 0px 8px;&quot;&gt;
&lt;div class=&quot;content-fragment-inner msdn-content-fragment-inner&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://tiagobonamigo.blogspot.com/feeds/7652368150656670062/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://tiagobonamigo.blogspot.com/2012/08/como-ver-o-espaco-ocupado-por-cada.html#comment-form' title='2 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5493816805450410906/posts/default/7652368150656670062'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5493816805450410906/posts/default/7652368150656670062'/><link rel='alternate' type='text/html' href='http://tiagobonamigo.blogspot.com/2012/08/como-ver-o-espaco-ocupado-por-cada.html' title='Como ver o espaço ocupado por cada tabela'/><author><name>Tiago</name><uri>http://www.blogger.com/profile/01881296233037002659</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYgvP1aOIW6AbcU9CtOoUpKlb4B9DHsm9mmUbySunzf5ONl3W2MhePN7heD74UaTJu6qsKRHPWVeoa4PmxQDpdEsqqz0kt3rOHFsAbzWf183MgCd-NlE1k7p-hSDhyphenhyphen1Bu1gSe3MYiTOKA/s72-c/result_tablesize.png" height="72" width="72"/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5493816805450410906.post-7072737555109228543</id><published>2012-08-21T19:30:00.000-03:00</published><updated>2012-08-21T19:30:00.883-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="desempenho"/><category scheme="http://www.blogger.com/atom/ns#" term="performance"/><category scheme="http://www.blogger.com/atom/ns#" term="rede"/><category scheme="http://www.blogger.com/atom/ns#" term="sql server 2005"/><category scheme="http://www.blogger.com/atom/ns#" term="sql server 2008"/><category scheme="http://www.blogger.com/atom/ns#" term="stored procedures"/><title type='text'>Muitas inserções ou consultas? Melhore a performance com SET NOCOUNT ON</title><content type='html'>Olá pessoal, aqui vai um post mais simples mas bastante útil. Caso você tenha uma stored procedure ou function que executa muitos INSERTs, UPDATEs, DELETEs e SELECTs no banco de dados e você precisa retornar estes dados para outra máquina e nem usa as messages do servidor nesta outra máquina para contar o número de registros afetados, coloque um SET NOCOUNT ON no início da procedure e veja se há melhora no desempenho.&lt;br /&gt;
&lt;br /&gt;
Estou conectado em um servidor SQL Server 2008 via TCP/IP na Internet, link de 10Mbit e executando os scripts via Management Studio. Então caso você tenha uma situação similar e queira comprovar os benefícios do SET NOCOUNT ON de uma maneira bem simples, execute primeiro esta rotina:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;create&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;proc&lt;/span&gt; TESTE_NOCOUNT_OFF &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;begin&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;create&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;table&lt;/span&gt;
#temp&lt;span style=&quot;color: blue;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ID &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;not&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;null&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;primary&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;key&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CAMPOTESTE &lt;span style=&quot;color: blue;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;10&lt;span style=&quot;color: grey;&quot;&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: grey;&quot;&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;set&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;nocount&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;off&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;declare&lt;/span&gt; @contador &lt;span style=&quot;color: blue;&quot;&gt;int&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;set&lt;/span&gt; @contador &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;while&lt;/span&gt; @contador &lt;span style=&quot;color: grey;&quot;&gt;&amp;lt;&lt;/span&gt; 100000&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;begin&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;insert&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;into&lt;/span&gt; #temp&lt;span style=&quot;color: blue;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;ID&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; CAMPOTESTE&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;values &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;@contador&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;TESTETESTE&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;set&lt;/span&gt; @contador &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; @contador &lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt; 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;end&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;drop&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;table&lt;/span&gt;
#temp&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;end&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;GO&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;exec&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;
TESTE_NOCOUNT_OFF&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;GO&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;drop&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;proc&lt;/span&gt; TESTE_NOCOUNT_OFF&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; font-size: 10pt; line-height: 115%;&quot;&gt;GO&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
Anote o tempo de execução com o NOCOUNT desligado (aqui rodou em 44 segundos) e depois execute esta rotina, com o NOCOUNT ligado:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;create&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;proc&lt;/span&gt; TESTE_NOCOUNT_ON &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;begin&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;create&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;table&lt;/span&gt;
#temp&lt;span style=&quot;color: blue;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ID &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;not&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;null&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;primary&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;key&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CAMPOTESTE &lt;span style=&quot;color: blue;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;10&lt;span style=&quot;color: grey;&quot;&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: grey;&quot;&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;set&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;nocount&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;on&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;declare&lt;/span&gt; @contador &lt;span style=&quot;color: blue;&quot;&gt;int&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;set&lt;/span&gt; @contador &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;while&lt;/span&gt; @contador &lt;span style=&quot;color: grey;&quot;&gt;&amp;lt;&lt;/span&gt; 100000&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;begin&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;insert&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;into&lt;/span&gt; #temp&lt;span style=&quot;color: blue;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;ID&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; CAMPOTESTE&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;values &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;@contador&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;TESTETESTE&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;set&lt;/span&gt; @contador &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; @contador &lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt; 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;end&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;drop&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;table&lt;/span&gt;
#temp&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;set&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;nocount&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;off&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;end&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;GO&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;exec&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;
TESTE_NOCOUNT_ON&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;GO&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;drop&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;proc&lt;/span&gt; TESTE_NOCOUNT_ON&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; font-size: 10pt; line-height: 115%;&quot;&gt;GO&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Agora sem as mensagens rodou em 1 segundo. Legal, não? O motivo da demora com as mensagens não é o consumo de CPU ou I/O, mas sim rede. Como o SQL Server transmite por padrão as mensagens com os contadores, independente destas serem usadas pelo cliente, a performance da rotina sofre um grande impacto. Eliminando as mensagens, é eliminado este overhead e há uma melhoria sensível na execução.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Qualquer dúvida, avisem. Obrigado por visitar o blog!&lt;/div&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://tiagobonamigo.blogspot.com/feeds/7072737555109228543/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://tiagobonamigo.blogspot.com/2012/08/muitas-insercoes-ou-consultas-melhore.html#comment-form' title='1 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5493816805450410906/posts/default/7072737555109228543'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5493816805450410906/posts/default/7072737555109228543'/><link rel='alternate' type='text/html' href='http://tiagobonamigo.blogspot.com/2012/08/muitas-insercoes-ou-consultas-melhore.html' title='Muitas inserções ou consultas? Melhore a performance com SET NOCOUNT ON'/><author><name>Tiago</name><uri>http://www.blogger.com/profile/01881296233037002659</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5493816805450410906.post-651405047694572075</id><published>2012-08-20T07:30:00.000-03:00</published><updated>2012-08-20T07:30:00.841-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="buffer pool"/><category scheme="http://www.blogger.com/atom/ns#" term="desempenho"/><category scheme="http://www.blogger.com/atom/ns#" term="memória"/><category scheme="http://www.blogger.com/atom/ns#" term="sql server 2005"/><category scheme="http://www.blogger.com/atom/ns#" term="sql server 2008"/><title type='text'>Quanta memória RAM cada banco de dados está utilizando?</title><content type='html'>Uma ótima dica do blog do &lt;a href=&quot;http://www.johnsansom.com/&quot;&gt;John Samson&lt;/a&gt;, vale pra SQL Server 2005 e 2008, logo testarei no 2012. Em vários momentos precisamos saber o uso de memória por banco de dados, seja pra migração, seja para monitorar a performance de escrita em disco. Mas bem, vamos à query:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-ansi-language: EN-US; mso-no-proof: yes;&quot;&gt;SELECT&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-ansi-language: EN-US; mso-no-proof: yes;&quot;&gt;(&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-ansi-language: EN-US; mso-no-proof: yes;&quot;&gt;CASE&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-ansi-language: EN-US; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;WHEN &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;[is_modified] &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; 1&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;THEN&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;Dirty&#39;&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;ELSE&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;Clean&#39;&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;END&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;Page State&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-ansi-language: EN-US; mso-no-proof: yes;&quot;&gt;(&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-ansi-language: EN-US; mso-no-proof: yes;&quot;&gt;CASE&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-ansi-language: EN-US; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;WHEN &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;[database_id] &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt;
32767&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;THEN&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;Resource Database&#39;&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;ELSE&lt;/span&gt;
&lt;span style=&quot;color: magenta;&quot;&gt;DB_NAME&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;database_id&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;END&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;Database Name&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: magenta; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-ansi-language: EN-US; mso-no-proof: yes;&quot;&gt;COUNT&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-ansi-language: EN-US; mso-no-proof: yes;&quot;&gt;
&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-ansi-language: EN-US; mso-no-proof: yes;&quot;&gt;(*)&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-ansi-language: EN-US; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;Page Count&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: magenta; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-ansi-language: EN-US; mso-no-proof: yes;&quot;&gt;cast&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-ansi-language: EN-US; mso-no-proof: yes;&quot;&gt;((&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;color: magenta; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-ansi-language: EN-US; mso-no-proof: yes;&quot;&gt;COUNT&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-ansi-language: EN-US; mso-no-proof: yes;&quot;&gt; &lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-ansi-language: EN-US; mso-no-proof: yes;&quot;&gt;(*)&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-ansi-language: EN-US; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: grey;&quot;&gt;*&lt;/span&gt; 8&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;numeric&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;10&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;2&lt;span style=&quot;color: grey;&quot;&gt;))/&lt;/span&gt;1024 &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;MBs used&#39;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-ansi-language: EN-US; mso-no-proof: yes;&quot;&gt;FROM&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-ansi-language: EN-US; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: green;&quot;&gt;sys&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;dm_os_buffer_descriptors&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-ansi-language: EN-US; mso-no-proof: yes;&quot;&gt;GROUP&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-ansi-language: EN-US; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;BY&lt;/span&gt; [database_id]&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;
[is_modified]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-ansi-language: EN-US; mso-no-proof: yes;&quot;&gt;ORDER&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-ansi-language: EN-US; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;BY&lt;/span&gt; [database_id]&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;
[is_modified]&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Então vamos entender os registros acima. O que acontece é que o SQL Server faz uso de memória para o Buffer Pool, que é onde ele mantém uma cópia em memória de dados acessados a partir de consultas feitas anteriormente para que ele não precise acessar o disco/storage caso a consulta seja feita novamente. A consulta acima mostra quão grande é o Buffer Pool para cada banco de dados.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Pages são as unidades de armazenamento de dados do SQL Server, e por padrão tem 8 KBytes. O resultado da coluna Page Reads, que pode ser Dirty ou Clean, diferencia o status dos pages. Existem os que contém dados que estão alinhados com o banco de dados (clean) e os que já foram modificados em memória mas ainda não em disco (dirty). Multiplicando o número de pages por 8Kb e dividindo por 1024 temos o número de MBs alocados por banco.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Hoje o post foi mais curto, mas espero que seja útil e obrigado por visitar o blog! :)&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://tiagobonamigo.blogspot.com/feeds/651405047694572075/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://tiagobonamigo.blogspot.com/2012/08/quanta-memoria-ram-cada-banco-de-dados.html#comment-form' title='1 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5493816805450410906/posts/default/651405047694572075'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5493816805450410906/posts/default/651405047694572075'/><link rel='alternate' type='text/html' href='http://tiagobonamigo.blogspot.com/2012/08/quanta-memoria-ram-cada-banco-de-dados.html' title='Quanta memória RAM cada banco de dados está utilizando?'/><author><name>Tiago</name><uri>http://www.blogger.com/profile/01881296233037002659</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total><georss:featurename>Porto Alegre - RS, Brasil</georss:featurename><georss:point>-30.0277041 -51.2287346</georss:point><georss:box>-30.2476641 -51.544591600000004 -29.8077441 -50.9128776</georss:box></entry><entry><id>tag:blogger.com,1999:blog-5493816805450410906.post-7834503744754584451</id><published>2012-08-19T19:30:00.000-03:00</published><updated>2012-08-19T22:50:51.442-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="dmf"/><category scheme="http://www.blogger.com/atom/ns#" term="dmfs"/><category scheme="http://www.blogger.com/atom/ns#" term="dmv"/><category scheme="http://www.blogger.com/atom/ns#" term="dmvs"/><category scheme="http://www.blogger.com/atom/ns#" term="queries"/><category scheme="http://www.blogger.com/atom/ns#" term="query"/><category scheme="http://www.blogger.com/atom/ns#" term="sql server 2005"/><category scheme="http://www.blogger.com/atom/ns#" term="sql server 2008"/><category scheme="http://www.blogger.com/atom/ns#" term="última execução"/><title type='text'>Como visualizar a última query executada por cada conexão aberta</title><content type='html'>Essa surgiu no meu trabalho. Precisávamos entender o funcionamento de um web service simples para o qual não tínhamos o código-fonte disponível, então em um determinado momento foi necessário ver qual consulta ele estava fazendo no banco a partir de uma determinada solicitação. Novamente, antes de sairmos ligando o Profiler, consideramos que a regra de negócio do web service poderia ser simples e talvez pegando a última query executada por aquela conexão teríamos a resposta. Pois bem, deu certo.&lt;br /&gt;
&lt;br /&gt;
Aí vai a query que resolveu o nosso problema:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white; color: blue; font-family: monospace; font-size: 12px; line-height: 19.450000762939453px; margin: 0px; padding: 0px; text-align: justify;&quot;&gt;SELECT&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white; font-family: monospace; font-size: 12px; line-height: 19.450000762939453px; margin: 0px; padding: 0px; text-align: justify;&quot;&gt;session_id&lt;/span&gt;&lt;span style=&quot;background-color: white; color: grey; font-family: monospace; font-size: 12px; line-height: 19.450000762939453px; margin: 0px; padding: 0px; text-align: justify;&quot;&gt;,&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white; color: blue; font-family: monospace; font-size: 12px; line-height: 19.450000762939453px; margin: 0px; padding: 0px; text-align: justify;&quot;&gt;TEXT&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;FROM&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white; font-family: monospace; font-size: 12px; line-height: 19.450000762939453px; margin: 0px; padding: 0px; text-align: justify;&quot;&gt;sys.dm_exec_connections&lt;/span&gt;&lt;span style=&quot;background-color: white; color: grey; font-family: monospace; font-size: 12px; line-height: 19.450000762939453px; margin: 0px; padding: 0px; text-align: justify;&quot;&gt;CROSS&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white; font-family: monospace; font-size: 12px; line-height: 19.450000762939453px; margin: 0px; padding: 0px; text-align: justify;&quot;&gt;APPLY&amp;nbsp;sys.dm_exec_sql_text&lt;/span&gt;&lt;span style=&quot;background-color: white; color: grey; font-family: monospace; font-size: 12px; line-height: 19.450000762939453px; margin: 0px; padding: 0px; text-align: justify;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;background-color: white; font-family: monospace; font-size: 12px; line-height: 19.450000762939453px; margin: 0px; padding: 0px; text-align: justify;&quot;&gt;most_recent_sql_handle&lt;/span&gt;&lt;span style=&quot;background-color: white; color: grey; font-family: monospace; font-size: 12px; line-height: 19.450000762939453px; margin: 0px; padding: 0px; text-align: justify;&quot;&gt;)&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white; color: blue; font-family: monospace; font-size: 12px; line-height: 19.450000762939453px; margin: 0px; padding: 0px; text-align: justify;&quot;&gt;AS&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white; font-family: monospace; font-size: 12px; line-height: 19.450000762939453px; margin: 0px; padding: 0px; text-align: justify;&quot;&gt;ST&lt;/span&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white; font-family: monospace; font-size: 12px; line-height: 19.450000762939453px; margin: 0px; padding: 0px; text-align: justify;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
Aparece o session_id e o texto da query executada. Mas caso você queria mais informações para saber de qual máquina veio a query, o seu IP, qual software está fazendo a requisição e outras informações, sem problemas. É só fazem um INNER JOIN com a view de sistema &lt;b&gt;sys.sysprocesses &lt;/b&gt;, que é a mesma utilizada pelas stored procedures&amp;nbsp;&lt;b&gt;sp_who&lt;/b&gt; e &lt;b&gt;sp_who2&lt;/b&gt;,&amp;nbsp;empregadas para visualizar as sessões e processos em aberto. Então aí vai uma versão mais completinha:&lt;br /&gt;
&lt;br /&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt;&quot;&gt;SELECT&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt;&quot;&gt;
client_net_address &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; [IP do cliente]&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;p&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;hostname
&lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; [Nome da máquina do cliente]&lt;span style=&quot;color: grey;&quot;&gt;,&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;[text] &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; [Texto da consulta]&lt;span style=&quot;color: grey;&quot;&gt;,&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: magenta; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;DB_NAME&lt;/span&gt;&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;p&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;dbid&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; [Nome do BD no
qual foi executada a query]&lt;span style=&quot;color: grey;&quot;&gt;,&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;p&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;[program_name]
&lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; [Programa solicitante]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: green;&quot;&gt;sys&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;dm_exec_connections&lt;/span&gt; c&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;INNER&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: grey;&quot;&gt;JOIN&lt;/span&gt; &lt;span style=&quot;color: green;&quot;&gt;sys&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;sysprocesses&lt;/span&gt; p &lt;span style=&quot;color: blue;&quot;&gt;on&lt;/span&gt; c&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;session_id &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; p&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;spid&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;&quot;&gt;CROSS&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: grey;&quot;&gt;APPLY&lt;/span&gt; &lt;span style=&quot;color: green;&quot;&gt;sys&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;dm_exec_sql_text&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;most_recent_sql_handle&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; ST&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
Espero que a dica seja útil! Caso vocês tenham quaisquer dúvidas sobre como resolver um problema no SQL Server, coloquem um comentário que tento responder - dentro da minha capacidade, é claro - o mais rápido possível.</content><link rel='replies' type='application/atom+xml' href='http://tiagobonamigo.blogspot.com/feeds/7834503744754584451/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://tiagobonamigo.blogspot.com/2012/08/como-visualizar-ultima-query-executada.html#comment-form' title='6 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5493816805450410906/posts/default/7834503744754584451'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5493816805450410906/posts/default/7834503744754584451'/><link rel='alternate' type='text/html' href='http://tiagobonamigo.blogspot.com/2012/08/como-visualizar-ultima-query-executada.html' title='Como visualizar a última query executada por cada conexão aberta'/><author><name>Tiago</name><uri>http://www.blogger.com/profile/01881296233037002659</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5493816805450410906.post-1317122258675101420</id><published>2012-08-18T11:23:00.002-03:00</published><updated>2012-08-19T15:00:14.271-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="desempenho"/><category scheme="http://www.blogger.com/atom/ns#" term="performance"/><category scheme="http://www.blogger.com/atom/ns#" term="queries"/><category scheme="http://www.blogger.com/atom/ns#" term="query"/><category scheme="http://www.blogger.com/atom/ns#" term="sql server 2005"/><category scheme="http://www.blogger.com/atom/ns#" term="sql server 2008"/><title type='text'>Forma rápida de verificar queries que estão pesando muito no servidor - Parte II</title><content type='html'>Olá pessoal, demorei a retomar os posts do blog pela correria do mestrado, mas vamos voltar ao assunto de verificar rapidamente quais são as queries que estão sobrecarregando o seu servidor. Como foi explicado no último post, muitas vezes é melhor utilizar as DMVs e DMFs (dynamic management views / dynamic management functions) do que configurar o Profiler, gerar um workload e fazer a análise subsequente em função da velocidade de resposta. Novamente, estou falando de SQL Server 2005 e 2008, não testei os pontos abaixo em 2012.&lt;br /&gt;
&lt;br /&gt;
O cerne da query que coloquei no post anterior é a DMV&amp;nbsp;&lt;b&gt;sys.dm_exec_query_stats&lt;/b&gt;, que contém uma série de dados relevantes para a avaliação das consultas no cache, porém ela falha em alguns pontos. Vou apresentar alguns deles para apoiar a sua decisão, para ver se o melhor é usar o Profiler, fazer uso da DMV ou outra ferramenta:&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Queries com a opção RECOMPILE não são capturadas&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Não há muito o que dizer além do que está dito no título. Se você faz uso de queries com RECOMPILE, a DMV não vai armazenar informações pertinentes à sua execução e podem ser justamente as consultas causando a sobrecarga no seu banco de dados.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Queries estruturalmente similares não podem ser agrupadas&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Isso é um problema maior no SQL Server 2005 do que no 2008, mas para explicar o problema vou lançar um exemplo. Digamos que executamos duas queries simples, como as que coloco abaixo:&lt;br /&gt;
&lt;br /&gt;
SELECT Nome FROM Tabela WHERE id = 1&lt;br /&gt;
SELECT Nome FROM Tabela WHERE id = 2&lt;br /&gt;
&lt;br /&gt;
Tanto no SQL Server 2005 quanto no SQL Server 2008, ele vai armazenar a instrução de maneira isolada no cache. Então no campo &lt;b&gt;text &lt;/b&gt;retornado pela DMF&amp;nbsp;&lt;b&gt;sys.dm_exec_sql_text&lt;/b&gt; , não poderemos usar um GROUP BY, COMPUTE ou qualquer outra função de agrupamento pois em um tem o trecho &quot;id = 1&quot; e no outro &quot;id = 2&quot;. No SQL Server 2008, tem algo bem legal que permite que consigamos agrupar as duas queries, que são os novos campos&amp;nbsp;&lt;b&gt;query_hash&lt;/b&gt; e &lt;b&gt;query_plan_hash&lt;/b&gt; retornados pela DMV &lt;b&gt;sys.dm_exec_query_stats&lt;/b&gt;. Estes dois campos geram um hash da consulta desconsiderando os parâmetros, permitindo que você agrupe as consultas estruturalmente iguais mas com parâmetros diferentes.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Algumas informações de execução da procedure não são consideradas&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Alguns recursos de temporização como o WAITFOR não são considerados nas DMV e aparecem no Profiler.&lt;br /&gt;
&lt;br /&gt;
Existem alguns outros pontos, mas acredito que os três pontos acima são os de maior criticidade. E até pegando o ponto do agrupamento, dá para alterar a query anterior, desta vez somente para SQL Server 2008, para fazer uso do query_hash e ter estatísticas acerca de queries estruturalmente idênticas. Porém, dica de outro, a Microsoft já lançou uma ferramenta muito legal que já faz isso pra você:&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://blogs.msdn.com/b/bartd/archive/2010/11/03/query-hash-statistics-a-query-cost-analysis-tool-now-available-for-download.aspx&quot;&gt;Query Hash Statistics&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
E cumprindo o que prometi no post anterior, aí vai uma lista de coisas que dá para se analisar com a sys.dm_exec_query_stats no SQL Server 2008 R2. Vou colar a tabela do MSDN (ver artigo &lt;a href=&quot;http://msdn.microsoft.com/pt-br/library/ms189741(v=sql.105).aspx&quot;&gt;aqui&lt;/a&gt;) e adicionar alguns comentários em vermelho para deixar alguns pontos mais claros.&lt;br /&gt;
&lt;br /&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;3&quot; cellspacing=&quot;0&quot;&gt;&lt;tbody&gt;
&lt;tr style=&quot;background-color: black; color: white;&quot;&gt;&lt;th&gt;Nome da coluna&lt;/th&gt;&lt;th&gt;Tipo de dados&lt;/th&gt;&lt;th&gt;Descrição&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;sql_handle&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;varbinary(64)&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;É um token que se refere ao lote ou procedimento armazenado de que a consulta faz parte.  &lt;br /&gt;
Pode ser usado &lt;strong&gt;sql_handle&lt;/strong&gt;, junto com &lt;strong&gt;statement_start_offset&lt;/strong&gt; e &lt;strong&gt;statement_end_offset&lt;/strong&gt;, para recuperar o texto SQL da consulta, chamando a função de gerenciamento dinâmico &lt;strong&gt;sys.dm_exec_sql_text&lt;/strong&gt;.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;statement_start_offset&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;int&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Indica, em bytes, começando com 0, a posição inicial da consulta que a linha descreve dentro do texto de seu lote ou objeto persistente.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;statement_end_offset&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;int&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Indica, em bytes, começando com 0, a posição final da consulta que a linha descreve dentro do texto de seu lote ou objeto persistente. Um valor de -1 indica o fim do lote.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;plan_generation_num&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Um número de sequência que pode ser usado para distinguir entre instâncias de planos após uma recompilação.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;plan_handle&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;varbinary(64)&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Um token que se refere ao plano compilado de que a consulta faz parte. Este valor pode ser transmitido à função de gerenciamento dinâmico &lt;a href=&quot;http://msdn.microsoft.com/pt-br/library/ms189747(v=sql.105).aspx&quot;&gt;sys.dm_exec_query_plan&lt;/a&gt; para a obtenção do plano de consulta.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;creation_time&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;datetime&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Hora em que o plano foi compilado.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;last_execution_time&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;datetime&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Hora do início da execução do plano.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;execution_count&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Número de vezes que o plano foi executado desde sua última compilação.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;total_worker_time&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Tempo total da CPU, relatado em microssegundos (mas preciso somente em milissegundos), que foi consumido pelas execuções desse plano desde que foi compilado.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;last_worker_time&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Tempo de CPU, relatado em microssegundos (mas preciso somente em milissegundos), consumido na última vez em que o plano foi executado.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;min_worker_time&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Tempo de CPU mínimo, relatado em microssegundos (mas preciso somente em milissegundos), que esse plano já consumiu durante uma única execução.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;max_worker_time&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Tempo de CPU máximo, relatado em microssegundos (mas preciso somente em milissegundos), que esse plano já consumiu durante uma única execução.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;total_physical_reads&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Número total de leituras físicas efetuadas por execuções deste plano desde sua compilação.&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;Novamente, leitura física é pertinente ao acesso ao disco para recuperação de dados da tabela. Logical reads são leituras que acessam o cache de dados e também o disco. O uso de cache é basicamente o resultado da substração Logical Reads - Physical Reads.&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;last_physical_reads&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Número de leituras físicas efetuadas na última vez em que o plano foi executado.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;min_physical_reads&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Número mínimo de leituras físicas que este plano efetuou durante uma única execução.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;max_physical_reads&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Número máximo de leituras físicas que este plano efetuou durante uma única execução.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;total_logical_writes&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Número total de gravações lógicas efetuadas por execuções deste plano desde sua compilação.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;last_logical_writes&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Número de gravações lógicas efetuadas na última vez em que o plano foi executado.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;min_logical_writes&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Número mínimo de gravações lógicas que este plano efetuou durante uma única execução.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;max_logical_writes&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Número máximo de gravações lógicas que este plano efetuou durante uma única execução.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;total_logical_reads&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Número total de leituras lógicas efetuadas por execuções deste plano desde sua compilação.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;last_logical_reads&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Número de leituras lógicas efetuadas na última vez em que o plano foi executado.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;min_logical_reads&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Número mínimo de leituras lógicas que este plano efetuou durante uma única execução.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;max_logical_reads&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Número máximo de leituras lógicas que este plano efetuou durante uma única execução.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;total_clr_time&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;O tempo, relatado em microssegundos (mas preciso somente em milissegundos), consumido dentro de objetos CLR (Common Language Runtime) Microsoft&amp;nbsp;.NET Framework por execuções desse plano desde sua compilação. Os objetos CLR podem ser procedimentos armazenados, funções, disparadores, tipos e agregados.&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;Para quem usa objetos CLR, é uma ótima ficar de olho nestes indicadores pois pode existir uma regra de negócio no seu objeto que causa uma flutuação grande no tempo de execução.&amp;nbsp;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;last_clr_time&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Tempo, relatado em microssegundos (mas preciso somente em milissegundos), consumido pela execução dentro de objetos CLR .NET Framework durante a última execução desse plano. Os objetos CLR podem ser procedimentos armazenados, funções, disparadores, tipos e agregados.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;min_clr_time&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Tempo mínimo, relatado em microssegundos (mas preciso somente em milissegundos), que esse plano já consumiu dentro de objetos CLR .NET Framework durante uma única execução. Os objetos CLR podem ser procedimentos armazenados, funções, disparadores, tipos e agregados.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;max_clr_time&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Tempo máximo, relatado em microssegundos (mas preciso somente em milissegundos), que esse plano já consumiu dentro de CLR .NET Framework durante uma única execução. Os objetos CLR podem ser procedimentos armazenados, funções, disparadores, tipos e agregados.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;total_elapsed_time&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Tempo total decorrido, relatado em microssegundos (mas preciso somente em milissegundos), para execuções concluídas desse plano.&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;Conforme disse antes, instruções de espera como WAITFOR não são consideradas nesse contadores &quot;_elapsed_time&quot;.&lt;/span&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;last_elapsed_time&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Tempo decorrido, relatado em microssegundos (mas preciso somente em milissegundos), para a execução completa mais recente desse plano.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;min_elapsed_time&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Tempo decorrido mínimo, relatado em microssegundos (mas preciso somente em milissegundos), para qualquer execução concluída desse plano.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;max_elapsed_time&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Tempo decorrido máximo, relatado em microssegundos (mas preciso somente em milissegundos), para qualquer execução concluída desse plano.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;query_hash&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;binary(8)&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Valor de hash binário calculado na consulta e usado para identificar consultas com lógica semelhante. Você pode usar o hash de consulta para determinar o recurso agregado usado para consultas que são diferentes apenas nos valores literais. Para obter mais informações, consulte &lt;a href=&quot;http://msdn.microsoft.com/pt-br/library/cc645887(v=sql.105).aspx&quot;&gt;Localizando e ajustando consultas semelhantes usando consulta e hashes de plano de consulta&lt;/a&gt;.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;query_plan_hash&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;binary(8)&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;Valor de hash binário calculado no plano de execução de consulta e usado para identificar planos de execução de consulta semelhantes. Você pode usar o hash de plano de consulta para localizar o custo cumulativo de consultas com planos de execução semelhantes. Para obter mais informações, consulte &lt;a href=&quot;http://msdn.microsoft.com/pt-br/library/cc645887(v=sql.105).aspx&quot;&gt;Localizando e ajustando consultas semelhantes usando consulta e hashes de plano de consulta&lt;/a&gt;.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;total_rows&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;O número total de linhas retornadas pela consulta. Não pode ser nulo.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;last_rows&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;O número total de linhas retornadas pela última execução da consulta. Não pode ser nulo.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;min_rows&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;O número mínimo de linhas retornadas pela consulta no número de vezes em que o plano foi executado desde sua última compilação. Não pode ser nulo.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;max_rows&lt;/strong&gt;
            &lt;/td&gt;&lt;td&gt;&lt;span class=&quot;input&quot;&gt;bigint&lt;/span&gt;
            &lt;/td&gt;&lt;td&gt;O número máximo de linhas retornadas pela consulta no número de vezes em que o plano foi executado desde sua última compilação. Não pode ser nulo.&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;&lt;div&gt;
Então é isso pessoal. Qualquer dúvida ou caso queiram &quot;encomendar&quot; uma query, é só lançar nos comentários que responderei o mais rápido possível. Obrigado por visitar o blog e espero publicar novos posts logo mais. Abraços!&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://tiagobonamigo.blogspot.com/feeds/1317122258675101420/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://tiagobonamigo.blogspot.com/2012/08/forma-rapida-de-verificar-queries-que.html#comment-form' title='0 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5493816805450410906/posts/default/1317122258675101420'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5493816805450410906/posts/default/1317122258675101420'/><link rel='alternate' type='text/html' href='http://tiagobonamigo.blogspot.com/2012/08/forma-rapida-de-verificar-queries-que.html' title='Forma rápida de verificar queries que estão pesando muito no servidor - Parte II'/><author><name>Tiago</name><uri>http://www.blogger.com/profile/01881296233037002659</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total><georss:featurename>Porto Alegre - RS, Brasil</georss:featurename><georss:point>-30.0277041 -51.2287346</georss:point><georss:box>-30.2476641 -51.544591600000004 -29.8077441 -50.9128776</georss:box></entry><entry><id>tag:blogger.com,1999:blog-5493816805450410906.post-4619108693225704657</id><published>2012-07-26T21:25:00.003-03:00</published><updated>2012-08-19T22:49:51.949-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="desempenho"/><category scheme="http://www.blogger.com/atom/ns#" term="performance"/><category scheme="http://www.blogger.com/atom/ns#" term="queries"/><category scheme="http://www.blogger.com/atom/ns#" term="query"/><category scheme="http://www.blogger.com/atom/ns#" term="sql server 2005"/><category scheme="http://www.blogger.com/atom/ns#" term="sql server 2008"/><title type='text'>Forma rápida de verificar queries que estão pesando muito no servidor - Parte I</title><content type='html'>&lt;br /&gt;
Oi pessoal! Aí vai um dica muito boa tirada do &lt;a href=&quot;http://sqlserverpedia.com/wiki/Queries_in_Procedure_Cache&quot;&gt;SQLServerPedia&lt;/a&gt; e que já usei algumas vezes para detectar problemas de performance no servidor. Quando tem uma série de consultas onerando o servidor, uma das primeiras coisas que o DBA pensa é em ver os processos em execução e, caso não encontre um padrão, usar o Profiler. O Profiler, para quem não conhece, é uma ferramenta que consegue listar atividades ocorridas no SQL Server, então é possível utilizá-la para verificar o consumo de CPU e I/O de consultas de maneira comparativa.&lt;br /&gt;
&lt;br /&gt;
Existem situações em que usar o Profiler não é uma escolha: a execução do Profiler onera o servidor e pode piorar ainda mais a situação de um servidor sobrecarregado ou, ainda pior, o usuário pode ter só um Management Studio Express e não ter o Profiler instalado para fazer essa verificação. Nestes casos é possível, para SQL Server 2005 e 2008, apelar para as dynamic management views, as famosas DMVs. As DMVs fornecem informações extremamente valiosas para o gerenciamento de um servidor, e entre elas o procedure cache, que é um cache que armazena planos de execução corriqueiramente utilizados. 

Então, vamos à consulta:
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;select&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;
&lt;span style=&quot;color: teal;&quot;&gt;total_worker_time&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;execution_count&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: teal;&quot;&gt;MediaCPU&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white; color: teal; font-family: Consolas; font-size: 9.5pt;&quot;&gt;total_worker_time&lt;/span&gt;&lt;span style=&quot;background-color: white; font-family: Consolas; font-size: 9.5pt;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;background-color: white; color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;background-color: white; font-family: Consolas; font-size: 9.5pt;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;background-color: white; color: teal; font-family: Consolas; font-size: 9.5pt;&quot;&gt;TotalCPU&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: grey; font-family: Consolas; font-size: 9.5pt;&quot;&gt;,&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;
&lt;span style=&quot;color: teal;&quot;&gt;total_elapsed_time&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;execution_count&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: teal;&quot;&gt;MediaDuration&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: teal;&quot;&gt;total_elapsed_time&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt;
&lt;span style=&quot;color: teal;&quot;&gt;TotalDuration&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: grey; font-family: Consolas; font-size: 9.5pt;&quot;&gt;,&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;
&lt;span style=&quot;color: teal;&quot;&gt;total_logical_reads&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;execution_count&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: teal;&quot;&gt;MediaLogicalReads&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: teal;&quot;&gt;total_logical_reads&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt;
&lt;span style=&quot;color: teal;&quot;&gt;TotalLogicalReads&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: grey; font-family: Consolas; font-size: 9.5pt;&quot;&gt;,&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;
&lt;span style=&quot;color: teal;&quot;&gt;total_physical_reads&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;execution_count&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: teal;&quot;&gt;MediaPhysicalReads&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: teal;&quot;&gt;total_physical_reads&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt;
&lt;span style=&quot;color: teal;&quot;&gt;TotalPhysicalReads&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: grey; font-family: Consolas; font-size: 9.5pt;&quot;&gt;,&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;
&lt;span style=&quot;color: teal;&quot;&gt;execution_count&lt;/span&gt;&amp;nbsp;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: grey; font-family: Consolas; font-size: 9.5pt;&quot;&gt;,&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;
&lt;span style=&quot;color: magenta;&quot;&gt;substring&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;st&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;qs&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;statement_start_offset&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;/&lt;/span&gt;2&lt;span style=&quot;color: grey;&quot;&gt;)+&lt;/span&gt;1 &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: grey; font-family: Consolas; font-size: 9.5pt;&quot;&gt;,&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt; &lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;color: grey; font-family: Consolas; font-size: 9.5pt;&quot;&gt;((&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;case&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;
&lt;span style=&quot;color: teal;&quot;&gt;qs&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;statement_end_offset&lt;/span&gt;&amp;nbsp;
&lt;span style=&quot;color: blue;&quot;&gt;when&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;1 &lt;span style=&quot;color: blue;&quot;&gt;then&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;datalength&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;st&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;else&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;
&lt;span style=&quot;color: teal;&quot;&gt;qs&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;statement_end_offset&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;end&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;
&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt; &lt;span style=&quot;color: teal;&quot;&gt;qs&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;statement_start_offset&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)/&lt;/span&gt;2&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt; 1&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: teal;&quot;&gt;txt&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: grey; font-family: Consolas; font-size: 9.5pt;&quot;&gt;,&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;
&lt;span style=&quot;color: teal;&quot;&gt;query_plan&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;from&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;
&lt;span style=&quot;color: lime;&quot;&gt;sys&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: lime;&quot;&gt;dm_exec_query_stats&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt;
&lt;span style=&quot;color: teal;&quot;&gt;qs&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: grey; font-family: Consolas; font-size: 9.5pt;&quot;&gt;cross&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;
&lt;span style=&quot;color: grey;&quot;&gt;apply&lt;/span&gt; &lt;span style=&quot;color: lime;&quot;&gt;sys&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: lime;&quot;&gt;dm_exec_sql_text&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;qs&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;sql_handle&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: teal;&quot;&gt;st&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: grey; font-family: Consolas; font-size: 9.5pt;&quot;&gt;cross&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;
&lt;span style=&quot;color: grey;&quot;&gt;apply&lt;/span&gt; &lt;span style=&quot;color: lime;&quot;&gt;sys&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: lime;&quot;&gt;dm_exec_query_plan&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;qs&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;plan_handle&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: teal;&quot;&gt;qp&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;order&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;by&lt;/span&gt; 1 &lt;span style=&quot;color: blue;&quot;&gt;desc&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
Se você quer entender os resultados da query acima, basicamente são apresentados nos oito primeiros campos contadores de média e total para uso de CPU, duração da execução, logical reads (leituras de cache) e physical reads, que é leitura direta de valores no disco. Como os indicadores do SQL são inexatos é melhor tomá-los como medidas relativas. O &quot;order by 1 desc&quot; trará as consultas que tiveram mais peso em processamento, alternando para &quot;order by 5 desc&quot; se teria os mais pesados em logical reads e por aí vai. O execution_count apresenta o número de execuções, txt o corpo da consulta e query_plan acaba por apresentar o plano de execução.&lt;br /&gt;
&lt;br /&gt;
Nas primeiras consultas, utilize um &quot;top&quot; para restringir o número de registros retornados, principalmente se o cache comportar muitos registros.

Com essa query já se resolvem muitos problemas. Amanhã vou dissecar a sys.dm_exec_query_stats para que, caso vocês queiram, vocês possam aplicar em outros cenários de monitoramento.&lt;br /&gt;
&lt;br /&gt;
Novamente fico à disposição para quaisquer dúvidas. Se eu não souber, corro atrás. Abraços!&lt;br /&gt;
&lt;hr /&gt;
P.S.: Um amigo, ex-colega e leitor do blog, Jackson Barbian, sinalizou que não conseguiu executar o script no SQL Server 2005 pois estava apresentando erro de sintaxe. Descobrimos depois que o problema era o modo de compatibilidade do banco de dados com o SQL Server 2000, o que faz com que o CROSS APPLY não funcione. Nesses casos, acesse o banco de dados MASTER ou outro banco de dados e execute a query, pois os dados dela não são sensíveis ao banco de dados selecionado. Valeu Jackson!</content><link rel='replies' type='application/atom+xml' href='http://tiagobonamigo.blogspot.com/feeds/4619108693225704657/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://tiagobonamigo.blogspot.com/2012/07/forma-rapida-de-verificar-queries-que.html#comment-form' title='1 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5493816805450410906/posts/default/4619108693225704657'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5493816805450410906/posts/default/4619108693225704657'/><link rel='alternate' type='text/html' href='http://tiagobonamigo.blogspot.com/2012/07/forma-rapida-de-verificar-queries-que.html' title='Forma rápida de verificar queries que estão pesando muito no servidor - Parte I'/><author><name>Tiago</name><uri>http://www.blogger.com/profile/01881296233037002659</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5493816805450410906.post-3243969626546650203</id><published>2012-07-25T21:29:00.003-03:00</published><updated>2012-08-19T22:50:17.218-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="queries"/><category scheme="http://www.blogger.com/atom/ns#" term="query"/><category scheme="http://www.blogger.com/atom/ns#" term="sql server 2005"/><category scheme="http://www.blogger.com/atom/ns#" term="sql server 2008"/><category scheme="http://www.blogger.com/atom/ns#" term="stored procedures"/><title type='text'>Verificando em quais stored procedures e views uma tabela está sendo utilizada</title><content type='html'>Oi pessoal! Um problema muito frequente que encontro é mapear quais stored procedures e views utilizam uma determinada tabela. Claro, este método não vai capturar usos indiretos, como uma stored procedure que consulta uma view que consulta a tabela desejada, mas sim se o nome da tabela consta no código da stored procedure. Portanto CLR procedures também estão de fora. Mas vamos à query, funciona em 2005 e 2008, ainda vou testar no 2012:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt; obj&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;Name &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; [Nome da procedure]&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;sc&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;TEXT&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; [Conteudo da procedure]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: blue; font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;FROM&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt; &lt;span style=&quot;color: green;&quot;&gt;sys&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;syscomments&lt;/span&gt; sc&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: grey; font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;INNER&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt; &lt;span style=&quot;color: grey;&quot;&gt;JOIN&lt;/span&gt; &lt;span style=&quot;color: green;&quot;&gt;sys&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;objects&lt;/span&gt; obj &lt;span style=&quot;color: blue;&quot;&gt;ON&lt;/span&gt; sc&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;Id &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; obj&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;OBJECT_ID&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: blue; font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;WHERE&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt; sc&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;TEXT&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: grey;&quot;&gt;LIKE&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;%TABELA%&#39;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white; color: grey; font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;AND&lt;/span&gt;&lt;span style=&quot;background-color: white; font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;TYPE&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;P&#39;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Onde está o texto TABELA, você deve colocar o nome da tabela em questão. Se as procedures utilizam a convenção de nomenclatura de schema + tabela (Exemplo: dbo.tabela) você vai achar os registros facilmente. Se o nome da sua tabela estiver contida no nome de outra tabela ou view e vocês não usar essa convenção de nomenclatura, bem, podem pintar alguns resultados errados, mas ainda assim afunila o volume de procedures a se inspecionar.&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Ah, e para pegar views, é barbadinha. Troque a linha:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: grey; font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;AND&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;TYPE&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: red;&quot;&gt;&#39;P&#39;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;Por:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: grey; font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;AND&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;TYPE&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: red;&quot;&gt;&#39;V&#39;&lt;/span&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
E você listará as views onde o nome da tabela é encontrado. Pra manutenção em sistemas, é uma belezinha. Abração!&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://tiagobonamigo.blogspot.com/feeds/3243969626546650203/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://tiagobonamigo.blogspot.com/2012/07/verificando-em-quais-stored-procedures.html#comment-form' title='1 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5493816805450410906/posts/default/3243969626546650203'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5493816805450410906/posts/default/3243969626546650203'/><link rel='alternate' type='text/html' href='http://tiagobonamigo.blogspot.com/2012/07/verificando-em-quais-stored-procedures.html' title='Verificando em quais stored procedures e views uma tabela está sendo utilizada'/><author><name>Tiago</name><uri>http://www.blogger.com/profile/01881296233037002659</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5493816805450410906.post-8925995171298488883</id><published>2012-07-25T20:21:00.000-03:00</published><updated>2012-08-19T22:51:16.872-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="kill"/><category scheme="http://www.blogger.com/atom/ns#" term="sessões"/><category scheme="http://www.blogger.com/atom/ns#" term="sp_who"/><category scheme="http://www.blogger.com/atom/ns#" term="sp_who2"/><category scheme="http://www.blogger.com/atom/ns#" term="sql server 2005"/><category scheme="http://www.blogger.com/atom/ns#" term="sql server 2008"/><category scheme="http://www.blogger.com/atom/ns#" term="usuários"/><title type='text'>Script para finalizar processos de usuário provenientes de uma máquina específica em um banco específico</title><content type='html'>Oi pessoal, tudo bem? Primeiro post e uma dica resultante de um problema no trabalho hoje. Pintou um problema em que uma stored procedure fez um lock inadequado em uma tabela e enfileirou uma série de SELECTs e INSERTs feitos por agentes.&lt;br /&gt;
&lt;br /&gt;
Como esse enfileiramento acabou se tornando muito grande, tendo grande impacto no desempenho de outras aplicações, perguntei ao colega se haveria problema em finalizar estes processos enfileirados e ele disse que não. Pois bem, a única coisa que identificava estes SELECTs e INSERTs eram os servidores a partir dos quais as solicitações eram feitas. Então fiz rapidinho um script para finalizar todos os processos oriundos destes servidores em um banco de dados específico:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: blue; font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;declare&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt; @output &lt;span style=&quot;color: blue;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;max&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: blue; font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;set&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt; @output &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: blue; font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;select&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt; @output &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; @output &lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;kill &#39;&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;cast&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;spid &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(max&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;))&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;;&#39;&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;from&lt;/span&gt; &lt;span style=&quot;color: green;&quot;&gt;sys&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;sysprocesses&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: blue; font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;where&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt; &lt;span style=&quot;color: magenta;&quot;&gt;DB_NAME&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;dbid&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: red;&quot;&gt;&#39;[NOME DO BANCO]&#39;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span lang=&quot;EN-US&quot; style=&quot;color: grey; font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;and&lt;/span&gt;&lt;span lang=&quot;EN-US&quot; style=&quot;font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt; hostname&amp;nbsp;&lt;span style=&quot;color: grey;&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;[SERVIDOR1]&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;[SERVIDOR2]&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;[SERVIDOR3]&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;exec&lt;/span&gt;&lt;span style=&quot;color: grey; font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;@output&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
Resolveu o problema rapidinho. Caso tenham dúvidas, mandem para cá que tentarei respondê-las. Abraços!</content><link rel='replies' type='application/atom+xml' href='http://tiagobonamigo.blogspot.com/feeds/8925995171298488883/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://tiagobonamigo.blogspot.com/2012/07/script-para-finalizar-processos-de.html#comment-form' title='0 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5493816805450410906/posts/default/8925995171298488883'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5493816805450410906/posts/default/8925995171298488883'/><link rel='alternate' type='text/html' href='http://tiagobonamigo.blogspot.com/2012/07/script-para-finalizar-processos-de.html' title='Script para finalizar processos de usuário provenientes de uma máquina específica em um banco específico'/><author><name>Tiago</name><uri>http://www.blogger.com/profile/01881296233037002659</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>