Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Generic COUNT(*)


Generic COUNT(*)

Author
Message
stevanuz
stevanuz
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 40
Comments posted to this topic are about the item Generic COUNT(*)
Victor Shahar
Victor Shahar
Valued Member
Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)

Group: General Forum Members
Points: 55 Visits: 247
Hi,

Assuming that you use the auto statistics create & open and have all the statistics updated well, i think that it will be better to read from the statistics and not performing a real counting of the rows which is a litle bit physically i would say.

See my code and try it for huge tables and see the difference in time and I/O's.


ALTER function [dbo].[fn_UTL_GetRowCount]
(
@TableName sysname = null
)
returns bigint
as
begin

-- Example:
-- select dbo.fn_UTL_GetRowCount ('tblAccounts')
-- select top 10 name, dbo.fn_UTL_GetRowCount (name) from sys.tables

declare @out bigint
select @out = i.rowcnt
from sys.sysindexes i
where id = object_id(@TableName)
and (i.indid = 0 or i.indid = 1)
return @out

end
Kingston Dhasian
Kingston Dhasian
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3004 Visits: 5012
Victor Shahar (10/20/2010)
Hi,

Assuming that you use the auto statistics create & open and have all the statistics updated well, i think that it will be better to read from the statistics and not performing a real counting of the rows which is a litle bit physically i would say.

See my code and try it for huge tables and see the difference in time and I/O's.


ALTER function [dbo].[fn_UTL_GetRowCount]
(
@TableName sysname = null
)
returns bigint
as
begin

-- Example:
-- select dbo.fn_UTL_GetRowCount ('tblAccounts')
-- select top 10 name, dbo.fn_UTL_GetRowCount (name) from sys.tables

declare @out bigint
select @out = i.rowcnt
from sys.sysindexes i
where id = object_id(@TableName)
and (i.indid = 0 or i.indid = 1)
return @out

end


The sys.sysindexes System Table has been included as a view for backward compatibility. It might be removed in a future version of Microsoft SQL Server. Check the link below
http://msdn.microsoft.com/en-us/library/ms190283.aspx

So it is advisable to use the sys.dm_db_partition_stats System Table and the query would be similar
DECLARE @strTableName VARCHAR(100)

SET @strTableName = 'mstEmployees'

SELECT SUM(row_count)
FROM sys.dm_db_partition_stats
WHERE object_name( object_id ) = @strTableName
AND index_id IN ( 0, 1 )



You can also have a look at the discussion that followed the QOTD for 19th October 2010
http://www.sqlservercentral.com/Forums/Topic1006683-2739-1.aspx


Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
JohnFx
JohnFx
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 72
Is the point of this approach simply to turn an ad-hoc SQL command into a stored procedure? If so, I really don't see the benefit of an ad-hoc dynamic query implemented in a proc over an ad-hoc query in the data tier. That is, why not just build the COUNT query dynamically in the data tier for the relevant table?

This approach just seems like like a convoluted way to do something that adds complexity with no obvious benefit.
stevanuz
stevanuz
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 40
thanks all for the comments.

the use of the stored procedure 4 this solution might seem not so obvious but i do personally prefer sp instead of adhoc launched from data access since its easier to maintain; so i believe it's not abt right or wrong or overcomplicate things but about preferences.
JohnFx
JohnFx
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 72
stevanuz (10/22/2010)
thanks all for the comments.

the use of the stored procedure 4 this solution might seem not so obvious but i do personally prefer sp instead of ad-hoc launched from data access since its easier to maintain; so i believe it's not about right or wrong or over-complicate things but about preferences.



I think this may be an example of taking a good idea (encapsulating logic in a stored proc) and applying it without consideration of whether it is appropriate in the specific circumstance. If you want to encapsulate the logic, I'd suggest creating a centralized count method in your code that builds the SQL. You take on a lot of baggage using dynamic SQL that could create unintended side effects (for example permission and scope issues) with no benefit on the other end of that trade-off.
Iwas Bornready
Iwas Bornready
SSCrazy Eights
SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)

Group: General Forum Members
Points: 8712 Visits: 885
Thanks for the script.
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