<?xml version="1.0" encoding="utf-8"?><!-- generator="b2evolution/6.11.7-stable" -->
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>=tg= Thomas Grohser - Category: "Useless for most people..."</title>
		<link>https://www.insidesql.org/blogs/tgrohser/</link>
		<atom:link rel="self" type="application/rss+xml" href="https://www.insidesql.org/blogs/tgrohser/?tempskin=_rss2" />
		<description>InsideSQL.org Blogs - Blogs über SQL Server</description>
		<language>en-EU</language>
		<docs>http://blogs.law.harvard.edu/tech/rss</docs>
		<admin:generatorAgent rdf:resource="http://b2evolution.net/?v=6.11.7-stable"/>
		<ttl>60</ttl>
				<item>
			<title>Found prove that SQL Server is the Devils Engine - ERROR 666</title>
			<link>https://www.insidesql.org/blogs/tgrohser/2011/08/05/found-prove-that-sql-server</link>
			<pubDate>Fri, 05 Aug 2011 14:37:00 +0000</pubDate>			<dc:creator>=tg=</dc:creator>
			<category domain="main">Useless for most people...</category>			<guid isPermaLink="false">3002@https://www.insidesql.org/blogs/</guid>
						<description>&lt;strong&gt;Msg 666&lt;/strong&gt;, Level 16, State 2, Line 1
The maximum system-generated unique value for a duplicate group was exceeded for index with partition ID 1234567890. 
Dropping and re-creating the index may resolve this; otherwise, use another clustering key.

It all started a few hours ago with the discussion on how Sql Server uses uniqueifiers to handle none unique clustered index keys.
After consulting SQL Server Internals books the answer was that SQL Server adds 4 extra bytes per row. So naturally the next question came up: signed or unsigned integer in the 4 bytes... No answer to that question so a test was mandadory

I will not show the source because nobody should do that. But i will dicribe the process: Create a table with one tinyint column
create a clusterd index on the column (do not specify unique). Insert a row with the value 1. Repeat the last step 214783647 times and enjoy the error message above and it&#039;s a signed int...

