SQL表增长不一致

时间:2014-01-06 22:42:51

标签: sql space disk create-table

与其内在数据相比,有一个SQL表快速增长且不一致。为了简化,有一个Windows服务备份此表中的.txt文件的内容,文件的重量从1KB到45KB左右。因此,nvarchar(max)列用于存储这些文本文件的内容。

在此表上运行sp_spaceused命令时,结果如下:

name    rows    reserved       data      index_size   unused
Files   20402   814872 KB    813416 KB     1048 KB     408 KB

但是当运行这个简单的查询时,它给出了该表使用的字节数据总量,结果不在附近:(97231108字节)。

SELECT (SUM(DATALENGTH(A)) +
        SUM(DATALENGTH(B)) +
        SUM(DATALENGTH(C)) +
        SUM(DATALENGTH(D)) +
        SUM(DATALENGTH(E)) +
        SUM(DATALENGTH(F)) +
        SUM(DATALENGTH(G)) + 
        SUM(DATALENGTH(H)) +
        SUM(DATALENGTH(I))) AS BytesUsed
FROM Files

RESULT: 97231108 bytes

此表的create语句如下:

CREATE TABLE [dbo].[Files](
    [A] [int] IDENTITY(33515427,1) NOT NULL,
    [B] [nvarchar](100) NOT NULL,
    [C] [nvarchar](max) NOT NULL,  
    [D] [nvarchar](100) NOT NULL,
    [E] [datetime] NULL,
    [F] [nvarchar](2) NULL,
    [G] [datetime] NULL,
    [H] [nvarchar](100) NULL,
    [I] [int] NULL,

    CONSTRAINT [PK_Files] PRIMARY KEY CLUSTERED 
    (
    [A] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
    ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
    CONSTRAINT [UK_Files_FileType_FileDate] UNIQUE NONCLUSTERED 
    (
    [D] ASC,
    [E] ASC
            )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,                           
            ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
        ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

    GO

    ALTER TABLE [dbo].[Files]  WITH CHECK ADD  CONSTRAINT [FK_Files_FileStatus] FOREIGN   
    KEY([F])
    REFERENCES [dbo].[F] ([F])
    GO

    ALTER TABLE [dbo].[Files] CHECK CONSTRAINT [FK_Files_FileStatus]
    GO

临时修复:我重新创建了表(DROP& CREATE),然后将旧表的数据复制到新表中,这使得表从65GB变为108MB。

我的问题是:

  • 什么能让这张桌子占据这么大的空间,又怎样才能阻止它再次成长呢?

1 个答案:

答案 0 :(得分:0)

安装最新的Service Pack解决了这个问题。