Monday, August 27, 2012

SQL Server - retrieving record count instantaneously


So here it is, the first one of these not-so-original knowledge, but I didn't know until I needed it today.

In SQL Server it may take a long time to simply return a total record count using select count (*) from YourTable as the engine may decide to perform a scan instead of a seek.

Among other places this page helped me with a solution that retrieves the record count of a table instantaneously:

SQL 2005 or later
select sum (spart.rows)
from sys.partitions spart
where spart.object_id = object_id('YourTable')
and spart.index_id < 2

SQL 2000
select max(ROWS)
from sysindexes
where id = object_id('YourTable')

Your blog sucks!


Within the past 3 months two people made comments criticizing the fact my blog is dead. Not that they used to read it and then I suddenly I stopped producing content, which would be silly because the content is very very minimal.

These individuals don't even have a blog to begin with but they are right. If I had the initiative to start one why the hell didn't I keep up with it. It's been 16 months since my last post!

I believe it has to do with wanting to write something unique, or a nugget of information that is hard to find. But following the advice from one of my mentors (despite the fact he doesn't know I exist) Scott Hanselman, I will try to write anything that is useful to me, regardless if it's easily found throughout the internets.