Find out the Database Actual Size and used size ???

  • Hi to every one,

    How should i found the Database actual size and how much the used space ?

    Krish....

  • Is this the information you are seeking?

    CREATE PROCEDURE [dbo].[DBSize_TableSpace_Used]

    AS

    -- Author: Michael Valentine Jones

    -- Script to analyze table space usage using the

    -- output from the sp_spaceused stored procedure

    -- Works with SQL 7.0, 2000, and 2005

    set nocount on

    print 'Show Size, Space Used, Unused Space, Type, and Name of all database files'

    select

    [FileSizeMB]=

    convert(numeric(10,2),sum(round(a.size/128.,2))),

    [UsedSpaceMB]=

    convert(numeric(10,2),sum(round(fileproperty( a.name,'SpaceUsed')/128.,2))) ,

    [UnusedSpaceMB]=

    convert(numeric(10,2),sum(round((a.size-fileproperty( a.name,'SpaceUsed'))/128.,2))) ,

    [Type] =

    case when a.groupid is null then '' when a.groupid = 0 then 'Log' else 'Data' end,

    [DBFileName]= isnull(a.name,'*** Total for all files ***')

    from

    sysfiles a

    group by

    groupid,

    a.name

    with rollup

    having

    a.groupid is null or

    a.name is not null

    order by

    case when a.groupid is null then 99 when a.groupid = 0 then 0 else 1 end,

    a.groupid,

    case when a.name is null then 99 else 0 end,

    a.name

    create table #TABLE_SPACE_WORK

    (

    TABLE_NAME sysnamenot null ,

    TABLE_ROWS numeric(18,0)not null ,

    RESERVED varchar(50) not null ,

    DATA varchar(50) not null ,

    INDEX_SIZE varchar(50) not null ,

    UNUSED varchar(50) not null ,

    )

    create table #TABLE_SPACE_USED

    (

    Seqintnot null

    identity(1,1)primary key clustered,

    TABLE_NAME sysnamenot null ,

    TABLE_ROWS numeric(18,0)not null ,

    RESERVED varchar(50) not null ,

    DATA varchar(50) not null ,

    INDEX_SIZE varchar(50) not null ,

    UNUSED varchar(50) not null ,

    )

    create table #TABLE_SPACE

    (

    Seqintnot null

    identity(1,1)primary key clustered,

    TABLE_NAME SYSNAME not null ,

    TABLE_ROWS int not null ,

    RESERVED int not null ,

    DATA int not null ,

    INDEX_SIZE int not null ,

    UNUSED int not null ,

    USED_MBnumeric(18,4)not null,

    USED_GBnumeric(18,4)not null,

    AVERAGE_BYTES_PER_ROWnumeric(18,5)null,

    AVERAGE_DATA_BYTES_PER_ROWnumeric(18,5)null,

    AVERAGE_INDEX_BYTES_PER_ROWnumeric(18,5)null,

    AVERAGE_UNUSED_BYTES_PER_ROWnumeric(18,5)null,

    )

    declare @fetch_status int

    declare @proc varchar(200)

    select@proc= rtrim(db_name())+'.dbo.sp_spaceused'

    declare Cur_Cursor cursor local

    for

    select

    TABLE_NAME=

    rtrim(TABLE_SCHEMA)+'.'+rtrim(TABLE_NAME)

    from

    INFORMATION_SCHEMA.TABLES

    where

    TABLE_TYPE= 'BASE TABLE'

    order by

    1

    open Cur_Cursor

    declare @TABLE_NAME varchar(200)

    select @fetch_status = 0

    while @fetch_status = 0

    begin

    fetch next from Cur_Cursor

    into

    @TABLE_NAME

    select @fetch_status = @@fetch_status

    if @fetch_status <> 0

    begin

    continue

    end

    truncate table #TABLE_SPACE_WORK

    insert into #TABLE_SPACE_WORK

    (

    TABLE_NAME,

    TABLE_ROWS,

    RESERVED,

    DATA,

    INDEX_SIZE,

    UNUSED

    )

    exec @proc @objname =

    @TABLE_NAME ,@updateusage = 'true'

    -- Needed to work with SQL 7

    update #TABLE_SPACE_WORK

    set

    TABLE_NAME = @TABLE_NAME

    insert into #TABLE_SPACE_USED

    (

    TABLE_NAME,

    TABLE_ROWS,

    RESERVED,

    DATA,

    INDEX_SIZE,

    UNUSED

    )

    select

    TABLE_NAME,

    TABLE_ROWS,

    RESERVED,

    DATA,

    INDEX_SIZE,

    UNUSED

    from

    #TABLE_SPACE_WORK

    end --While end

    close Cur_Cursor

    deallocate Cur_Cursor

    insert into #TABLE_SPACE

    (

    TABLE_NAME,

    TABLE_ROWS,

    RESERVED,

    DATA,

    INDEX_SIZE,

    UNUSED,

    USED_MB,

    USED_GB,

    AVERAGE_BYTES_PER_ROW,

    AVERAGE_DATA_BYTES_PER_ROW,

    AVERAGE_INDEX_BYTES_PER_ROW,

    AVERAGE_UNUSED_BYTES_PER_ROW

    )

    select

    TABLE_NAME,

    TABLE_ROWS,

    RESERVED,

    DATA,

    INDEX_SIZE,

    UNUSED,

    USED_MB=

    round(convert(numeric(25,10),RESERVED)/

    convert(numeric(25,10),1024),4),

    USED_GB=

    round(convert(numeric(25,10),RESERVED)/

    convert(numeric(25,10),1024*1024),4),

    AVERAGE_BYTES_PER_ROW=

    case

    when TABLE_ROWS <> 0

    then round(

    (1024.000000*convert(numeric(25,10),RESERVED))/

    convert(numeric(25,10),TABLE_ROWS),5)

    else null

    end,

    AVERAGE_DATA_BYTES_PER_ROW=

    case

    when TABLE_ROWS <> 0

    then round(

    (1024.000000*convert(numeric(25,10),DATA))/

    convert(numeric(25,10),TABLE_ROWS),5)

    else null

    end,

    AVERAGE_INDEX_BYTES_PER_ROW=

    case

    when TABLE_ROWS <> 0

    then round(

    (1024.000000*convert(numeric(25,10),INDEX_SIZE))/

    convert(numeric(25,10),TABLE_ROWS),5)

    else null

    end,

    AVERAGE_UNUSED_BYTES_PER_ROW=

    case

    when TABLE_ROWS <> 0

    then round(

    (1024.000000*convert(numeric(25,10),UNUSED))/

    convert(numeric(25,10),TABLE_ROWS),5)

    else null

    end

    from

    (

    select

    TABLE_NAME,

    TABLE_ROWS,

    RESERVED=

    convert(int,rtrim(replace(RESERVED,'KB',''))),

    DATA=

    convert(int,rtrim(replace(DATA,'KB',''))),

    INDEX_SIZE=

    convert(int,rtrim(replace(INDEX_SIZE,'KB',''))),

    UNUSED=

    convert(int,rtrim(replace(UNUSED,'KB','')))

    from

    #TABLE_SPACE_USED aa

    ) a

    order by

    TABLE_NAME

    print 'Show results in descending order by size in MB'

    select * from #TABLE_SPACE order by USED_MB desc

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • 1.If you need to check at the individual file level-what the used space, free space and what is the auto growth then use the below query:

    Use <database Name>

    Go

    SELECTfilegroup_name(groupid),Name, Filename,

    CONVERT(Decimal(15,2),ROUND(a.Size/128.000,2)) [Currently Allocated Space (MB)],

    CONVERT(Decimal(15,2),ROUND(FILEPROPERTY(a.Name,'SpaceUsed')/128.000,2)) AS [Space Used (MB)],

    CONVERT(Decimal(15,2),ROUND((a.Size-FILEPROPERTY(a.Name,'SpaceUsed'))/128.000,2)) AS [Available Space (MB)]

    ,a.maxsize

    FROM dbo.sysfiles a (NOLOCK)

    2.You can use below query for the database level:

    USE <your dbname>;

    GO

    EXEC sp_spaceused @updateusage = N'TRUE';

    GO

    3.Suppose if you need to Identify what the Filegroup free space and used space you can use below

    declare @dbname varchar(256)

    declare @sql varchar(4000)

    IF (OBJECT_ID('tempdb..#space') IS NOT NULL)

    drop table #space

    IF (OBJECT_ID('tempdb..#fs') IS NOT NULL)

    drop table #fs

    IF (OBJECT_ID('tempdb..#fg') IS NOT NULL)

    drop table #fg

    create table #space

    (dbname varchar(256),

    fileid int,

    filegroup int,

    totalextents int,

    usedextents int,

    name varchar(255),

    filename varchar(1000))

    create table #fs

    (fileid int,

    filegroup int,

    totalextents int,

    usedextents int,

    name varchar(255),

    filename varchar(1000))

    create table #fg

    (dbname varchar(256),

    groupid int,

    groupname varchar(256))

    declare csdb cursor for

    select name

    from master.dbo.sysdatabases

    order by name

    open csdb

    fetch csdb into @dbname

    while @@fetch_status = 0

    begin

    set @sql = '

    use ' + @dbname + '

    insert into #fs

    exec (''DBCC showfilestats'')'

    exec (@sql)

    insert into #space

    select @dbname, *

    from #fs

    set @sql = '

    use ' + @dbname + '

    insert into #fg

    select ''' + @dbname + ''', groupid, groupname

    from sysfilegroups'

    exec (@sql)

    truncate table #fs

    fetch csdb into @dbname

    end

    close csdb

    deallocate csdb

    select g.dbname, g.groupname, sum((TotalExtents - UsedExtents) * 64.0 / 1024.0) AvailSpaceMB

    from #space f

    join #fg g on f.dbname = g.dbname

    and f.filegroup = g.groupid

    group by g.dbname, g.groupname

    order by g.dbname, g.groupname

    drop table #space

    drop table #fs

    drop table #fg

    I hope it useful for someone who needs..

    Thanks,

    Rama Udaya.K

    ramaudaya.blogspot.com

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply