<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>DBCC &amp; DMVs - Techyaz.com</title>
	<atom:link href="https://techyaz.com/category/sql-server/sql-server-dmvs/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>Tips, Tutorials and How-to Topics</description>
	<lastBuildDate>Mon, 14 May 2018 14:20:34 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8</generator>

<image>
	<url>https://techyaz.com/wp-content/uploads/2017/11/cropped-Site-icon-150x150.png</url>
	<title>DBCC &amp; DMVs - Techyaz.com</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Enable, Disable or Check SQL Server Trace Flags?</title>
		<link>https://techyaz.com/sql-server/how-to-enable-disable-or-check-sql-server-trace-flags/</link>
					<comments>https://techyaz.com/sql-server/how-to-enable-disable-or-check-sql-server-trace-flags/#comments</comments>
		
		<dc:creator><![CDATA[Manvendra Deo Singh]]></dc:creator>
		<pubDate>Sun, 01 Apr 2018 12:17:44 +0000</pubDate>
				<category><![CDATA[DBCC & DMVs]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Administration]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Troubleshooting]]></category>
		<category><![CDATA[DBCC]]></category>
		<category><![CDATA[SQL Server Configuration Manager]]></category>
		<category><![CDATA[trace flags]]></category>
		<guid isPermaLink="false">http://techyaz.com/?p=2047</guid>

					<description><![CDATA[<p>Trace flags are used to gather in-depth information about certain logs during some transactions. We use DBCC commands to enable trace flags, disable trace flags or check the status of trace flags. Here i am going to discuss about SQL&#46;&#46;&#46;</p>
<p>The post <a href="https://techyaz.com/sql-server/how-to-enable-disable-or-check-sql-server-trace-flags/">How to Enable, Disable or Check SQL Server Trace Flags?</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Trace flags are used to gather in-depth information about certain logs during some transactions. We use DBCC commands to enable trace flags, disable trace flags or check the status of trace flags. Here i am going to discuss about SQL Server Trace flag and how we can enable, disable or check their status whether they are enabled or disabled on your SQL Server Instance.</p>
<p>Let&#8217;s start with the basics. MSDN says &#8220;Trace flags are used to temporarily set specific server characteristics or to switch off a particular behavior. Trace flags are frequently used to diagnose performance issues or to debug stored procedures or complex computer systems.&#8221;</p>
<p>There are two types of trace flags in SQL Server.</p>
<ul>
<li>Session</li>
<li>Global</li>
</ul>
<p>We can enable trace flag for the context of a specific query that we can run under a session. Session trace flags can be enabled for a connection and are visible only to that connection. This type of trace flag never affects another session running on your database server and the effect of the trace flag is lost when that connection logs out.</p>
<p>Global trace flags are set at the server level and must be enabled globally. If you will try to enable global trace flag for a particular session then there will not be any impact of this trace flag. Let&#8217;s go ahead and understand how we can check the status of trace flags running on your SQL Server instance.</p>
<h3><span style="color: #333399;">Check Trace Flag Status</span></h3>
<p>We use <strong><em>DBCC TRACESTATUS</em></strong> command to check all running or active trace flags on database system. We can also check the status of a specific trace flag using this DBCC command. The output of this DBCC command has four columns. First column is the trace flag number, second column shows the status of trace flag, third and fourth columns are showing whether the trace flag is enabled at the global level or session level.</p>
<p>The status column of this DBCC command has two values. The value shows whether the trace flag is ON (1) or OFF (0). You can see the below screenshot to understand the trace flags and their status.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACESTATUS</strong></span></pre>
<p>We can run above DBCC command in different ways to get distinct outputs about trace flags status. Here, I am giving you few examples to use DBCC TRACESTATUS command.</p>
<p>Run below command to display the status of all trace flags that are currently enabled globally.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACESTATUS(-1);  </strong></span>
<span style="color: #0000ff;"><strong>GO</strong></span></pre>
<p>We can even get status of multiple specific trace flags by executing below command. Run below command to get the status of trace flags 8690 and 1118.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACESTATUS (8690, 1118);  </strong></span>
<span style="color: #0000ff;"><strong>GO</strong></span></pre>
<p>If you want to list all the trace flags that are enabled for the current session then you can get it by running below command.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACESTATUS();  </strong></span>
<span style="color: #0000ff;"><strong>GO</strong></span></pre>
<p>If you want to check whether trace flag status for specific trace is enabled globally or not then you can run below command.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACESTATUS(8690, -1)</strong></span></pre>
<h3><span style="color: #333399;">Enable Trace Flag</span></h3>
<p>We enable trace flags to gather more information about certain logs during some transactions. We use DBCC TRACEON command to enable SQL Server trace flags. Here, I will show you different uses of DBCC TRACEON command to enable trace flags.</p>
<p>Remember, all trace flags enabled using DBCC TRACEON command will be disabled once you restart the SQL Server service. If you want to enable any trace flag permanently then we recommend that you should enable them at startup, by using the <strong>-T</strong> command line option. This ensures the trace flag remains active after a server restart. You can launch <strong>SQL Server Configuration Manager</strong> and open the <strong>Properties</strong> window of <strong>SQL Server service</strong>. Here you can click on <strong>Startup Parameters</strong> tab. Here you can add your trace flag number along with <strong>-T</strong> and then click on Ok button to close the window.</p>
<p>If we need to enable a trace flag globally then we need to run below command with <strong>-1</strong> option as shown in below screenshot.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACEON (8690, -1)</strong></span></pre>
<p>Now we can check the trace flag status whether it’s been enabled globally on our database system or not.</p>
<p><img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-2048" src="http://techyaz.com/wp-content/uploads/2018/04/1-traceon-min.jpg" alt="Enable Trace flag globally DBCC TRACEON" width="691" height="127" srcset="https://techyaz.com/wp-content/uploads/2018/04/1-traceon-min.jpg 691w, https://techyaz.com/wp-content/uploads/2018/04/1-traceon-min-300x55.jpg 300w" sizes="(max-width: 691px) 100vw, 691px" /></p>
<p>If you want this trace to be enabled for current connection only then you should run below command. You can remove -1 from above command.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACEON (8690);  </strong></span>
<span style="color: #0000ff;"><strong>GO</strong></span></pre>
<p>Even you can enable multiple trace flags by running single command. You can run below command to enable trace flag 8690 and 1117 globally. If you want to enable it for current connection only then you can remove -1 from below command.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACEON (8690, 1117, -1);  </strong></span>
<span style="color: #0000ff;"><strong>GO</strong></span></pre>
<p>Run below DBCC command to check the status of the trace flag that we have enabled by running above command.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACESTATUS</strong></span></pre>
<p>You can see trace flag 8690 is showing along with its value set as<strong> 1</strong> for session as well as global in below screenshot.</p>
<p><img decoding="async" class="alignnone size-full wp-image-2049" src="http://techyaz.com/wp-content/uploads/2018/04/2-tracestatus-min.jpg" alt="Check trace flag status using DBCC TRACESTATUS" width="383" height="225" srcset="https://techyaz.com/wp-content/uploads/2018/04/2-tracestatus-min.jpg 383w, https://techyaz.com/wp-content/uploads/2018/04/2-tracestatus-min-300x176.jpg 300w" sizes="(max-width: 383px) 100vw, 383px" /></p>
<p>If you want to check trace flag status for specific trace number then you can run below command. Below command will let you know whether trace flag 8690 is enabled globally or not.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACESTATUS(8690, -1)</strong></span></pre>
<h3><span style="color: #333399;">Disable Trace Flag</span></h3>
<p>Similarly, we can disable trace flag 8690 that we have enabled in above section by executing another DBCC command known as DBCC TRACEOFF. Run below command to disable trace flag. If we will use -1 value along with DBCC TRACEOFF command that means it will disable this trace flag globally.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACEOFF(8690, -1)</strong></span></pre>
<p><img decoding="async" class="alignnone size-full wp-image-2050" src="http://techyaz.com/wp-content/uploads/2018/04/3-traceon-min.jpg" alt="Disable Trace flag using DBCC TRACEOFF" width="699" height="128" srcset="https://techyaz.com/wp-content/uploads/2018/04/3-traceon-min.jpg 699w, https://techyaz.com/wp-content/uploads/2018/04/3-traceon-min-300x55.jpg 300w" sizes="(max-width: 699px) 100vw, 699px" /></p>
<p>If you want to disable it for current connection only then we will remove <strong>-1</strong> from above command and run below command.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACEOFF(8690);   </strong></span>
<span style="color: #0000ff;"><strong>GO</strong></span></pre>
<p>Now we can again check the status of trace flag 8690 whether this trace is still active or it has been deactivated by above command.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACESTATUS</strong></span></pre>
<p>We can see trace 8690 is disabled now and this trace flag cannot be active in below image.</p>
<p><img loading="lazy" decoding="async" class="alignnone size-full wp-image-2051" src="http://techyaz.com/wp-content/uploads/2018/04/4-tracestatus-min.jpg" alt="dbcc tracestatus post deactivating a trace" width="394" height="214" srcset="https://techyaz.com/wp-content/uploads/2018/04/4-tracestatus-min.jpg 394w, https://techyaz.com/wp-content/uploads/2018/04/4-tracestatus-min-300x163.jpg 300w" sizes="auto, (max-width: 394px) 100vw, 394px" /></p>
<p>If you want to check trace flag status for a individual trace then you can run below command.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACESTATUS(8690, -1)</strong></span></pre>
<p><img loading="lazy" decoding="async" class="alignnone size-full wp-image-2052" src="http://techyaz.com/wp-content/uploads/2018/04/5-tracestatus-min.jpg" alt="Check trace flag globally enabled or not" width="396" height="137" srcset="https://techyaz.com/wp-content/uploads/2018/04/5-tracestatus-min.jpg 396w, https://techyaz.com/wp-content/uploads/2018/04/5-tracestatus-min-300x104.jpg 300w" sizes="auto, (max-width: 396px) 100vw, 396px" /></p>
<p>I hope you like this article. Please follow our <a href="https://www.facebook.com/Techyaz/" target="_blank" rel="noopener">Facebook</a> page and <a href="https://twitter.com/Tech_yaz" target="_blank" rel="noopener">Twitter</a> handle to get latest updates.</p>
<p><em><strong><span style="color: #800000;">Read More:</span></strong></em></p>
<ul>
<li><strong><a href="http://techyaz.com/sql-server/troubleshooting/to-find-out-no-of-traces-running-on-your-db-instance/" target="_blank" rel="noopener">Find Total no of Running Traces files on your Database Server</a></strong></li>
<li><strong><a href="http://techyaz.com/sql-server/avoid-database-shrink-operation/" target="_blank" rel="noopener">Understanding Database Shrink Operation</a></strong></li>
<li><strong><a href="http://techyaz.com/sql-server/how-to-avoid-page-split-in-sql-server/" target="_blank" rel="noopener">How to Avoid Page Split in SQL Server?</a></strong></li>
</ul>
<p>The post <a href="https://techyaz.com/sql-server/how-to-enable-disable-or-check-sql-server-trace-flags/">How to Enable, Disable or Check SQL Server Trace Flags?</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techyaz.com/sql-server/how-to-enable-disable-or-check-sql-server-trace-flags/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>4 Different Usages of DBCC SQLPERF in SQL Server</title>
		<link>https://techyaz.com/sql-server/sql-server-dmvs/4-different-usages-dbcc-sqlperf-sql-server/</link>
					<comments>https://techyaz.com/sql-server/sql-server-dmvs/4-different-usages-dbcc-sqlperf-sql-server/#respond</comments>
		
		<dc:creator><![CDATA[Manvendra Deo Singh]]></dc:creator>
		<pubDate>Mon, 08 Jan 2018 11:02:31 +0000</pubDate>
				<category><![CDATA[DBCC & DMVs]]></category>
		<category><![CDATA[Performance Tuning]]></category>
		<category><![CDATA[Wait Types]]></category>
		<category><![CDATA[DBCC]]></category>
		<category><![CDATA[dmv]]></category>
		<category><![CDATA[latches]]></category>
		<category><![CDATA[log space]]></category>
		<category><![CDATA[log_files]]></category>
		<category><![CDATA[transaction log]]></category>
		<category><![CDATA[wait statistics]]></category>
		<category><![CDATA[Waittypes]]></category>
		<guid isPermaLink="false">http://techyaz.com/?p=1445</guid>

					<description><![CDATA[<p>DBCC SQLPERF is very useful command that we use to get multiple statistics related information in SQL Server. This command provides transaction log space statistics and wait statistics information at instance level and we also use it to reset wait and&#46;&#46;&#46;</p>
<p>The post <a href="https://techyaz.com/sql-server/sql-server-dmvs/4-different-usages-dbcc-sqlperf-sql-server/">4 Different Usages of DBCC SQLPERF in SQL Server</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>DBCC SQLPERF is very useful command that we use to get multiple statistics related information in SQL Server. This command provides <a href="http://techyaz.com/sql-server/understanding-transaction-log-architecture/" target="_blank" rel="noopener">transaction log</a> space statistics and wait statistics information at instance level and we also use it to reset wait and latch statistics data. We can use DBCC SQLPERF command to do below tasks.</p>
<ol>
<li>Get log space size</li>
<li>View wait statistics data</li>
<li>Clear wait statistics data</li>
<li>Clear latch statistics data</li>
</ol>
<p>Let’s discuss each of above tasks as use cases of DBCC SQLPERF command.</p>
<p><em><strong><span style="color: #800000;">Read More</span></strong></em></p>
<ul>
<li><a href="http://techyaz.com/sql-server/get-total-virtual-log-files/" target="_blank" rel="noopener">How to get total no of VLFs (Virtual Log Files) inside Log file?</a></li>
<li><a href="http://techyaz.com/sql-server/understanding-sql-server-data-files-pages-extents/" target="_blank" rel="noopener">Understanding Data Files, Pages &amp; Extents</a></li>
<li><a href="http://techyaz.com/sql-server/how-to-avoid-page-split-in-sql-server/" target="_blank" rel="noopener">How to Avoid Page Split in SQL Server?</a></li>
</ul>
<h4><span style="color: #333399;">Get Transaction Log space using DBCC SQLPERF</span></h4>
<p>We can get transaction log space utilization details like how large your transaction logs are and what percentage of them is in use by executing dbcc sqlperf command. Run below command to get the log space details of all database hosted your SQL Server Instance.</p>
<div class="codediv">
<pre><strong><span style="color: #008000;">--Get Transaction Log Size Details</span>
<span style="color: #0000ff;">DBCC SQLPERF(logspace)
</span></strong></pre>
</div>
<p>You can see the below screenshot; all the columns are self-explanatory. We can see percent of log space used out of total space allocated to the log file.</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1447" src="http://techyaz.com/wp-content/uploads/2018/01/1-Check-log-space.jpg" alt="Check Log space utilization using dbcc sqlperf" width="417" height="236" srcset="https://techyaz.com/wp-content/uploads/2018/01/1-Check-log-space.jpg 417w, https://techyaz.com/wp-content/uploads/2018/01/1-Check-log-space-300x170.jpg 300w" sizes="auto, (max-width: 417px) 100vw, 417px" /></p>
<h4><span style="color: #333399;">View and Clear Wait Statistics Data</span></h4>
<p>We can get wait statistics data using dbcc sqlperf command as well. We used to get these details using dbcc sqlperf command in older version of SQL Server instances. Although, you can still get wait statistics data using this command in current versions of SQL Server but now there is a better way to get such details using DMV <strong>sys.dm_os_wait_stats</strong>.  Here, I will show you wait stats data using DMV only. Run below commands to get wait statistics details of your SQL Server instance.</p>
<div class="codediv">
<pre><strong><span style="color: #008000;">--Get wait Statistics using DBCC command.</span>
DBCC SQLPERF(waitstats)

<span style="color: #008000;">--Get Wait Statistics using DMV sys.dm_os_wait_stats</span>
SELECT * FROM sys.dm_os_wait_stats
Order by wait_time_ms desc
</strong></pre>
</div>
<p>You can see the details about all wait types on your SQL Server instance. You can also see the total wait time in milliseconds. These information is very important to deal with performance issues but details like total wait time, total waiting tasks count are not the real-time values. These values are accumulated since last SQL Server service start or last time wait stats was reset using dbcc sqlperf command. It is highly recommended to not clear wait stats data until it is required to do that otherwise you will lose valuable information about wait statistics. Read attached article about <a href="http://techyaz.com/sql-server/sql-server-dmvs/clear-wait-statistics-data-sql-server/" target="_blank" rel="noopener">why should you not clear wait statistics data stored in DMV sys.dm_os_wait_stats</a>.</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1448" src="http://techyaz.com/wp-content/uploads/2018/01/2-sys.dm_os_wait_stats.jpg" alt="View Wait stats data using DMV" width="643" height="300" srcset="https://techyaz.com/wp-content/uploads/2018/01/2-sys.dm_os_wait_stats.jpg 643w, https://techyaz.com/wp-content/uploads/2018/01/2-sys.dm_os_wait_stats-300x140.jpg 300w" sizes="auto, (max-width: 643px) 100vw, 643px" /></p>
<p>If you want to get the correct waiting task counts or waiting time then you need to clear the wait statistics data using dbcc sqlperf command. Here, I will show you the difference after clearing the wait stats. Run below command to clear all wait stats data on your SQL Server Instance.</p>
<div class="codediv">
<pre><strong>DBCC SQLPERF(“sys.dm_os_wait_stats”, CLEAR)
</strong></pre>
</div>
<p>You can see I have successfully ran this command in below screenshot.</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1449" src="http://techyaz.com/wp-content/uploads/2018/01/3-Clear-buffer-for-wait-stats.jpg" alt="clear wait stats" width="689" height="121" srcset="https://techyaz.com/wp-content/uploads/2018/01/3-Clear-buffer-for-wait-stats.jpg 689w, https://techyaz.com/wp-content/uploads/2018/01/3-Clear-buffer-for-wait-stats-300x53.jpg 300w" sizes="auto, (max-width: 689px) 100vw, 689px" /></p>
<p>Now we will again run same command to get the wait statistics data. I ran it and I get below data that is showing different than the one showing before clearing wait stats. This way you can better analyze your database performance and waiting tasks and time to reach on any conclusion.</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1450" src="http://techyaz.com/wp-content/uploads/2018/01/4-Check-wait-stats-after-clearning-buffer.jpg" alt="Check wait stats after clearing its buffer" width="804" height="283" srcset="https://techyaz.com/wp-content/uploads/2018/01/4-Check-wait-stats-after-clearning-buffer.jpg 804w, https://techyaz.com/wp-content/uploads/2018/01/4-Check-wait-stats-after-clearning-buffer-300x106.jpg 300w, https://techyaz.com/wp-content/uploads/2018/01/4-Check-wait-stats-after-clearning-buffer-768x270.jpg 768w" sizes="auto, (max-width: 804px) 100vw, 804px" /></p>
<p>&nbsp;</p>
<h4><span style="color: #333399;">Clear Latch Statistics Data</span></h4>
<p>I described about clearing wait stats data in above section. Here, I will show you how to clear latch statistics data. <b>Latches</b> are locks for RAM memory structures to prevent concurrent access and ensure serial execution of kernel code. You can run SQL command to check current latch statistics data.</p>
<div class="codediv">
<pre><span style="color: #008000;"><span style="color: #008000;"><b>--Get latch statistics data</b></span></span><b>
SELECT * FROM sys.dm_os_latch_stats
ORDER BY wait_time_ms desc
</b></pre>
</div>
<p>You can see current latch statistics details that is also not showing only real time value but it is accumulated data since last SQL Server service restarted or last time latch wait statistics was reset/cleared.</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1451" src="http://techyaz.com/wp-content/uploads/2018/01/5-Check-Latch.jpg" alt="Get Latch Statistics Data" width="678" height="217" srcset="https://techyaz.com/wp-content/uploads/2018/01/5-Check-Latch.jpg 678w, https://techyaz.com/wp-content/uploads/2018/01/5-Check-Latch-300x96.jpg 300w" sizes="auto, (max-width: 678px) 100vw, 678px" /></p>
<p>&nbsp;</p>
<p>Now we will clear these latch statistics data to get real time value of latch details. Run below dbcc sqlperf command to clear the latch statistics.</p>
<div class="codediv">
<pre><strong>DBCC SQLPERF(“sys.dm_os_latch_stats”, CLEAR)
</strong></pre>
</div>
<p>You can see we have successfully executed above command on our SQL Server Instance.</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1452" src="http://techyaz.com/wp-content/uploads/2018/01/6-Clear-stored-latch-details.jpg" alt="Clear Latch Statistics Data" width="722" height="121" srcset="https://techyaz.com/wp-content/uploads/2018/01/6-Clear-stored-latch-details.jpg 722w, https://techyaz.com/wp-content/uploads/2018/01/6-Clear-stored-latch-details-300x50.jpg 300w" sizes="auto, (max-width: 722px) 100vw, 722px" /></p>
<p>Now go ahead and run same SQL command to get the latch statistics again. You can see now there is no waiting requests on our SQL Server Instance and you are getting real time value about latches.</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1453" src="http://techyaz.com/wp-content/uploads/2018/01/7-check-latch-details-after-clearning-buffer.jpg" alt="latch details after cleaning its buffer data " width="677" height="197" srcset="https://techyaz.com/wp-content/uploads/2018/01/7-check-latch-details-after-clearning-buffer.jpg 677w, https://techyaz.com/wp-content/uploads/2018/01/7-check-latch-details-after-clearning-buffer-300x87.jpg 300w" sizes="auto, (max-width: 677px) 100vw, 677px" /></p>
<p><em><strong><span style="color: #800000;">Related Articles</span></strong></em></p>
<ul>
<li><a href="http://techyaz.com/sql-server/sql-server-dmvs/clear-wait-statistics-data-sql-server/" target="_blank" rel="noopener">Should We Clear Wait Stats Data from DMV sys.dm_os_wait_stats?</a></li>
<li><a href="http://techyaz.com/sql-server/backupbuffer-wait-type/" target="_blank" rel="noopener">How to Fix BACKUPBUFFER Wait Type?</a></li>
<li><a href="http://techyaz.com/sql-server/improve-sql-server-bulk-data-import-performance/" target="_blank" rel="noopener">How to Improve Bulk Data Load Performance?</a></li>
</ul>
<p>These are the 4 use cases of DBCC SQLPERF command that we use during different database activities. I hope you like this article. Please follow our <a href="https://www.facebook.com/Techyaz/">Facebook</a> page and <a href="https://twitter.com/Tech_yaz">Twitter </a>handle to get latest updates.</p>
<p>The post <a href="https://techyaz.com/sql-server/sql-server-dmvs/4-different-usages-dbcc-sqlperf-sql-server/">4 Different Usages of DBCC SQLPERF in SQL Server</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techyaz.com/sql-server/sql-server-dmvs/4-different-usages-dbcc-sqlperf-sql-server/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Should We Clear Wait Statistics Data from DMV sys.dm_os_wait_stats in SQL Server?</title>
		<link>https://techyaz.com/sql-server/performance-tuning/clear-wait-statistics-data-sql-server/</link>
					<comments>https://techyaz.com/sql-server/performance-tuning/clear-wait-statistics-data-sql-server/#respond</comments>
		
		<dc:creator><![CDATA[Manvendra Deo Singh]]></dc:creator>
		<pubDate>Thu, 28 Dec 2017 14:46:54 +0000</pubDate>
				<category><![CDATA[DBCC & DMVs]]></category>
		<category><![CDATA[Performance Tuning]]></category>
		<category><![CDATA[SQL Server Interview Questions]]></category>
		<category><![CDATA[DBA interview questions]]></category>
		<category><![CDATA[DBCC]]></category>
		<category><![CDATA[dmv]]></category>
		<category><![CDATA[performance tuning]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[wait statistics]]></category>
		<category><![CDATA[Waittypes]]></category>
		<guid isPermaLink="false">http://techyaz.com/?p=1412</guid>

					<description><![CDATA[<p>Should we clear or reset Performance Wait Statistics Data present in DMV sys.dm_os_wait_stats in SQL Server? The answer to this question is depends on case to case. Ideally, you should not clear your wait statistics data frequently. Best way is Do Not clear wait&#46;&#46;&#46;</p>
<p>The post <a href="https://techyaz.com/sql-server/performance-tuning/clear-wait-statistics-data-sql-server/">Should We Clear Wait Statistics Data from DMV sys.dm_os_wait_stats in SQL Server?</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Should we clear or reset Performance Wait Statistics Data present in DMV <strong>sys.dm_os_wait_stats </strong>in SQL Server? The answer to this question is depends on case to case. Ideally, you should not clear your wait statistics data frequently. Best way is Do Not clear wait stats data until it is required to gather information for any performance issue. Although, clearing wait stats has no impact on SQL Server performance. It will just remove information related to accumulated wait stats.</p>
<h5><span style="color: #333399;">Should We Clear Wait Stats Data in SQL Server?</span></h5>
<p>We do not recommend to clear the wait stats data frequently because all valuable information about wait statistics will be vanished if you clear them. Always try to capture wait statistics information before clearing wait stats. This way you would have both before and after data. You can use <strong>sys.dm_os_wait_stats</strong> DMV to get the SQL Server wait statistics information.</p>
<p>Generally, we reset or clear wait stats information to capture actual data during a given time duration because this DMV does not show only current data. The DMV sys.dm_os_wait_stats accumulates all the counts and wait times since last time SQL Server started or its stats got cleared by dbcc sqlperf command.</p>
<p>Here I will show you how to clear the SQL Server wait statistics and the difference between data captured before clearing the wait stats and after clearing the wait stats.</p>
<h5><span style="color: #333399;">Get Wait Statistics Data</span></h5>
<p>The DMV sys.dm_os_wait_stats provides essential metrics for diagnosing SQL Server performance problems. This DMV captures all the details like waiting requests counts, signal waits and waiting time since last time SQL Server instance got started or last time you have cleared your wait stats using DBCC SQLPERF command. Run below command to get performance wait stats details of your SQL Server instance.</p>
<pre><strong><span style="color: #008000;">--Get Wait Statistics using DMV sys.dm_os_wait_stats</span>
<span style="color: #0000ff;">SELECT * FROM sys.dm_os_wait_stats
Order by wait_time_ms desc
</span></strong></pre>
<p>You can see the details about all the wait types on your SQL Server instance. You can also see the total wait time in milliseconds. This data is very important if you are dealing with any performance issue but as i discussed above details like total wait time, total waiting tasks counts are the accumulated/incremental values.</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1414" src="http://techyaz.com/wp-content/uploads/2017/12/1-Get-wait-stats.jpg" alt="Get SQL Server Wait Stats Data" width="643" height="300" srcset="https://techyaz.com/wp-content/uploads/2017/12/1-Get-wait-stats.jpg 643w, https://techyaz.com/wp-content/uploads/2017/12/1-Get-wait-stats-300x140.jpg 300w" sizes="auto, (max-width: 643px) 100vw, 643px" /></p>
<h5><span style="color: #333399;">Reset or CLEAR Wait Statistics</span></h5>
<p>Now you have captured wait stats information since your SQL Server was started. If you want to get only current waiting task counts or waiting time then you need to reset or clear wait statistics data using dbcc sqlperf command. Run below command to clear all wait stats data on your SQL Server Instance.</p>
<pre><strong><span style="color: #0000ff;">DBCC SQLPERF(“sys.dm_os_wait_stats”, CLEAR)
</span></strong></pre>
<p>You can see I have successfully executed this command in below screenshot.</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1415" src="http://techyaz.com/wp-content/uploads/2017/12/2-Clear-wait-stats.jpg" alt="Clear Wait Stats" width="689" height="121" srcset="https://techyaz.com/wp-content/uploads/2017/12/2-Clear-wait-stats.jpg 689w, https://techyaz.com/wp-content/uploads/2017/12/2-Clear-wait-stats-300x53.jpg 300w" sizes="auto, (max-width: 689px) 100vw, 689px" /></p>
<p>Now we will again run same command to get the wait statistics data. I ran it and I get below information that is showing different than the one showing before clearing wait stats.</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1416" src="http://techyaz.com/wp-content/uploads/2017/12/3-Check-wait-stats-after-resetting-wait-stats.jpg" alt="Get wait stats after clearing its older data" width="804" height="283" srcset="https://techyaz.com/wp-content/uploads/2017/12/3-Check-wait-stats-after-resetting-wait-stats.jpg 804w, https://techyaz.com/wp-content/uploads/2017/12/3-Check-wait-stats-after-resetting-wait-stats-300x106.jpg 300w, https://techyaz.com/wp-content/uploads/2017/12/3-Check-wait-stats-after-resetting-wait-stats-768x270.jpg 768w" sizes="auto, (max-width: 804px) 100vw, 804px" /></p>
<p>You can see how you will lose valuable information if you will clear your wait stats without capture its earlier data. This way you can also better analyze your database wait types, waiting tasks to reach on any conclusion to fix the issue.</p>
<p><em><strong><span style="color: #800000;">Related Articles</span></strong></em></p>
<ul>
<li><a href="http://techyaz.com/sql-server/what-is-sql-server-wait-type-or-how-to-get-wait-type-info-in-sql-server/">Understanding SQL Server Wait types</a></li>
<li><a href="http://techyaz.com/sql-server/identify-disk-bottleneck-using-perfmon-counters/" target="_blank" rel="noopener">Identify Disk Bottleneck in SQL Server using Perfmon Disk Counters</a></li>
<li><a href="http://techyaz.com/sql-server/resource_semaphore-wait-type/" target="_blank" rel="noopener">Understanding Resource_Semaphore wait type</a></li>
</ul>
<p>Now you understand that we should not reset or clear wait stats frequently because wait stats data is very crucial to deal with any performance issue. You can reset or clear the wait stats but make sure to capture the data before clearing the wait stats. I hope you like this article. Please follow our <a href="https://www.facebook.com/Techyaz/">Facebook</a> page and <a href="https://twitter.com/Tech_yaz">Twitter </a>handle to get latest updates.</p>
<p>The post <a href="https://techyaz.com/sql-server/performance-tuning/clear-wait-statistics-data-sql-server/">Should We Clear Wait Statistics Data from DMV sys.dm_os_wait_stats in SQL Server?</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techyaz.com/sql-server/performance-tuning/clear-wait-statistics-data-sql-server/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Why is table row count showing incorrect value in sys.partitions or in DMV sys.dm_db_partition_stats</title>
		<link>https://techyaz.com/sql-server/t-sql/table-row-count-showing-incorrect-value-sys-partitions-dmv-sys-dm_db_partition_stats/</link>
					<comments>https://techyaz.com/sql-server/t-sql/table-row-count-showing-incorrect-value-sys-partitions-dmv-sys-dm_db_partition_stats/#comments</comments>
		
		<dc:creator><![CDATA[Manvendra Deo Singh]]></dc:creator>
		<pubDate>Tue, 05 Sep 2017 08:37:02 +0000</pubDate>
				<category><![CDATA[DBCC & DMVs]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Troubleshooting]]></category>
		<category><![CDATA[DBCC]]></category>
		<category><![CDATA[dmv]]></category>
		<category><![CDATA[row count]]></category>
		<guid isPermaLink="false">http://techyaz.com/?p=800</guid>

					<description><![CDATA[<p>Last week, I had a requirement to check the row counts of all the tables present in a database. I used attached article to get the row counts of all tables. We get row counts of all tables using system&#46;&#46;&#46;</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/table-row-count-showing-incorrect-value-sys-partitions-dmv-sys-dm_db_partition_stats/">Why is table row count showing incorrect value in sys.partitions or in DMV sys.dm_db_partition_stats</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Last week, I had a requirement to check the row counts of all the tables present in a database. I used attached article to <a href="https://techyaz.com/sql-server/get-row-counts-tables-database/" target="_blank" rel="noopener">get the row counts of all tables</a>. We get row counts of all tables using system catalog view sys.partitions or DMV sys.dm_db_partition_stats. But row counts were not showing correct for some of the tables. Here i will explain why row counts are not showing equal to the exact row count of the table.</p>
<p>Let&#8217;s check the row counts of tables using script that are using sys.partitions system catalog view and compare the output of a table with the output of SELECT COUNT (*) .</p>
<h3><span style="color: #000080;"><strong>Get Row Counts of All Tables using sys.partitions or DMV sys.dm_db_partition_stats</strong></span></h3>
<p>System catalog view sys.partitions contains a row for each partition of all the tables and most types of indexes in the database. All tables and indexes in SQL Server contain at least one partition, whether they are explicitly partitioned. Run below script to get the row count of all tables in a database. If you want to find row counts using DMV sys.dm_db_partition_stats, i would suggest you to visit our attached article <a href="https://techyaz.com/sql-server/get-row-counts-tables-database/" target="_blank" rel="noopener">how to find row counts of all tables in a database</a>.</p>
<pre class="brush: sql; title: ; notranslate" title=""><strong><span class="kwrd" style="color: blue;"><span style="color: #008000;">--Run below script to get the row counts of all tables in a database.</span>
SELECT      SCHEMA_NAME(O.schema_id) + '.' +
        O.Name As [Schema.TableName], SUM(P.rows) AS 'Total_RowCount'
FROM        sys.objects O
INNER JOIN sys.partitions P ON O.object_id = P.object_id
WHERE     O.type = 'U'
GROUP BY    O.schema_id, O.Name
</span></strong></pre>
<p>You can see row count of all tables in below screenshot.</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-801" src="http://techyaz.com/wp-content/uploads/2017/09/1-sys.patitions.jpg" alt="row count" width="649" height="390" srcset="https://techyaz.com/wp-content/uploads/2017/09/1-sys.patitions.jpg 649w, https://techyaz.com/wp-content/uploads/2017/09/1-sys.patitions-300x180.jpg 300w" sizes="auto, (max-width: 649px) 100vw, 649px" /></p>
<p>Now we will check row count of a table using SELECT COUNT(*) command. I ran below command and you can see that row count is not same here. Row count came using sys.partitions system catalog view has more value than the row count of SELECT COUNT (*).</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-802" src="http://techyaz.com/wp-content/uploads/2017/09/2-select-count.jpg" alt="" width="872" height="158" srcset="https://techyaz.com/wp-content/uploads/2017/09/2-select-count.jpg 872w, https://techyaz.com/wp-content/uploads/2017/09/2-select-count-300x54.jpg 300w, https://techyaz.com/wp-content/uploads/2017/09/2-select-count-768x139.jpg 768w" sizes="auto, (max-width: 872px) 100vw, 872px" /></p>
<p>As i said above &#8220;sys.partitions or DMV sys.dm_db_partition_stats contains a row for each partition of all the tables and most types of indexes in the database&#8221; so when you are fetching row counts of tables using sys.partitions system catalog view or DMV sys.dm_db_partition_stats, it will display the row counts after adding indexes row counts and table row counts. That is why value is showing more in sys.partitions or in sys.dm_db_partition_stats output.</p>
<p>Suppose you have a table with 5000 rows in it and you&#8217;ve defined 5 indexes on it, the above query would show 5*5000=25000 rows for that table. So, it isn&#8217;t just that some rows are inconsistence, it is that some rows are multiplied by the number of indexes they have.</p>
<p>If we need correct row count using the system cataog view sys.partitions then we need to put a condition in WHERE clause for index_id column. We can also get total row count of all the heap tables or for all the clustered index tables as well by changing the value of index_id mentioned in WHERE clause. Index_id value indicates the ID of the index within the object to which this table belongs.</p>
<ul>
<li>0 = heap</li>
<li>1 = clustered index</li>
<li>2 or greater = nonclustered index</li>
</ul>
<p>We can pass index_id value in above script to get the total row count of all tables whether it is heap or clustered index table. If you need total row count of all the tables present in a database, you should put index_id&lt;2 condition in WHERE clause. Run below scripts to get the row count of all heap &amp; clustered index tables.</p>
<pre class="brush: sql; title: ; notranslate" title=""><strong><span class="kwrd" style="color: blue;"><span style="color: #008000;">--Run below script to get the row counts of all tables in a database.
--Change the value of index_id to 0 or 1 to get total row count to heap or cluster index tables.
</span>SELECT      SCHEMA_NAME(O.schema_id) + '.' +
        O.Name As [Schema.TableName], SUM(P.rows) AS 'Total_RowCount'
FROM        sys.objects O
INNER JOIN sys.partitions P ON O.object_id = P.object_id
WHERE     O.type = 'U'
		AND P.index_id&lt;2  
GROUP BY    O.schema_id, O.Name
</span></strong></pre>
<p>Now you can run above script and compare the output with the SELECT COUNT(*) output. We can see row count for given table is showing same after adding idex_id condition in below screenshot.</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-803 size-full" src="http://techyaz.com/wp-content/uploads/2017/09/3.5.jpg" alt="row count of all table using sys.partitions" width="769" height="489" srcset="https://techyaz.com/wp-content/uploads/2017/09/3.5.jpg 769w, https://techyaz.com/wp-content/uploads/2017/09/3.5-300x191.jpg 300w, https://techyaz.com/wp-content/uploads/2017/09/3.5-768x488.jpg 768w" sizes="auto, (max-width: 769px) 100vw, 769px" /></p>
<p>I hope you like this article. You can comment us about your questions in below section. Please follow us on our <a href="https://www.facebook.com/Techyaz/">facebook page</a> and on <a href="https://twitter.com/Tech_yaz">Twitter </a>handle to get latest updates.</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/table-row-count-showing-incorrect-value-sys-partitions-dmv-sys-dm_db_partition_stats/">Why is table row count showing incorrect value in sys.partitions or in DMV sys.dm_db_partition_stats</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techyaz.com/sql-server/t-sql/table-row-count-showing-incorrect-value-sys-partitions-dmv-sys-dm_db_partition_stats/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>How to Get Row Count of All Tables of a Database</title>
		<link>https://techyaz.com/sql-server/t-sql/get-row-counts-tables-database/</link>
					<comments>https://techyaz.com/sql-server/t-sql/get-row-counts-tables-database/#respond</comments>
		
		<dc:creator><![CDATA[Manvendra Deo Singh]]></dc:creator>
		<pubDate>Wed, 30 Aug 2017 14:08:41 +0000</pubDate>
				<category><![CDATA[DBCC & DMVs]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Administration]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[dmv]]></category>
		<category><![CDATA[row count]]></category>
		<guid isPermaLink="false">http://techyaz.com/?p=703</guid>

					<description><![CDATA[<p>Last week, I had a requirement to check the row count of all tables having a specific schema of databases. I did some search on this because my database has thousands of tables and it was not possible to run&#46;&#46;&#46;</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/get-row-counts-tables-database/">How to Get Row Count of All Tables of a Database</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Last week, I had a requirement to check the row count of all tables having a specific schema of databases. I did some search on this because my database has thousands of tables and it was not possible to run SELECT COUNT (*) for each table individually. Here i will explain how to get row counts of all:</p>
<ul>
<li>Tables</li>
<li>Heap tables only</li>
<li>Clustered index tables only</li>
<li>Tables having specific Schema</li>
</ul>
<p>We can get total row counts of all tables using system catalog view <strong>sys.partitions</strong> or DMV <strong>sys.dm_db_partition_stats</strong>. Read attached article if you are getting <a href="https://techyaz.com/sql-server/troubleshooting/table-row-count-showing-incorrect-value-sys-partitions-dmv-sys-dm_db_partition_stats/" target="_blank" rel="noopener">wrong row counts using this DMV or catalog view</a>. Let&#8217;s check the row counts using both ways in this tip.</p>
<h3><span style="color: #000080;"><strong>Get Row Counts of Tables using sys.partitions</strong></span></h3>
<p>System catalog view sys.partitions contains a row for each partition of all the tables and most types of indexes in the database. Special index types such as Full-Text, Spatial, and XML are not included in this view. All tables and indexes in SQL Server contain at least one partition, whether or not they are explicitly partitioned. Run below script to get the row count of all tables in a database.</p>
<pre class="brush: sql; title: ; notranslate" title=""><span class="kwrd" style="color: blue;"><strong><span style="color: #008000;">--Run below script to get the row counts of all tables in a database.</span></strong>
<strong>SELECT      SCHEMA_NAME(O.schema_id) + '.' +SELECT      SCHEMA_NAME(O.schema_id) + '.' + O.name As [Schema.TableName],  SUM(P.rows) AS [Total_RowCount] </strong>
<strong>FROM        sys.objects O</strong>
<strong>INNER JOIN sys.partitions P ON O.object_id = P.object_id</strong>
<strong>WHERE     O.type = 'U' AND P.index_id&lt;2  </strong>
<strong>GROUP BY    O.schema_id, O.name</strong>
<strong>ORDER BY [Total_RowCount] desc
</strong></span></pre>
<p>You can see row count of all tables in below screenshot.</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-704" src="http://techyaz.com/wp-content/uploads/2017/08/1-row-count-of-all-tables.jpg" alt="Row Counts of All Tables" width="824" height="439" srcset="https://techyaz.com/wp-content/uploads/2017/08/1-row-count-of-all-tables.jpg 824w, https://techyaz.com/wp-content/uploads/2017/08/1-row-count-of-all-tables-300x160.jpg 300w, https://techyaz.com/wp-content/uploads/2017/08/1-row-count-of-all-tables-768x409.jpg 768w" sizes="auto, (max-width: 824px) 100vw, 824px" /></p>
<p>We can get total row count of all the heap tables or for all the clustered index tables as well by changing the value of <strong>index_id</strong> mentioned in WHERE clause. <strong>Index_id</strong> value indicates the ID of the index within the object/table. If <strong>index_id</strong> value is 0 that means that table doesn&#8217;t have any cluster index and a Heap table.</p>
<ul>
<li>0 = heap</li>
<li>1 = clustered index</li>
<li>2 or greater = nonclustered index</li>
</ul>
<p>We can pass <strong>index_id</strong> value in above script to get the total row count of all tables whether it is heap or clustered index table. Run below scripts to get the row count of all heap tables.</p>
<pre class="brush: sql; title: ; notranslate" title=""><strong><span class="kwrd" style="color: blue;"><span style="color: #008000;">--Run below script to get the row counts of all Heap (tables without cluster index) in a database.
--Change the value of index_id from 0 to 1 to get total row count to cluster index tables.</span>
<span style="color: #0000ff;">SELECT      SCHEMA_NAME(O.schema_id) + '.' +
        O.</span></span><span style="color: #0000ff;">n<span class="kwrd">ame As [Schema.TableName], SUM(P.rows) AS [Total_RowCount</span>]<span class="kwrd">
FROM        sys.objects O
INNER JOIN sys.partitions P ON O.object_id = P.object_id
WHERE     O.type = 'U'
		AND P.index_id=0  <span style="color: #008000;">--Change this value according to your need</span>
GROUP BY    O.schema_id, O.</span>n<span class="kwrd">ame
ORDER BY [Total_RowCount] desc
</span></span></strong></pre>
<p><strong><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-705" src="http://techyaz.com/wp-content/uploads/2017/08/2-row-count-of-all-heap-tables.jpg" alt="row count of all heap tables" width="805" height="279" srcset="https://techyaz.com/wp-content/uploads/2017/08/2-row-count-of-all-heap-tables.jpg 805w, https://techyaz.com/wp-content/uploads/2017/08/2-row-count-of-all-heap-tables-300x104.jpg 300w, https://techyaz.com/wp-content/uploads/2017/08/2-row-count-of-all-heap-tables-768x266.jpg 768w" sizes="auto, (max-width: 805px) 100vw, 805px" /></strong></p>
<p>You can see there are only two tables that have no cluster index in above screenshot. We can also get the <strong>row count of all tables having a specific schema</strong> by adding a condition in WHERE clause.</p>
<pre class="brush: sql; title: ; notranslate" title=""><strong><span style="color: #0000ff;"><span class="kwrd"><span style="color: #008000;">--Run below script to get the row count of all tables having dbo Schema in a database.</span>
SELECT      SCHEMA_NAME(O.schema_id) + '.' +
        O.name As [Schema.TableName], SUM(P.rows) AS [Total_RowCount</span>]<span class="kwrd">
FROM        sys.objects O
INNER JOIN sys.partitions P ON O.object_id = P.object_id
WHERE     O.type = 'U'
		AND P.index_id&lt;2  
		AND SCHEMA_NAME(O.schema_id) ='dbo'<span style="color: #008000;">--Change dbo with your schema name for which you need row counts</span>
GROUP BY    O.schema_id, O.</span>n<span class="kwrd">ame
ORDER BY [Total_RowCount] desc
</span></span></strong></pre>
<p>You can see the output in below image. You can make changes as per your need.</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-708" src="http://techyaz.com/wp-content/uploads/2017/08/4-row-count-of-all-tables-using-dmv.jpg" alt="row count of heap" width="690" height="339" srcset="https://techyaz.com/wp-content/uploads/2017/08/4-row-count-of-all-tables-using-dmv.jpg 690w, https://techyaz.com/wp-content/uploads/2017/08/4-row-count-of-all-tables-using-dmv-300x147.jpg 300w" sizes="auto, (max-width: 690px) 100vw, 690px" /></p>
<h3><span style="color: #000080;"><strong>Get Row Count of All Tables using DMV sys.dm_db_partition_stats</strong></span></h3>
<p>DMV sys.dm_db_partition_stats returns page and row-count information for every partition in the current database. We will leverage this DMV to get row count details of all tables. Run below script to get the row count of all tables using this DMV.</p>
<pre class="brush: sql; title: ; notranslate" title=""><span class="kwrd" style="color: blue;"><strong><span style="color: #008000;">--Run below script to get the row count of all tables in a database.</span>
SELECT QUOTENAME(SCHEMA_NAME(O.schema_id)) + '.' + QUOTENAME(O.name) AS [Schema.TableName]
      , SUM(p_dmv.row_count) AS [Total_RowCount]
FROM sys.objects AS O
      INNER JOIN sys.dm_db_partition_stats AS p_dmv
            ON O.object_id = p_dmv.object_id
WHERE  O.type = 'U'
        AND p_dmv.index_id &lt; 2
GROUP BY
      O.schema_id
      , O.name
ORDER BY [Schema.TableName]
GO</strong> 
</span></pre>
<p>We can see the output of above script in below image.</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-707" src="http://techyaz.com/wp-content/uploads/2017/08/3-row-count-of-all-tables-for-a-specific-schema.jpg" alt="row count of all tables using dmv" width="751" height="464" srcset="https://techyaz.com/wp-content/uploads/2017/08/3-row-count-of-all-tables-for-a-specific-schema.jpg 751w, https://techyaz.com/wp-content/uploads/2017/08/3-row-count-of-all-tables-for-a-specific-schema-300x185.jpg 300w" sizes="auto, (max-width: 751px) 100vw, 751px" /></p>
<p>We can pass index_id value in above script to get the total row count of all tables whether it is heap or clustered index table. Run below scripts to get the row count of all heap &amp; clustered index tables.</p>
<pre class="brush: sql; title: ; notranslate" title=""><span class="kwrd" style="color: blue;"><strong><span style="color: #008000;">--Run below script to get the row counts of all Heap (tables without cluster index) in a database.
--Change the value of index_id from 0 to 1 to get total row count to cluster index tables.
--Change the schema name if you want the row count of tables that are having specific schema.</span>
SELECT QUOTENAME(SCHEMA_NAME(O.schema_id)) + '.' + QUOTENAME(O.name) AS [Schema.TableName]
      , SUM(p_dmv.row_count) AS [Total_RowCount]
FROM sys.objects AS O
      INNER JOIN sys.dm_db_partition_stats AS p_dmv
            ON O.object_id = p_dmv.object_id
WHERE  O.type = 'U'
        AND p_dmv.index_id &lt; 2
	AND SCHEMA_NAME(O.schema_id) = 'dbo'<span style="color: #008000;">--Change the name of your Schema to get row count of all tables having this schema.</span>
GROUP BY
      O.schema_id</strong>
<strong>      , O.name
ORDER BY [Schema.TableName]
GO 
</strong></span></pre>
<p>And here is the output:</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-708" src="http://techyaz.com/wp-content/uploads/2017/08/4-row-count-of-all-tables-using-dmv.jpg" alt="row count of heap" width="690" height="339" srcset="https://techyaz.com/wp-content/uploads/2017/08/4-row-count-of-all-tables-using-dmv.jpg 690w, https://techyaz.com/wp-content/uploads/2017/08/4-row-count-of-all-tables-using-dmv-300x147.jpg 300w" sizes="auto, (max-width: 690px) 100vw, 690px" /></p>
<p>I hope you like this article. You can comment about your questions in comment section. Please follow us on our <a href="https://www.facebook.com/Techyaz/">facebook page</a> and on <a href="https://twitter.com/Tech_yaz">Twitter </a>handle to get latest updates.</p>
<p><span style="color: #800000;"><em><strong>Related Articles:</strong></em></span></p>
<ul>
<li><strong><a href="https://techyaz.com/sql-server/troubleshooting/table-row-count-showing-incorrect-value-sys-partitions-dmv-sys-dm_db_partition_stats/" target="_blank" rel="noopener">Why rou count showing incorrect value in sys.partitions?</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/understanding-different-types-sql-joins-examples/" target="_blank" rel="noopener">Understanding Different Types of SQL JOINs with Examples</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/create-logon-trigger-restrict-sysadmin-logins-connect-sql-server-given-time-interval/" target="_blank" rel="noopener">Create a Logon Trigger to Restrict sysadmin logins to Connect to SQL Server during a given time Interval</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/how-to-protect-my-stored-procedure-code/" target="_blank" rel="noopener">How to Protect Stored Procedure Code/Script?</a></strong></li>
</ul>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/get-row-counts-tables-database/">How to Get Row Count of All Tables of a Database</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techyaz.com/sql-server/t-sql/get-row-counts-tables-database/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
