SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Can you count columns from sys.partition table, if not is select count(1) the fastest way ?


Can you count columns from sys.partition table, if not is select count(1) the fastest way ?

Author
Message
isuckatsql
isuckatsql
SSC Eights!
SSC Eights! (899 reputation)SSC Eights! (899 reputation)SSC Eights! (899 reputation)SSC Eights! (899 reputation)SSC Eights! (899 reputation)SSC Eights! (899 reputation)SSC Eights! (899 reputation)SSC Eights! (899 reputation)

Group: General Forum Members
Points: 899 Visits: 1110
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
GilaMonster
GilaMonster
SSC Guru
SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)

Group: General Forum Members
Points: 89993 Visits: 45284
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, MVP, M.Sc (Comp Sci)
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


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search