Skip to content

Instantly share code, notes, and snippets.

@gertd
Last active January 17, 2017 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gertd/50cf74f179b38c50ed477990c5851732 to your computer and use it in GitHub Desktop.
Save gertd/50cf74f179b38c50ed477990c5851732 to your computer and use it in GitHub Desktop.
SQL Server Fast Row Count function
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