我在数据库里面简历一张表
create table test_table(A1 int, A2 char(3), A3 char(3))
然后往这张表添加了250万行记录后,
用 EXEC   sp_spaceused   'test_table'
看到该表占用了60MB左右的硬盘空间。不解。按照该表结构,每行占用10个字节,250万行也就是2500000*10/1024/1024=24MB左右啊...哪位仁兄帮忙解释一下? 

解决方案 »

  1.   

    确认你这张表是新建立的?
    把你的sp_spaceused结果也发出来看下。
      

  2.   

    create table test_table(A1 int, A2 char(3), A3 char(3))这个表没有变长列,空间还是比较好算的:每个数据行的空间:状态位2字节+列计数4字节+空位图ceiling(3列/8)=1字节+字长数据10字节+行偏移矩阵2字节 = 19字节
    每个数据页可容纳的行数:floor(8096/19) = 426行
    总的数据页数:ceiling(2500000/426) = 5869页
    总的表空间:8192 * 5869 = 48078848字节 = 45.851MB确定没有聚集索引吗?有点奇怪。
      

  3.   


    feilniu正解,学习了。。还烦请问下面表结构,每行需占多少空间?
    CREATE TABLE [dbo].[Car_Gps_Info](
    [CarID] [bigint] NULL,
    [GpsDate] [int] NULL,
    [Cmd] [varchar](3) COLLATE Chinese_PRC_CI_AS NULL,
    [GpsTime] [datetime] NULL,
    [Long] [int] NULL,
    [Lat] [int] NULL,
    [Speed] [tinyint] NULL,
    [Dire] [smallint] NULL,
    [Height] [smallint] NULL,
    [Mile] [bigint] NULL,
    [Oil] [smallint] NULL,
    [Alarm] [tinyint] NULL,
    [State1] [tinyint] NULL,
    [State2] [tinyint] NULL,
    [State3] [tinyint] NULL,
    [StateAlarm] [int] NULL,
    [Zcode] [int] NULL,
    [Reserve] [varchar](100) COLLATE Chinese_PRC_CI_AS NULL
    )
    --其中Reserve字段基本都是空值,其他字段全部有值
    谢了。
      

  4.   

    create table test_table(A1 int, A2 char(3), A3 char(3))Godeclare @i int
    set @i=1
    while @i <= 2500
    begin
      insert into test_table select @i, 'L', 'CMD'
      set @i = @i + 1
    endset @i = 1
    while @i <= 10
    begin
      insert into test_table select * from test_table
      set @i = @i + 1
    endGoEXEC sp_spaceused 'test_table'
    结果:
      name           rows           reserved          data       index_size unused
    test_table 2559981     50632 KB 50568 KB 8 KB 56 KB
      

  5.   

    EXEC sp_spaceused 'test_table'
    结果:
    name rows reserved data index_size unused
    test_table 2559981 50632 KB 50568 KB 8 KB 56 KB总的数据页数:ceiling(2559981/426) = 6010页
    总的表空间:8192 * 6010 = 49233920字节 = 48080 KB这样的话就很接近了。但还有一点误差不知道什么原因。
      

  6.   

    我的结果:是48080create table test_table(A1 int, A2 char(3), A3 char(3))Godeclare @i int
    set @i=1
    while @i <= 2500
    begin
      insert into test_table select @i, 'L', 'CMD'
      set @i = @i + 1
    endset @i = 1
    while @i <= 10
    begin
      insert into test_table select * from test_table
      set @i = @i + 1
    endGosp_spaceused test_tablename rows reserved data index_size unused
    ------------ ----------- ----------- ---------- ------------     --------
    test_table 2560000   48144 KB 48080 KB 16 KB 48 KBselect OBJECT_ID('test_table')
    select DB_ID()select  avg_fragment_size_in_pages,avg_fragmentation_in_percent,avg_page_space_used_in_percent,avg_record_size_in_bytes,page_count,record_count
    from sys.dm_db_index_physical_stats(10,293576084,null,null,'DETAILED')/*
    page_count结果为6010
    */
      

  7.   

    估算表的空间,可参看:联机丛书 -> 估计数据库的大小。具体原理参看《SQLServer2005技术内幕:存储引擎》第6章第7章。