The uniqueifier is per value so even after you could no longer insert a value of 1 into the table you could insert 2 billion and change records with the values 2 and so on.</description>
			<content:encoded><![CDATA[<strong>Msg 666</strong>, Level 16, State 2, Line 1
The maximum system-generated unique value for a duplicate group was exceeded for index with partition ID 1234567890. 
Dropping and re-creating the index may resolve this; otherwise, use another clustering key.

It all started a few hours ago with the discussion on how Sql Server uses uniqueifiers to handle none unique clustered index keys.
After consulting SQL Server Internals books the answer was that SQL Server adds 4 extra bytes per row. So naturally the next question came up: signed or unsigned integer in the 4 bytes... No answer to that question so a test was mandadory

I will not show the source because nobody should do that. But i will dicribe the process: Create a table with one tinyint column
create a clusterd index on the column (do not specify unique). Insert a row with the value 1. Repeat the last step 214783647 times and enjoy the error message above and it's a signed int...

The uniqueifier is per value so even after you could no longer insert a value of 1 into the table you could insert 2 billion and change records with the values 2 and so on.]]></content:encoded>
								<comments>https://www.insidesql.org/blogs/tgrohser/2011/08/05/found-prove-that-sql-server#comments</comments>
			<wfw:commentRss>https://www.insidesql.org/blogs/tgrohser/?tempskin=_rss2&#38;disp=comments&#38;p=3002</wfw:commentRss>
		</item>
				<item>
			<title>increasing the performance of count(*)</title>
			<link>https://www.insidesql.org/blogs/tgrohser/2011/01/26/maximum-count-performance</link>
			<pubDate>Wed, 26 Jan 2011 12:14:00 +0000</pubDate>			<dc:creator>=tg=</dc:creator>
			<category domain="main">Useless for most people...</category>			<guid isPermaLink="false">2490@https://www.insidesql.org/blogs/</guid>
						<description>&lt;p&gt; &lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt;I got a very strange request: We have to constantly count the rows in a table can we increase the performance of count(*)&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt;So lets analyze the problem step by step.&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt;The table is a typical table the actual columns have no effect so I replaced them with just one column in the example to make things easier; Let&#039;s start by creating a heap&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt; &lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; font-size: 9.75pt;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;CREATE TABLE &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt; MyTable&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: gray; font-size: 9.75pt;&quot;&gt;(&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; font-size: 9.75pt;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;ID &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;NOT&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;NULL&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;IDENTITY&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;),&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; font-size: 9.75pt;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;Payload &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;300&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;NOT&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;NULL&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: gray; font-size: 9.75pt;&quot;&gt;)&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt; &lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt;and fill it with random data&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt; &lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; font-size: 9.75pt;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECLARE &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;@i &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: gray;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; font-size: 9.75pt;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;WHILE &lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;@i &lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt; 1000&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: blue; font-size: 9.75pt;&quot;&gt;BEGIN&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; font-size: 9.75pt;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;INSERT&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INTO&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt; MyTable(Payload) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;VALUES &lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: fuchsia;&quot;&gt;REPLICATE&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#039;ABC&#039;&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;, 20 + @i % 80));&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; font-size: 9.75pt;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt; @i += 1;&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; font-size: 9.75pt;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;END&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt; &lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; font-size: 9.75pt;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;Now lets see where we are on a heap&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Calibri; font-size: 11pt;&quot;&gt; &lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; font-size: 9.75pt;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;STATISTICS&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;IO&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;ON&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Calibri; font-size: 11pt;&quot;&gt; &lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; font-size: 9.75pt;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT &lt;/span&gt;&lt;span style=&quot;color: fuchsia;&quot;&gt;COUNT&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;(*)&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt; MyTable &lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt; &lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt;Table &#039;MyTable&#039;. Scan count 1,&lt;span style=&quot;background: yellow; font-weight: bold; mso-highlight: yellow;&quot;&gt; logical reads 25&lt;/span&gt;, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt; &lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt;To count the rows SQL Server must do a full table scan and reads each page exactly once.&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt;Let&#039;s see if an index will help us…&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt; &lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; font-size: 9.75pt;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;CREATE &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;CLUSTERED&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INDEX&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt; CI_MyTable &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt; MyTable&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;); &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT &lt;/span&gt;&lt;span style=&quot;color: fuchsia;&quot;&gt;COUNT&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;(*)&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt; MyTable&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: blue; font-size: 9.75pt;&quot;&gt; &lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt;Table &#039;MyTable&#039;. Scan count 1,&lt;span style=&quot;background: yellow; font-weight: bold; mso-highlight: yellow;&quot;&gt; logical reads 27&lt;/span&gt;, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: blue; font-size: 9.75pt;&quot;&gt; &lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt;SQL Server now performs a clustered index scan which is in fact a table scan plus the overhead to find the first page. Actually a little bit more work than the table scan.&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt;The trick to get it faster is to create an index that uses less space that the table (SQL Server will always select the smalest index to execute a COUNT(*))&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt; &lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt;The Smallest INDEX you can create on a table that has a clustered index is a secondary index that is composed just out of  the clustered key:&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: blue; font-size: 9.75pt;&quot;&gt; &lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; font-size: 9.75pt;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;CREATE INDEX&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt; IX_ID &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt; MyTable&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;);&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: blue; font-size: 9.75pt;&quot;&gt; &lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; font-size: 9.75pt;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT &lt;/span&gt;&lt;span style=&quot;color: fuchsia;&quot;&gt;COUNT&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;(*)&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt; MyTable&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: gray; font-size: 9.75pt;&quot;&gt; &lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt; &lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt;Table &#039;MyTable&#039;. Scan count 1,&lt;span style=&quot;background: yellow; font-weight: bold; mso-highlight: yellow;&quot;&gt; logical reads 3&lt;/span&gt;, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt; &lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt;Now it just has to Scan the leaf nodes of the secondary index&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt; &lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt;Careful this kind of index has very limited use besides the example above.&lt;/p&gt;
