<?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:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>=tg= Thomas Grohser - Latest Comments on increasing the performance of count(*)</title>
		<link>https://www.insidesql.org/blogs/tgrohser/?disp=comments</link>
		<atom:link rel="self" type="application/rss+xml" href="https://www.insidesql.org/blogs/tgrohser/?tempskin=_rss2&#38;disp=comments&#38;p=2490" />
		<description></description>
		<language>en-EU</language>
		<docs>http://backend.userland.com/rss</docs>
		<admin:generatorAgent rdf:resource="http://b2evolution.net/?v=6.11.7-stable"/>
		<ttl>60</ttl>
		<item>
			<title> Christoph Ingenhaag in response to: increasing the performance of count(*)</title>
			<pubDate>Thu, 24 Feb 2011 18:25:46 +0000</pubDate>
			<dc:creator><span class="user anonymous" rel="bubbletip_comment_1161">Christoph Ingenhaag</span></dc:creator>
			<guid isPermaLink="false">c1161@https://www.insidesql.org/blogs/</guid>
			<description>An indexed view is another choice. 

With 1100000 rows in MyTable a select makes 957 logical reads on my system using the IX_ID index.
A select on MyView (code follows) makes 2 logical reads

create view dbo.MyView 
with schemabinding
as
select
    count_big(*) as cnt
from dbo.MyTable
go

create unique clustered index cuidx on MyView(Cnt)
go

select cnt from MyView with (noexpand)

It is interessting the noexpand hint is necessary with more then 1000000 rows in MyTable on my system... (with Express Editions you need this hint)

And, the inserts are faster without the IX_ID Index. The update of the indexed view costs almost nothing. To check this I used the number function from Steve Kass (http://stevekass.com/2006/06/03/how-to-generate-a-sequence-on-the-fly/) and this statement:

insert into MyTable(Payload) 
select replicate(&#039;ABC&#039;, 100)
from dbo.numbers(1, 100000) 

Please check the plan. Maybe I have overseen something.&lt;br /&gt;</description>
			<content:encoded><![CDATA[An indexed view is another choice. 

With 1100000 rows in MyTable a select makes 957 logical reads on my system using the IX_ID index.
A select on MyView (code follows) makes 2 logical reads

create view dbo.MyView 
with schemabinding
as
select
    count_big(*) as cnt
from dbo.MyTable
go

create unique clustered index cuidx on MyView(Cnt)
go

select cnt from MyView with (noexpand)

It is interessting the noexpand hint is necessary with more then 1000000 rows in MyTable on my system... (with Express Editions you need this hint)

And, the inserts are faster without the IX_ID Index. The update of the indexed view costs almost nothing. To check this I used the number function from Steve Kass (http://stevekass.com/2006/06/03/how-to-generate-a-sequence-on-the-fly/) and this statement:

insert into MyTable(Payload) 
select replicate('ABC', 100)
from dbo.numbers(1, 100000) 

Please check the plan. Maybe I have overseen something.<br />]]></content:encoded>
			<link>https://www.insidesql.org/blogs/tgrohser/2011/01/26/maximum-count-performance#c1161</link>
		</item>
		<item>
			<title>admin in response to: increasing the performance of count(*)</title>
			<pubDate>Wed, 26 Jan 2011 19:33:36 +0000</pubDate>
			<dc:creator><a href="http://www.insidesql.org/blogs/" title="Show the user profile" class="login user nowrap" rel="bubbletip_user_1"><span class="identity_link_username">admin</span></a></dc:creator>
			<guid isPermaLink="false">c1145@https://www.insidesql.org/blogs/</guid>
			<description>LOL. 

Agreed, in that table dimensions I guess it is really not worth it departing from the &quot;standard&quot; way of doing things. :-)</description>
			<content:encoded><![CDATA[LOL. 

Agreed, in that table dimensions I guess it is really not worth it departing from the "standard" way of doing things. :-)]]></content:encoded>
			<link>https://www.insidesql.org/blogs/tgrohser/2011/01/26/maximum-count-performance#c1145</link>
		</item>
		<item>
			<title>tgrohser in response to: increasing the performance of count(*)</title>
			<pubDate>Wed, 26 Jan 2011 14:53:15 +0000</pubDate>
			<dc:creator><a href="https://www.insidesql.org/blogs/tgrohser/?disp=user&amp;user_ID=17" title="Show the user profile" class="login user nowrap" rel="bubbletip_user_17"><span class="identity_link_username">tgrohser</span></a></dc:creator>
			<guid isPermaLink="false">c1143@https://www.insidesql.org/blogs/</guid>
			<description>The actual problem was in the size of about 0 to 2000 rows, so not a huge table, the exact count was not 100% relevant, we thought about query in the system tables too but we found out that the actual work for sql server in this size of table was smaller letting him count than finding the right object, the coresponding partitions and then reading the result.
Sure there is overhead for the extra index but the counting was done much more often then inserts.

for large tables I totaly agree the system tables are the much better way to go</description>
			<content:encoded><![CDATA[The actual problem was in the size of about 0 to 2000 rows, so not a huge table, the exact count was not 100% relevant, we thought about query in the system tables too but we found out that the actual work for sql server in this size of table was smaller letting him count than finding the right object, the coresponding partitions and then reading the result.
Sure there is overhead for the extra index but the counting was done much more often then inserts.

for large tables I totaly agree the system tables are the much better way to go]]></content:encoded>
			<link>https://www.insidesql.org/blogs/tgrohser/2011/01/26/maximum-count-performance#c1143</link>
		</item>
		<item>
			<title>admin in response to: increasing the performance of count(*)</title>
			<pubDate>Wed, 26 Jan 2011 13:22:49 +0000</pubDate>
			<dc:creator><a href="http://www.insidesql.org/blogs/" title="Show the user profile" class="login user nowrap" rel="bubbletip_user_1"><span class="identity_link_username">admin</span></a></dc:creator>
			<guid isPermaLink="false">c1142@https://www.insidesql.org/blogs/</guid>
			<description>If I were to perform a COUNT(*) constantly on a large table I would maybe revise this strategy and question the requirement at all. Even with a tailored index just to support that query, the actual work still has to be carried out by SQL Server and I wouldn&#039;t be surprised when the query still runs like a dog.

However, if accuracy of the COUNT(*) isn&#039;t important at all, say for example, if you want to use this for some kind of paging, or track growth over time, or other stuff where you can live with a more or less &quot;good approximation&quot;, it might be an option to get the row_count from the system tables such as sys.partitions. 
Of course, with the usual caveat, that system tables can change over time...</description>
			<content:encoded><![CDATA[If I were to perform a COUNT(*) constantly on a large table I would maybe revise this strategy and question the requirement at all. Even with a tailored index just to support that query, the actual work still has to be carried out by SQL Server and I wouldn't be surprised when the query still runs like a dog.

However, if accuracy of the COUNT(*) isn't important at all, say for example, if you want to use this for some kind of paging, or track growth over time, or other stuff where you can live with a more or less "good approximation", it might be an option to get the row_count from the system tables such as sys.partitions. 
Of course, with the usual caveat, that system tables can change over time...]]></content:encoded>
			<link>https://www.insidesql.org/blogs/tgrohser/2011/01/26/maximum-count-performance#c1142</link>
		</item>
			</channel>
</rss>
