分享

数据库大小,表大小及行数统计

 WindySky 2017-06-06
  1. -- 统计某个表的空间大小, 行数信息  
  2. EXEC Your_DB.dbo.sp_spaceused 'dbo.t_log';  
  3.   
  4.   
  5.   
  6. -- 统计多个数据库多个表的空间大小, 行数信息  
  7. create table tmp_table_space(table_name varchar(50), table_rows int, total_size varchar(20),data_size varchar(20), index_size varchar(20),  
  8. unused_size varchar(20), dbname varchar(30));  
  9. insert into tmp_table_space(table_name,table_rows,total_size,data_size,index_size,unused_size)  
  10. EXEC Your_DB.dbo.sp_spaceused 'dbo.t_log';  
  11. insert into tmp_table_space(table_name,table_rows,total_size,data_size,index_size,unused_size)  
  12. EXEC Your_DB.dbo.sp_spaceused 'dbo.t_log2';  
  13. update tmp_table_space set dbname='AHBZMJ' where dbname is null;  
  14.    
  15.   
  16.    
  17. /*************************************************************************  
  18. 用于查看对应数据库的大小、占用空间以及该数据库中各个系统表、用户表  
  19. 使用方法:  在查询分析器中选择您要查看的数据库,然后运行此代码即可。  
  20. **************************************************************************/  
  21. ----新建一个表spt_result_table存储数据库中各个表的空间信息  
  22. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[spt_result_table]')  
  23.               and OBJECTPROPERTY(id, N'IsUserTable') = 1)  
  24. drop table [dbo].[spt_result_table]  
  25. GO  
  26.    
  27. create table spt_result_table  
  28. (  
  29.     tablename       varchar(776)      null,  ----表名  
  30.     rows varchar(776)      null,  ----表中现有的行数  
  31.     reserved varchar(776)      null,  ----为表保留的空间总量  
  32.     data varchar(776)      null,  ----表中的数据所使用的空间量  
  33.     indexp varchar(776)      null,  ----表中的索引所使用的空间量  
  34.     unused varchar(776)      null   ----表中未用的空间量  
  35. )ON [PRIMARY]  
  36. GO  
  37.    
  38. ----创建存储过程prc_database_spaceused:计算数据库大小及各个表占用空间的情况  
  39. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[prc_database_spaceused]')  
  40.            and OBJECTPROPERTY(id, N'IsProcedure') = 1)  
  41. drop procedure [dbo].[prc_database_spaceused]  
  42. GO  
  43.    
  44. create procedure prc_database_spaceused  
  45. as  
  46.    
  47. BEGIN  
  48.     declare @id                 int  
  49.     declare @type         character(2)  
  50.     declare @pages         int  
  51.     declare @dbname             sysname           ----数据库名  
  52.     declare @dbsize             dec(15,0)         ----数据库大小  
  53.     declare @logsize            dec(15)  
  54.     declare @bytesperpage dec(15,0)  
  55.     declare @pagesperMB dec(15,0)  
  56.     declare @objname            varchar(776)       ----记录表名  
  57.    
  58.     declare @database_size       varchar(776)  
  59.     declare @unallocated_space   varchar(776)  
  60.     select  @dbname = db_name()                   ----数据库为当前数据库  
  61.     create table #spt_space  
  62.     (  
  63.     rows int     null,  
  64.     reserved dec(15) null,         ----保留的空间总量  
  65.     data dec(15) null,         ----数据使用的空间总量  
  66.     indexp dec(15) null,         ----索引使用的空间  
  67.     unused dec(15) null          ----未用的空间量  
  68.     )  
  69.    
  70.     ---- 计算数据大小(以kB页为单位)  
  71.     select  @dbsize = sum(convert(dec(15),size))  
  72.     from    dbo.sysfiles  
  73.     where   (status & 64 = 0)  
  74.    
  75.     ---- 计算日志大小(以kB页为单位)  
  76.     select  @logsize = sum(convert(dec(15),size))  
  77.     from    dbo.sysfiles  
  78.     where   (status & 64 <> 0)  
  79.    
  80.     ---- 求得一个page有多少bytes  
  81.     select  @bytesperpage = low  
  82.     from    master.dbo.spt_values  
  83.     where   number = 1 and type = 'E'  
  84.    
  85.     ---- 计算MB占多少page(MB = 1048576B)  
  86.     select  @pagesperMB = 1048576 / @bytesperpage  
  87.    
  88.     ---- 计算数据库大小  
  89.     set  @database_size   = ltrim(str((@dbsize + @logsize) / @pagesperMB,15,2) + ' MB')  
  90.    
  91.     ---- 计算未用的空间量的大小  
  92.     set     @unallocated_space   = ltrim(str((@dbsize -  
  93.                 (select sum(convert(dec(15),reserved))  
  94.          from sysindexes  
  95.          where indid in (0, 1, 255)  
  96.                  )) / @pagesperMB,15,2)+ ' MB')  
  97.    
  98.     ---- 保留的空间总量  
  99.     insert into #spt_space (reserved)  
  100.     select sum(convert(dec(15),reserved))  
  101.     from   sysindexes  
  102.     where  indid in (0, 1, 255)  
  103.    
  104.     select @pages = sum(convert(dec(15),dpages))  
  105.     from   sysindexes  
  106.     where  indid < 2  
  107.    
  108.     select @pages = @pages + isnull(sum(convert(dec(15),used)), 0)  
  109.     from   sysindexes  
  110.     where  indid = 255  
  111.    
  112.     ---- 数据使用的空间总量  
  113.     update #spt_space  
  114.     set    data = @pages  
  115.    
  116.     ---- 索引使用的空间  
  117.     update #spt_space  
  118.     set    indexp = (select sum(convert(dec(15),used))  
  119.                      from   sysindexes  
  120.                      where  indid in (0, 1, 255))- data  
  121.    
  122.     ---- 未用的空间量  
  123.     update #spt_space  
  124.     set    unused = reserved - (select sum(convert(dec(15),used))  
  125.             from   sysindexes  
  126.             where  indid in (0, 1, 255))  
  127.     ---- 输出数据库大小信息  
  128.     select  
  129.         database_name     = @dbname,  
  130.         database_size     = @database_size,  
  131.         unallocated_space = @unallocated_space,  
  132.         reserved          = ltrim(str(reserved * d.low / 1024.,15,0) + ' ' + 'KB'),  
  133.         data              = ltrim(str(data * d.low / 1024.,15,0) + ' ' + 'KB'),  
  134.         index_size        = ltrim(str(indexp * d.low / 1024.,15,0) + ' ' + 'KB'),  
  135.         unused            = ltrim(str(unused * d.low / 1024.,15,0) + ' ' + 'KB')  
  136.     from   #spt_space, master.dbo.spt_values d  
  137.     where  d.number = 1  
  138.     and    d.type = 'E'  
  139.    
  140.     ---- 清空临时表#spt_space  
  141.     delete from  #spt_space  
  142.    
  143.     -----定义游标,计算表大小信息  
  144.     declare  cur_table  cursor for  
  145.     select   name  
  146.     from     sysobjects  
  147.     where    xtype = 'U' or xtype = 'S'  
  148.     order    by  xtype  asc  
  149.    
  150.     -----打开游标  
  151.     open     cur_table  
  152.     fetch    next from cur_table   into  @objname  
  153.     while    (@@fetch_status = 0)  
  154.     begin  
  155.    
  156.     select @id = null  
  157.     select @id = id,  
  158.            @type = xtype  
  159.     from   sysobjects  
  160.     where  id = object_id(@objname)  
  161.    
  162.     --dbcc updateusage(0,@objname) with no_infomsgs  
  163.    
  164.     insert into #spt_space (reserved)  
  165.     select sum(reserved)  
  166.     from   sysindexes  
  167.     where  indid in (0, 1, 255)  
  168.     and    id = @id  
  169.    
  170.     select @pages = sum(dpages)  
  171.     from   sysindexes  
  172.     where  indid < 2  
  173.     and    id = @id  
  174.    
  175.     select @pages = @pages + isnull(sum(used), 0)  
  176.     from   sysindexes  
  177.     where  indid = 255  
  178.     and    id = @id  
  179.    
  180.     update #spt_space  
  181.     set    data = @pages  
  182.    
  183.     update #spt_space  
  184.     set    indexp = (select sum(used)  
  185.     from   sysindexes  
  186.     where  indid in (0, 1, 255)  
  187.     and    id = @id) - data  
  188.    
  189.     update #spt_space  
  190.     set    unused = reserved - (select sum(used)  
  191.         from   sysindexes  
  192.         where  indid in (0, 1, 255)  
  193.         and    id = @id)  
  194.    
  195.     update #spt_space  
  196.     set    rows = i.rows  
  197.     from   sysindexes i  
  198.     where  i.indid < 2  
  199.     and    i.id = @id  
  200.    
  201.     insert into spt_result_table  
  202.     select tablename = object_name(@id),  
  203.            rows = convert(char(11), rows),  
  204.            reserved = ltrim(str(reserved * d.low / 1024.,15,0) +  ' ' + 'KB'),  
  205.            data = ltrim(str(data * d.low / 1024.,15,0) +  ' ' + 'KB'),  
  206.            index_size = ltrim(str(indexp * d.low / 1024.,15,0) + ' ' + 'KB'),  
  207.            unused = ltrim(str(unused * d.low / 1024.,15,0) + ' ' + 'KB')  
  208.     from   #spt_space, master.dbo.spt_values d  
  209.     where  d.number = 1  
  210.     and    d.type = 'E'  
  211.    
  212.     truncate table  #spt_space  
  213.    
  214.     fetch   next from cur_table   into @objname  
  215.     end  
  216.     close        cur_table  
  217.     deallocate   cur_table  
  218.    
  219.     select * from spt_result_table where tablename is not null;  
  220.     drop table #spt_space  
  221. END  
  222. GO  
  223.    
  224. exec prc_database_spaceused  
  225.    
  226. ---- 删除spt_result_table表  
  227. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[spt_result_table]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)  
  228. drop table [dbo].[spt_result_table]  
  229. GO  
  230.    
  231. ---- 删除存储过程prc_database_spaceused  
  232. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[prc_database_spaceused]')  
  233.            and OBJECTPROPERTY(id, N'IsProcedure') = 1)  
  234. drop procedure [dbo].[prc_database_spaceused]  
  235. GO  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多