zoukankan      html  css  js  c++  java
  • SQL计算数据库表占用的真实空间大小

       最近客户的数据库暴涨,一开始找不到原因,用sql查询表的空间大小,结果之前用的sql语句只能查询除了text,image外的字段大小.搞得一时间混乱不堪.后来咨询了大师们,找到用sp_spaceused这个系统函数可以查询表的真实大小,包含text等字段.sql如下:

    declare @id            NVARCHAR(100)
    create table #spt_space
    (
        [name]            NVARCHAR(50) null,
        [rows]          int null,
        [reserved]       NVARCHAR(50) null,
        [data]         NVARCHAR(50) null,
        [index_size]        NVARCHAR(50) null,
        [unused]         NVARCHAR(50) null
    )
    
    set nocount on
    
    declare c_tables cursor for
    select name from sysobjects where xtype = 'U'
    
    open c_tables fetch next from c_tables into @id
    
    while @@fetch_status = 0
    begin
        /* Code from sp_spaceused */
        insert into #spt_space ([name],[rows],reserved,data,index_size,unused)
        EXEC sp_spaceused @id
        fetch next from c_tables   into @id
    
    end
    SELECT * FROM(
    SELECT *,CAST(REPLACE(reserved,'KB','') AS INT) AS reservedindex FROM #spt_space
    ) c
    order by c.reservedindex desc
    
    drop table #spt_space
    close c_tables
    deallocate c_tables
    
    版权说明

      如果标题未标有<转载、转>等字则属于作者原创,欢迎转载,其版权归作者和博客园共有。
      作      者:温景良
      文章出处:http://wenjl520.cnblogs.com/  或  http://www.cnblogs.com/

  • 相关阅读:
    mysql 防止插入某个字段重复的值
    vue 脚手架的目录结构
    搭建Vue 脚手架项目
    flex 布局的页面
    Java List 排序问题
    maven 管理oracle jar
    JPA 注解
    PL/SQL Developer 不显示系统表,默认显示My objects
    jquery面试题
    web前端课程检测2
  • 原文地址:https://www.cnblogs.com/wenjl520/p/2095320.html
Copyright © 2011-2022 走看看