SQL Server 2005 表太大

Posted

技术标签:

【中文标题】SQL Server 2005 表太大【英文标题】:SQL Server 2005 table too big 【发布时间】:2013-12-12 16:54:57 【问题描述】:

我有这张桌子。

我不知道为什么这张桌子太大了。

这是表结构。

    CREATE TABLE [dbo].[ACI_HISCLI](
    [TER_CODI] [varchar](13) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [HIS_NUME] [int] NOT NULL,
    [HIS_FECH] [datetime] NOT NULL,
    [HIS_ESTA] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [HIS_HORA] [datetime] NOT NULL,
    [MED_CODI] [decimal](13, 0) NOT NULL,
    [HIS_HOEN] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [HIS_HODC] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [HIS_MOTI] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_AODS] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_AODC] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_AOSS] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_AOSC] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_ODQ1] [decimal](4, 2) NULL,
    [HIS_ODQ2] [int] NULL,
    [HIS_ODQ3] [decimal](4, 2) NULL,
    [HIS_ODQ4] [int] NULL,
    [HIS_OSQ1] [decimal](4, 2) NULL,
    [HIS_OSQ2] [int] NULL,
    [HIS_OSQ3] [decimal](4, 2) NULL,
    [HIS_OSQ4] [int] NULL,
    [HIS_ODR1] [decimal](4, 2) NULL,
    [HIS_ODR2] [decimal](4, 2) NULL,
    [HIS_ODR3] [int] NULL,
    [HIS_OSR1] [decimal](4, 2) NULL,
    [HIS_OSR2] [decimal](4, 2) NULL,
    [HIS_OSR3] [int] NULL,
    [HIS_ODS1] [decimal](4, 2) NULL,
    [HIS_ODS2] [decimal](4, 2) NULL,
    [HIS_ODS3] [int] NULL,
    [HIS_ODS4] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_OSS1] [decimal](4, 2) NULL,
    [HIS_OSS2] [decimal](4, 2) NULL,
    [HIS_OSS3] [int] NULL,
    [HIS_OSS4] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_ADI1] [decimal](3, 2) NULL,
    [HIS_ADI2] [varchar](2) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_ADI3] [int] NULL,
    [HIS_TON1] [datetime] NULL,
    [HIS_TOOD] [int] NULL,
    [HIS_TOOS] [int] NULL,
    [HIS_MOVI] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_BIOM] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_FOJO] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_OTRO] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [DIA_COD1] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [DIA_COD2] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [DIA_COD3] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_COND] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_OBSE] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_IMA1] [image] NULL,
    [HIS_IMN1] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_IMA2] [image] NULL,
    [HIS_IMN2] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_CONS] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_ORIL] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_ORIT] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_ORIP] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_INGS] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_INGA] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_INGC] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_INGE] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_INGL] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_TAIN] [varchar](7) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_PUIN] [int] NULL,
    [HIS_TEIN] [decimal](6, 2) NULL,
    [HIS_TAEG] [varchar](7) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_PUEG] [int] NULL,
    [HIS_TEEG] [decimal](6, 2) NULL,
    [HIS_OBHE] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [USU_CREA] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [HIS_ASPR] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [PRO_CONS] [int] NULL,
    [MED_CODC] [decimal](13, 0) NULL,
    [ANE_CODI] [decimal](13, 0) NULL,
    [AYU_CODI] [decimal](13, 0) NULL,
    [INS_CODI] [decimal](13, 0) NULL,
    [HIS_DIPR] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_DIPO] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_DECI] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_GODV] [int] NULL,
    [HIS_GODT] [varchar](200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_GOIV] [int] NULL,
    [HIS_GOIT] [varchar](200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_BIFO] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_BIOT] [varchar](51) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_OBSD] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_OBSS] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_ODLA] [image] NULL,
    [HIS_ODFR] [image] NULL,
    [HIS_OILA] [image] NULL,
    [HIS_OIFR] [image] NULL,
    [HIS_ODSE] [image] NULL,
    [HIS_OISE] [image] NULL,
    [HIS_GOD1] [decimal](3, 2) NULL,
    [HIS_GOD2] [decimal](3, 2) NULL,
    [HIS_GOD3] [decimal](3, 2) NULL,
    [HIS_GOD4] [decimal](3, 2) NULL,
    [HIS_GOD5] [decimal](3, 2) NULL,
    [HIS_GOD6] [decimal](3, 2) NULL,
    [HIS_GOD7] [decimal](3, 2) NULL,
    [HIS_GOD8] [decimal](3, 2) NULL,
    [HIS_GOI1] [decimal](3, 2) NULL,
    [HIS_GOI2] [decimal](3, 2) NULL,
    [HIS_GOI3] [decimal](3, 2) NULL,
    [HIS_GOI4] [decimal](3, 2) NULL,
    [HIS_GOI5] [decimal](3, 2) NULL,
    [HIS_GOI6] [decimal](3, 2) NULL,
    [HIS_GOI7] [decimal](3, 2) NULL,
    [HIS_GOI8] [decimal](3, 2) NULL,
    [HIS_CONT] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_RESU] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_GLIC] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_HEMA] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_PAQU] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_CREA] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_ELEC] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_BIO1] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_BIO2] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_BIO3] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_BIO4] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_BIO5] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_OBEX] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_OBST] [varchar](150) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_DIT1] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_DIT2] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_DIT3] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_TANE] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [ENT_CODI] [numeric](13, 0) NULL,
    [HIS_DIDR] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_DIDO] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_DIPI] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_DISA] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_DIO1] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_DIO2] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_DIO3] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [HIS_HOAN] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [TER_NOAC] [varchar](120) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [TER_TACO] [varchar](25) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [TER_PACO] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [TER_DACO] [varchar](70) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [TER_IACO] [varchar](15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [TER_NORE] [varchar](120) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [TER_TRES] [varchar](25) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [TER_PRES] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [TER_DRES] [varchar](70) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [TER_IRES] [varchar](15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [USU_AUDI] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [FEC_AUDI] [datetime] NOT NULL,
    [TIP_AUDI] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [MOD_ORIG] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

使用命令sp_spaceued ACI_HISCLI

这是结果:

name: ACI_HISCLI
rows: 270719
reserved:   64521288 KB
data:       64377992 KB
index_size:      152 Kb
unused:       143144 KB

图像类型的字段,几乎为空。 该表为用户提供了如此多的交易,但只有文本蚂蚁,大小为 60 GB! 我不知道这张桌子为什么这么大! 如何优化此表或缩小此表。 我试试:

DBCC CLEANTABLE

并重建索引 谢谢。

【问题讨论】:

SELECT index_id,type_desc,total_pages,used_pages,data_pages FROM sys.partitions p INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id WHERE p.object_id = object_id('[dbo].[ACI_HISCLI]')的输出是什么 您能否提供任何 PK 和/或索引定义,或者这是一个堆?您使用的是什么版本的 SQL Server? 【参考方案1】:

所有这些 TEXT 和 IMAGE 字段都占用 16 个字节,因为它们是指向数据实际所在位置的指针(当那里有数据时)。但我几乎可以肯定,就像固定宽度数据类型(INT、BIGINT、DATETIME 等)一样,即使为 NULL,它们也会占用该空间。

此外,该表确实有太多难以管理的字段。您应该考虑将其拆分为两个(或多个)具有相同 PK、FKed 并具有 1 对 1 关系的表。经常不使用的字段应该移到辅助表中。

编辑: 使用的数据空间也受聚集索引上的 FILLFACTOR 设置的影响。将其设置得太低,尤其是对于如此大的行大小,将导致很少的行适合 8k 数据页。

此外,有时删除行时不会释放空间。对 Clustered Index 执行索引 REBUILD 会释放该空间(如果大小下降很多,您将知道执行 REBUILD 后是否有未释放的空间)。

EDIT2: 任何使用 SQL Server 2005 和更新版本的人都应该考虑将使用旧 blob 类型(现已弃用)的字段转换为较新的等效项:

文本 -> VARCHAR(MAX) NTEXT -> NVARCHAR(MAX) 图像 -> VARBINARY(MAX)

【讨论】:

即使 270,719 行中的每一行都使用完整的 8KB 页面,但“仅”为 2 GB。他们必须使用大量的行溢出或 LOB 空间。 @Martin,我并不是说它们是整个空间,只是当它们为 NULL 时不能打折。很可能大量使用了文本字段,但我认为 OP 对其中的数据有所了解。 已添加 +1 我可以拆分该表,将所有可为空的列放在另一个表中,并且(这里可能很棒)将所有 blob(图像)移出数据库。 @jean,您使用的是什么版本的 SQL Server?根据使用的版本,还有其他几件事情需要考虑。 Jean 不是 OP。标题写的是 2005 年。

以上是关于SQL Server 2005 表太大的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server - 插入表太慢

如何推断 SQL Server 中的日期以计算每日计数?

已安装 SQL Server 2005,安装 SQL Server 2008 时提示需要删除 SQL Server 2005 Express 工具

哪里能下载到SQL server 2005?

SQL Server 2005 异常

已安装 SQL Server 2005,安装 SQL Server 2008 时提示需要删除 SQL Server 2005 Express 工具