Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Can you count columns from sys.partition table, if not is select count(1) the fastest way ? Expand / Collapse
Author
Message
Posted Tuesday, May 6, 2014 12:42 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Saturday, July 19, 2014 9:15 PM
Points: 537, Visits: 1,110
Can you count columns from a variation of the following query ?

set statistics time on
SELECT
OBJECT_SCHEMA_NAME(object_id) as dbo
,OBJECT_NAME(object_id) as rows
,SUM(Rows) AS NumOfRows --sum the rows if there are multiple partitions
FROM
sys.partitions
WHERE
index_id < 2 --ignore the partitions from non-clustered indexes if any
and OBJECT_NAME(object_id) = 'table'
GROUP BY
OBJECT_ID
ORDER BY
NumOfRows DESC


If not, is the next query the fastest way?



set statistics time on
SELECT COUNT(1) cnt, company FROM table
group by company
order by cnt desc



Thanks
Post #1568139
Posted Tuesday, May 6, 2014 1:03 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 8:37 AM
Points: 40,596, Visits: 37,053
Yes, you can use sys.partitions if you want unfiltered row count. If you want to query the table directly, then SELECT COUNT(*) ...


Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1568147
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse