Last active
January 17, 2017 22:09
-
-
Save gertd/50cf74f179b38c50ed477990c5851732 to your computer and use it in GitHub Desktop.
SQL Server Fast Row Count function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if object_id('dbo.FastCount', 'FN') is not null | |
begin | |
drop function dbo.FastCount | |
end | |
go | |
create function dbo.FastCount(@object_id int) | |
returns bigint | |
as | |
begin | |
declare @fastcount bigint = 0 | |
select @fastcount = sum (p.rows) | |
from sys.tables as t | |
inner join sys.partitions as p on p.object_id = t.object_id | |
where p.index_id < 2 | |
and t.object_id = @object_id | |
return @fastcount | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment