zoukankan      html  css  js  c++  java
  • Using SQL statment to calculation tables space size in database

    Along with the growth of the time using ERP system, the database was growing. Apparently caused the ERP application program execution performance, therefore the boss wants to know what space size of the tables in database. Demand what measures.. ?

     Such as delete earlier historical records, etc. So I wrote a Storage process - > Jimmy_calcTablesSpaceSizeInDB

    remark by Jimmy on May 25th 2011. 

    use AX2009DEV
    go

    drop PROCEDURE [dbo].[Jimmy_calcTablesSpaceSizeInDB]
    go

    create procedure [dbo].[Jimmy_calcTablesSpaceSizeInDB]
    @dbName sysname = '', --数据库名,默认当前数据库
    @tableName sysname = '', --表名,默认全部表
    @columnName varchar(50) = '', --列名,排序用
    @sort varchar(4) = '' --asc升序,desc降序
    AS

    if (@dbName = '')
    set @dbName = DB_Name()
    else if (Charindex('M0A2_DB2',@dbName) > 0)
    set @dbName = 'M0A2_DB2.' + @dbName
    if (@tableName = '')
    set @tableName = '%'
    if (@columnName = '')
    set @columnName = 'name'
    if (@sort = '')
    set @sort = 'asc'

    exec ('SELECT a.name,rows as int,(reserved * 8) reserved,(data * 8) data,
    ((CASE WHEN used > data THEN (used - data) ELSE 0 END) * 8)index_size,
    ((CASE WHEN reserved > used THEN (reserved - used) ELSE 0 END) * 8)unused
    FROM
    ' + @dbName + '.sys.tables a INNER JOIN
    (SELECT object_id,SUM (reserved_page_count)reserved,SUM (used_page_count)used,
    SUM (CASE
    WHEN (index_id < 2) THEN (in_row_data_page_count + lob_used_page_count + row_overflow_used_page_count)
    ELSE lob_used_page_count + row_overflow_used_page_count END)data,
    SUM (CASE
    WHEN (index_id < 2) THEN row_count ELSE 0
    END)rows
    FROM
    ' + @dbName + '.sys.dm_db_partition_stats GROUP BY object_id)b
    ON a.object_id = b.object_id WHERE a.name LIKE
    ''' + @tableName + ''' ORDER BY ' + @columnName + ' ' + @sort)
    go



    sp_spaceused
    'SysDataBaseLog'

    exec Jimmy_calcTablesSpaceSizeInDB '','','data','desc'
  • 相关阅读:
    104. 二叉树的最大深度
    Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[]]
    python-admin管理后台
    django-cookies 和session
    django-关系映射
    django-关系映射 一对一 一对多 多对多
    django-Meta类
    django-orm聚合查询和原生数据库查询
    django-F对象、Q对象
    django-orm删除数据
  • 原文地址:https://www.cnblogs.com/Fandyx/p/2056829.html
Copyright © 2011-2022 走看看