&lt;p style=&quot;margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;&quot;&gt;Other uses would be for example: SELECT xyz WHERE uvw  IN (SELECT ID FROM MyTable)&lt;/p&gt;</description>
			<content:encoded><![CDATA[<p> </p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;">I got a very strange request: We have to constantly count the rows in a table can we increase the performance of count(*)</p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;">So lets analyze the problem step by step.</p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;">The table is a typical table the actual columns have no effect so I replaced them with just one column in the example to make things easier; Let's start by creating a heap</p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;"> </p>
<p style="margin: 0in; font-family: Georgia; font-size: 9.75pt;"><span style="color: blue;">CREATE TABLE </span><span style="color: black;"> MyTable</span></p>
<p style="margin: 0in; font-family: Georgia; color: gray; font-size: 9.75pt;">(</p>
<p style="margin: 0in; font-family: Georgia; font-size: 9.75pt;"><span style="color: black;">ID </span><span style="color: blue;">int</span><span style="color: gray;">NOT</span><span style="color: gray;">NULL</span><span style="color: blue;">IDENTITY</span><span style="color: gray;">(</span><span style="color: black;">1</span><span style="color: gray;">,</span><span style="color: black;">1</span><span style="color: gray;">),</span></p>
<p style="margin: 0in; font-family: Georgia; font-size: 9.75pt;"><span style="color: black;">Payload </span><span style="color: blue;">varchar</span><span style="color: gray;">(</span><span style="color: black;">300</span><span style="color: gray;">)</span><span style="color: gray;">NOT</span><span style="color: gray;">NULL</span></p>
<p style="margin: 0in; font-family: Georgia; color: gray; font-size: 9.75pt;">)</p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;"> </p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;">and fill it with random data</p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;"> </p>
<p style="margin: 0in; font-family: Georgia; font-size: 9.75pt;"><span style="color: blue;">DECLARE </span><span style="color: black;">@i </span><span style="color: blue;">AS</span><span style="color: blue;">int</span><span style="color: gray;">=</span><span style="color: black;"> 1</span><span style="color: gray;">;</span></p>
<p style="margin: 0in; font-family: Georgia; font-size: 9.75pt;"><span style="color: blue;">WHILE </span><span style="color: gray;">(</span><span style="color: black;">@i </span><span style="color: gray;">&lt;</span><span style="color: black;"> 1000</span><span style="color: gray;">)</span></p>
<p style="margin: 0in; font-family: Georgia; color: blue; font-size: 9.75pt;">BEGIN</p>
<p style="margin: 0in; font-family: Georgia; font-size: 9.75pt;"><span style="color: blue;">INSERT</span><span style="color: blue;">INTO</span><span style="color: gray;"> MyTable(Payload) </span><span style="color: blue;">VALUES </span><span style="color: gray;">(</span><span style="color: fuchsia;">REPLICATE</span><span style="color: gray;">(</span><span style="color: red;">'ABC'</span><span style="color: gray;">, 20 + @i % 80));</span></p>
<p style="margin: 0in; font-family: Georgia; font-size: 9.75pt;"><span style="color: blue;">SET</span><span style="color: gray;"> @i += 1;</span></p>
<p style="margin: 0in; font-family: Georgia; font-size: 9.75pt;"><span style="color: blue;">END</span></p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;"> </p>
<p style="margin: 0in; font-family: Georgia; font-size: 9.75pt;"><span style="color: black;">Now lets see where we are on a heap</span></p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;"> </p>
<p style="margin: 0in; font-family: Georgia; font-size: 9.75pt;"><span style="color: blue;">STATISTICS</span><span style="color: blue;">IO</span><span style="color: blue;">ON</span></p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;"> </p>
<p style="margin: 0in; font-family: Georgia; font-size: 9.75pt;"><span style="color: blue;">SELECT </span><span style="color: fuchsia;">COUNT</span><span style="color: gray;">(*)</span><span style="color: blue;">FROM </span><span style="color: black;"> MyTable </span></p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;"> </p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;">Table 'MyTable'. Scan count 1,<span style="background: yellow; font-weight: bold; mso-highlight: yellow;"> logical reads 25</span>, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.</p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;"> </p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;">To count the rows SQL Server must do a full table scan and reads each page exactly once.</p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;">Let's see if an index will help us…</p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;"> </p>
<p style="margin: 0in; font-family: Georgia; font-size: 9.75pt;"><span style="color: blue;">CREATE </span><span style="color: black;"> </span><span style="color: blue;">CLUSTERED</span><span style="color: blue;">INDEX</span><span style="color: black;"> CI_MyTable </span><span style="color: blue;">ON</span><span style="color: black;"> MyTable</span><span style="color: gray;">(</span><span style="color: black;">ID</span><span style="color: gray;">); </span><span style="color: blue;">SELECT </span><span style="color: fuchsia;">COUNT</span><span style="color: gray;">(*)</span><span style="color: blue;">FROM</span><span style="color: black;"> MyTable</span><span style="color: gray;">;</span></p>
<p style="margin: 0in; font-family: Georgia; color: blue; font-size: 9.75pt;"> </p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;">Table 'MyTable'. Scan count 1,<span style="background: yellow; font-weight: bold; mso-highlight: yellow;"> logical reads 27</span>, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.</p>
<p style="margin: 0in; font-family: Georgia; color: blue; font-size: 9.75pt;"> </p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;">SQL Server now performs a clustered index scan which is in fact a table scan plus the overhead to find the first page. Actually a little bit more work than the table scan.</p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;">The trick to get it faster is to create an index that uses less space that the table (SQL Server will always select the smalest index to execute a COUNT(*))</p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;"> </p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;">The Smallest INDEX you can create on a table that has a clustered index is a secondary index that is composed just out of  the clustered key:</p>
<p style="margin: 0in; font-family: Georgia; color: blue; font-size: 9.75pt;"> </p>
<p style="margin: 0in; font-family: Georgia; font-size: 9.75pt;"><span style="color: blue;">CREATE INDEX</span><span style="color: black;"> IX_ID </span><span style="color: blue;">ON</span><span style="color: black;"> MyTable</span><span style="color: gray;">(</span><span style="color: black;">ID</span><span style="color: gray;">);</span></p>
<p style="margin: 0in; font-family: Georgia; color: blue; font-size: 9.75pt;"> </p>
<p style="margin: 0in; font-family: Georgia; font-size: 9.75pt;"><span style="color: blue;">SELECT </span><span style="color: fuchsia;">COUNT</span><span style="color: gray;">(*)</span><span style="color: blue;">FROM</span><span style="color: black;"> MyTable</span><span style="color: gray;">;</span></p>
<p style="margin: 0in; font-family: Georgia; color: gray; font-size: 9.75pt;"> </p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;"> </p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;">Table 'MyTable'. Scan count 1,<span style="background: yellow; font-weight: bold; mso-highlight: yellow;"> logical reads 3</span>, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.</p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;"> </p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;">Now it just has to Scan the leaf nodes of the secondary index</p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;"> </p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;">Careful this kind of index has very limited use besides the example above.</p>
<p style="margin: 0in; font-family: Georgia; color: black; font-size: 9.75pt;">Other uses would be for example: SELECT xyz WHERE uvw  IN (SELECT ID FROM MyTable)</p>]]></content:encoded>
								<comments>https://www.insidesql.org/blogs/tgrohser/2011/01/26/maximum-count-performance#comments</comments>
			<wfw:commentRss>https://www.insidesql.org/blogs/tgrohser/?tempskin=_rss2&#38;disp=comments&#38;p=2490</wfw:commentRss>
		</item>
			</channel>
</rss>
