September 17, 2009 at 12:52 pm
The below query gives the Table name(user table) and the rows in each:
This is faster because we are getting the data from sysindexes.
SELECT object_name(id), rows
FROM sysindexes
WHERE INDID IN (0,1)
AND object_name(id) NOT LIKE 'sys%'
September 17, 2009 at 12:56 pm
SELECT object_name(id), rows
FROM sysindexes
WHERE INDID IN(0,1)
AND object_name(id) NOT LIKE 'sys%'
Gives all the tables and the rows in each table.
September 17, 2009 at 8:16 pm
SPK (9/17/2009)
SELECT object_name(id), rowsFROM sysindexes
WHERE INDID IN(0,1)
AND object_name(id) NOT LIKE 'sys%'
Gives all the tables and the rows in each table.
As has been said before in this thread, that won't be accurate unless you update usage first.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 16, 2009 at 4:52 am
Hi Kendal Van Dyke,
"How To Get Table Row Counts Quickly And Painlessly" really helpful and crisp snippet to find the rowcount of the table, my doubt while we are using select count(*) from
,in this we can use where clause to find the row count for the particular condition, select count(*) from
where [colname] = 'xyz'
how can i achieve this from your snippet?
once again thanks for your idea 🙂
___
Known Is An Drop,Unknown Is An Ocean....
Njoy Programming 🙂
February 9, 2010 at 2:46 am
Sometimes there is a need to get the record count of every table in a SQL Server database. The common method used for achieving this is doing a select count(*) from table_name but this can create a lot of overhead in large tables present in large databases.
The record count of every user table is maintained in the sysindexes system table . There is a indid column in the sysindexes table and every user table will have an entry in sysindexes with indid value of either 0 or 1 , but not both. The rowcnt column corresponding to indid 0 or 1 gives us the total row count of a table.
The User Defined Function given below shows how to retrieve row count for a table by making use of sysindexes.
February 9, 2010 at 3:51 am
bijayanix24 (2/9/2010)
...
'Team Mindfire' might like to read the other comments in this discussion. Much better solutions exist.
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
January 27, 2011 at 10:38 pm
Can anyone one told me what will be the shortest and fastest way of find the
count of table data
i am using
SELECT COUNT(*) FROM TblEmployee -- i want only count not else
what can i do for that
January 27, 2011 at 10:56 pm
U can Use below sql query to count the record from any user table
select rows
from sys.sysindexes
where id = object_id('Table_Name')
and indid < 2
January 28, 2011 at 12:07 am
If you use versions prior to 2005, you need to use dbcc updateusage to get accurate count
Failing to plan is Planning to fail
January 28, 2011 at 2:22 am
Since SQL 2005 I've tended to use Sys.Partitions over a Select Count(*), especially when I need to get row numbers across all tables in a specified DB and also across numerous DB's.
However, in future I will be adding the code to Update Stats prior to running the process, just to be sure the counts are as acurate as possible, may be with a 10-20 second delay.
My question is, would the Recovery Model for the DB impact the accuracy of the sys.Partitions and sys.dm_db_partition_stats views?
TIA.
_________________________________________________________________________
SSC Guide to Posting and Best Practices
January 28, 2011 at 2:48 am
rja.carnegie (9/2/2009)
I find the report of DBCC CHECKDB useful.Why doesn't "SELECT COUNT(*) FROM table" look at an index, which apparently it doesn't(?)
For 'select count(*) from
' the 2008 optimiser will scan the PK of the table if it has one. Not as good as (some of) the DMV queries, but not as bad as a full table scan.
January 28, 2011 at 6:14 am
Intresting post but not so much useful. Most of the time we need to know the count agaist with some filtered criteria. For me, I never come across the requirment like, show the count of table.
Anyway, good to know the way if required to get the total count of table.
January 28, 2011 at 6:59 am
I agree. I too am curious under what circumstances it would be useful to know the row-count from all tables.
Also under what circumstances is it useful to use 6 or 7 lines of query versus a single SELECT statement.
As a peek into the guts of the SQL Server system and thinking behind it, this is fascinating, but seems it would rarely be of use.
January 28, 2011 at 7:46 am
I agree with previous posters that this seems like a good way to find row counts for all tables in a database, but not a very good (easy, efficient, etc...) alternative to COUNT(*) on a single table within a query. I created the following proc and tested with my largest tables - hundreds of millions of rows with a non-sequential GUID pk and the results were generally 1 sec for COUNT(*) and 0 sec for the proc. Considering how I use COUNT(*) in my logic (and it is rarely used), it doesn't seem worth it - merely a nifty way to complicate things.
CREATE PROC spUtil_GetRowCount
(
@TableName VARCHAR(200)
)
AS
SELECT
ddps.row_count
FROM
sys.indexes i
INNER JOIN sys.objects o
ON i.OBJECT_ID = o.OBJECT_ID
INNER JOIN sys.dm_db_partition_stats ddps
ON i.OBJECT_ID = ddps.OBJECT_ID
AND i.index_id = ddps.index_id
WHERE
i.index_id < 2
AND o.is_ms_shipped = 0
AND o.name = @TableName
January 28, 2011 at 8:21 am
Nice article, but for SQL 2005 / 8 / r2, I just right-click and run the table usage report from sms. It also gives space used/free.
Viewing 15 posts - 76 through 90 (of 108 total)
You must be logged in to reply to this topic. Login to reply