To get the total row count in a table, we usually use the following
select statement:
|
This query will perform full table scan to get the row count.
You can check it by setting SET SHOWPLAN ON for SQL Server 6.5 or
SET SHOWPLAN_TEXT ON for SQL Server 7.0. So, if the table is very
big, it can take a lot of time. This is the example of simple table
creation and addition of new records into this table:
|
There is another way to determine the total row count in a table.
You can use sysindexes system table for this purpose. There is
field ROWS in the sysindexes table. This field contains the total
row count for each table in your database. So, you can use the
following select statement instead above one:
|
There are physical read and logical read operations. A logical read
occurs if the page is currently in the cache. If the page is not
currently in the cache, a physical read is performed to read the page
into the cache. To see how many logical or physical read operations
were made, you can use SET STATISTICS IO ON command.
This is the example:
|
These are the results:
|
So, you can improve the speed of the first query in several times.
This works for SQL Server 6.5 and SQL Server 7.0 as well.