|
|
|
Ever tried to estimate the table total size which include data and index in calculation ? Believe me its not easy job to estimate the size of a table for a give # of rows.
Here is the kool SP which does it for you. Full credit goes to Sharon Dooley for his excellent Job. |
Click here to copy the following block | if exists (select * from sysobjects where id = object_id(N'[dbo].[usp_CalcSpace]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[usp_CalcSpace] GO
create procedure usp_CalcSpace
(@table_name varchar(30)=null, @num_rows int = 0) as
declare @msg varchar(120)
if @table_name = null begin print 'Usage is:' print ' usp_CalcSpace table_name, no_of_rows' print 'where table_name is the name of the table,' print ' no_of_rows is the number of rows in the table,' print ' ' return end if @num_rows = 0 SELECT @num_rows=rows FROM sysindexes WHERE id = OBJECT_ID(@table_name) AND indid < 2
declare @num_fixed_col int, @fixed_data_size int, @num_variable_col int, @max_var_size int, @null_bitmap int, @variable_data_size int, @table_id int, @num_pages int, @table_size_in_bytes int, @table_size_in_meg real, @table_size_in_kbytes real, @sysstat int, @row_size int, @rows_per_page int, @free_rows_per_page int, @fillfactor int, @num_fixed_ckey_cols int, @fixed_ckey_size int, @num_variable_ckey_cols int, @max_var_ckey_size int, @cindex_null_bitmap int, @variable_ckey_size int, @cindex_row_size int, @cindex_rows_per_page int, @data_space_used int, @num_pages_clevel_0 int, @num_pages_clevel_1 int, @num_pages_clevel_x int, @num_pages_clevel_y int, @Num_CIndex_Pages int, @clustered_index_size_in_bytes int, @num_fixed_key_cols int, @fixed_key_size int, @num_variable_key_cols int, @max_var_key_size int, @index_null_bitmap int, @variable_key_size int, @nl_index_row_size int, @nl_index_rows_per_page int, @index_row_size int, @index_rows_per_page int, @free_index_rows_per_page int, @num_pages_level_0 int, @num_pages_level_1 int, @num_pages_level_x int, @num_pages_level_y int, @num_index_pages int, @nonclustered_index_size int, @total_num_nonclustered_index_pages int, @free_cindex_rows_per_page int, @tot_pages int
select @num_fixed_col =0, @fixed_data_size =0, @num_variable_col =0, @max_var_size =0, @null_bitmap =0, @variable_data_size =0, @table_id =0, @num_pages =0, @table_size_in_bytes =0, @table_size_in_meg =0, @table_size_in_kbytes =0, @sysstat =0, @row_size =0, @rows_per_page =0, @num_fixed_ckey_cols =0, @fixed_ckey_size =0, @num_variable_ckey_cols =0, @max_var_ckey_size =0, @cindex_null_bitmap =0, @variable_ckey_size =0, @cindex_row_size =0, @cindex_rows_per_page =0, @data_space_used =0, @num_pages_clevel_0 =0, @num_pages_clevel_1 =0, @Num_CIndex_Pages =0, @clustered_index_size_in_bytes =0, @num_fixed_key_cols =0, @fixed_key_size =0, @num_variable_key_cols =0, @max_var_key_size =0, @index_null_bitmap =0, @variable_key_size =0, @nl_index_row_size =0, @nl_index_rows_per_page =0, @index_row_size =0, @index_rows_per_page =0, @free_index_rows_per_page =0, @num_pages_level_0 =0, @num_pages_level_1 =0, @num_pages_level_x =0, @num_pages_level_y =0, @num_index_pages =0, @nonclustered_index_size =0, @total_num_nonclustered_index_pages =0, @free_cindex_rows_per_page =0, @tot_pages =0
set nocount on
select @sysstat = sysstat, @table_id = id from sysobjects where name = @table_name
if @sysstat & 7 not in (1,3) begin select @msg = 'I can''t find the table '+@table_name print @msg return end
select @num_fixed_col = count(name), @fixed_data_size = sum(length) from syscolumns where id= @table_id and xtype in ( select xtype from systypes where variable=0 )
if @num_fixed_col= 0 select @fixed_data_size=0
select @num_variable_col=count(name), @max_var_size= sum(length) from syscolumns where id= @table_id and xtype in ( select xtype from systypes where variable=1 ) if @num_variable_col= 0 select @max_var_size=0
select @null_bitmap=2+((@num_fixed_col+7)/8)
if @num_variable_col = 0 select @variable_data_size=0 else select @variable_data_size = 2 + (@num_variable_col *2 )+ @max_var_size
select @row_size= @fixed_data_size + @variable_data_size + @null_bitmap + 4
select @rows_per_page = (8096) / (@row_size+2)
select @fillfactor = 100 select @free_rows_per_page = 0 select @fillfactor=OrigFillFactor from sysindexes where id = @table_id and indid=1
if @fillfactor<>0
select @free_rows_per_page=8096 * ((100-@fillfactor)/100)/@row_size
select @num_pages = ceiling(convert(dec,@num_rows) / (@rows_per_page-@free_rows_per_page))
select @data_space_used=8192*@num_pages
select colid into #col_list from sysindexkeys where id= @table_id and indid=1
if (select count(*) from #col_list) >0 begin
select @num_fixed_ckey_cols=count(name), @fixed_ckey_size= sum(length) from syscolumns where id= @table_id and xtype in ( select xtype from systypes where variable=0 ) and colid in (select * from #col_list)
if @num_fixed_ckey_cols= 0 select @fixed_ckey_size=0
select @num_variable_ckey_cols=count(name), @max_var_ckey_size= sum(length) from syscolumns where id= @table_id and xtype in ( select xtype from systypes where variable=1 ) and colid in (select * from #col_list)
if @num_variable_ckey_cols= 0 select @max_var_ckey_size=0
if @num_fixed_ckey_cols <> 0 select @cindex_null_bitmap=2+((@num_fixed_ckey_cols + 7)/8) else select @cindex_null_bitmap=0
if @num_variable_ckey_cols <> 0 select @variable_ckey_size=2+(@num_variable_ckey_cols *2)+@max_var_ckey_size else select @variable_ckey_size=0
select @cindex_row_size=@fixed_ckey_size +@variable_ckey_size+@cindex_null_bitmap+1+8
select @cindex_rows_per_page=(8096)/(@cindex_row_size+2)
if @fillfactor=0 select @free_cindex_rows_per_page = 2 else select @free_cindex_rows_per_page= 8096 * ((100-@fillfactor)/100)/@cindex_row_size
select @num_pages_clevel_0=ceiling(convert(decimal,(@data_space_used/8192))/(@cindex_rows_per_page-@free_cindex_rows_per_page)) select @Num_CIndex_Pages=@num_pages_clevel_0 select @num_pages_clevel_x=@num_pages_clevel_0
while @num_pages_clevel_x <> 1 begin select @num_pages_clevel_y=ceiling(convert(decimal,@num_pages_clevel_x)/(@cindex_rows_per_page-@free_cindex_rows_per_page)) select @Num_CIndex_Pages=@Num_CIndex_Pages+@num_pages_clevel_y select @num_pages_clevel_x=@num_pages_clevel_y end end
select indid, colid into #col_list2 from sysindexkeys where id= @table_id and indid<>1
if (select count(*) from #col_list2) >0 begin declare @i int select @i=1
while @i< 249 begin select @i=@i+1
select @num_fixed_key_cols = 0, @fixed_key_size = 0, @num_variable_key_cols = 0, @max_var_key_size = 0, @index_null_bitmap = 0, @variable_key_size = 0, @nl_index_row_size = 0, @nl_index_rows_per_page = 0, @index_row_size = 0, @index_rows_per_page = 0, @free_index_rows_per_page = 0, @num_pages_level_0 = 0, @num_pages_level_x = 0, @num_pages_level_y = 0, @Num_Index_Pages = 0
select @num_fixed_key_cols=count(name), @fixed_key_size= sum(length) from syscolumns where id= @table_id and xtype in ( select xtype from systypes where variable=0 ) and colid in (select colid from #col_list2 where indid=@i) if @num_fixed_key_cols= 0 select @fixed_key_size=0
select @num_variable_key_cols=count(name), @max_var_key_size= sum(length) from syscolumns where id= @table_id and xtype in ( select xtype from systypes where variable=1 ) and colid in (select colid from #col_list2 where indid=@i) if @num_variable_key_cols= 0 select @max_var_key_size=0
if @num_fixed_key_cols = 0 and @num_variable_key_cols = 0 continue
if @num_fixed_key_cols <> 0 select @index_null_bitmap=2+((@num_fixed_key_cols + 7)/8) else select @index_null_bitmap=0
if @num_variable_key_cols <> 0 select @variable_key_size=2+(@num_variable_key_cols *2)+@max_var_key_size else select @variable_key_size=0
select @nl_index_row_size=@fixed_key_size +@variable_key_size+@index_null_bitmap+1+8
select @nl_index_rows_per_page=(8096)/(@nl_index_row_size+2)
select @index_row_size=@cindex_row_size + @fixed_key_size + @variable_key_size+@index_null_bitmap+1
select @index_rows_per_page = 8096/(@index_row_size + 2)
if @fillfactor=0
select @free_index_rows_per_page=0 else select @free_index_rows_per_page= 8096 * ((100-@fillfactor)/100)/@index_row_size
select @num_pages_level_0=ceiling(convert(decimal,@num_rows)/@index_rows_per_page-@free_index_rows_per_page)
select @Num_Index_Pages=@num_pages_level_0 select @num_pages_level_x=@num_pages_level_0
while @num_pages_level_x <> 1 begin select @num_pages_level_y=ceiling(convert(decimal,@num_pages_level_x)/@nl_index_rows_per_page) select @Num_Index_Pages=@Num_Index_Pages+@num_pages_level_y select @num_pages_level_x=@num_pages_level_y end
select @total_num_nonclustered_index_pages=@total_num_nonclustered_index_pages+@Num_Index_Pages end end
select @tot_pages=@num_pages + @Num_CIndex_Pages + @total_num_nonclustered_index_pages select @table_size_in_bytes= 8192*@tot_pages select @table_size_in_kbytes= @table_size_in_bytes/1024.0 select @table_size_in_meg= str(@table_size_in_kbytes/1000.0,17,2)
select substring(@table_name,1,20) as 'Table Name', @num_rows as 'Rows to estimate', convert(varchar(10),@table_size_in_meg) as 'MB Estimate', @tot_pages as 'Total Pages', @num_pages as '#Data Pgs', @Num_CIndex_Pages as '#Clustered Idx Pgs', @total_num_nonclustered_index_pages as '#NonClustered Idx Pgs' go
EXEC usp_CalcSpace 'DBTRACT'
go |
Table Name Rows to estimate MB Estimate Total Pages #Data Pgs #Clustered Idx Pgs #NonClustered Idx Pgs
-------------------- ---------------- ----------- ----------- ----------- ------------------ ---------------------
DBTRACT 660542 101.91 12739 12703 36 0 |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|