April 22, 2012

sp_estimated_rowsize_reduction_for_vardecimal (Transact-SQL MetaData) Definition

Please note: that the following source code is provided and copyrighted by Microsoft and is for educational purpose only.
The meta data is from an SQL 2012 Server.

I have posted alot more, find the whole list here.

Goto Definition or MetaData

Definition:

sys.sp_estimated_rowsize_reduction_for_vardecimal(nvarchar @table_name)

MetaData:

 create procedure sys.sp_estimated_rowsize_reduction_for_vardecimal    
@table_name nvarchar (776)
as
begin
set nocount on

-- Get the table ID.
declare @table_id int;
set @table_id = object_id(@table_name);

-- If we can't find an object with this name, or if it's not a user table, or if
-- it doesn't belong to this database, give up.
if (@table_id is null
or objectproperty(@table_id, 'IsUserTable') = 0
or not exists (select * from sys.tables where object_id = @table_id))
begin
raiserror(15388, -1, -1, @table_name);
return @@error;
end;

-- Get physical stats for the table.
create table #physical_stats(alloc_unit_type_desc nvarchar(60), record_count bigint, avg_record_size_in_bytes float);
insert into #physical_stats select alloc_unit_type_desc, record_count, avg_record_size_in_bytes from
sys.dm_db_index_physical_stats(db_id(), @table_id, objectproperty(@table_id, 'TableHasClustIndex'), 0, 'SAMPLED');

-- Get the table's size.
declare @table_size float;
select @table_size = sum(avg_record_size_in_bytes * record_count) from #physical_stats;

-- Get the row count.
declare @row_count bigint;
select @row_count = sum(record_count) from #physical_stats where alloc_unit_type_desc = 'IN_ROW_DATA';

-- Now estimate the average savings per row.
declare @avg_savings float;
exec sys.sp_decimal_estimate_savings_for_table @table_id, @avg_savings output;

declare @avg_rowlen_fixed_format float, @avg_rowlen_vardecimal_format float;
if (@row_count > 0)
begin
-- Get the average row size.
declare @avg_row_size float;
set @avg_row_size = @table_size / @row_count;

-- If the table is not compressed, subtract the savings from the actual size to get the
-- compressed size. If the table is already compressed, add the savings to the actual
-- size to get the uncompressed size.
if (objectproperty(@table_id, 'tablehasvardecimalstorageformat') = 0)
select @avg_rowlen_fixed_format = @avg_row_size, @avg_rowlen_vardecimal_format = @avg_row_size - @avg_savings;
else
select @avg_rowlen_fixed_format = @avg_row_size + @avg_savings, @avg_rowlen_vardecimal_format = @avg_row_size;
end
else
begin
select @avg_rowlen_fixed_format = 0, @avg_rowlen_vardecimal_format = 0;
end;

-- Select the result set.
select cast(@avg_rowlen_fixed_format as decimal(12, 2)) as avg_rowlen_fixed_format,
cast(@avg_rowlen_vardecimal_format as decimal(12, 2)) as avg_rowlen_vardecimal_format,
@row_count as row_count;
end;

No comments:

Post a Comment

Total Pageviews