Skip to content

Instantly share code, notes, and snippets.

@gertd
Last active January 17, 2017 22:01
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/70db7046c4f1d534ddebdccb1ec7cdc1 to your computer and use it in GitHub Desktop.
Save gertd/70db7046c4f1d534ddebdccb1ec7cdc1 to your computer and use it in GitHub Desktop.
SQL Server Get Datasize of Table
if object_id('dbo.DataSize', 'FN') is not null
begin
drop function dbo.DataSize
end
go
create function dbo.DataSize(@object_id int)
returns float
as
begin
declare @PageSize float
select @PageSize = v.low/1024.0 from master.dbo.spt_values v where v.number=1 and v.type=N'E'
declare @DataSize float
select @DataSize = case
when (tbl.is_memory_optimized=0) then
isnull((select @PageSize * sum(case when a.type <> 1 then a.used_pages when p.index_id < 2 then a.data_pages else 0 end)
from sys.indexes as i
join sys.partitions as p on p.object_id = i.object_id and p.index_id = i.index_id
join sys.allocation_units as a on a.container_id = p.partition_id
where i.object_id = tbl.object_id),0.0)
else
isnull((select (tms.[memory_used_by_table_kb])
from [sys].[dm_db_xtp_table_memory_stats] tms
where tms.object_id = tbl.object_id), 0.0)
end
from sys.tables AS tbl
where tbl.object_id = @object_id
return @DataSize
end
go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment