问题
There is a SQL table which is growing rapidly and inconsistently compared to it's intrinsic data. To make it short, there is a windows service backing up the content of .txt files in this table, the files weight from 1KB to 45KB approx. hence the nvarchar(max) column used to store the content of those text files.
When running the sp_spaceused command on this table, here is the result:
name rows reserved data index_size unused
Files 20402 814872 KB 813416 KB 1048 KB 408 KB
But when running this simple query, which gives me the total amount of data in bytes used by this table, the result is not anywhere near: (97231108 bytes).
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
The create statement for this table goes like this:
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
Temporary Fix: I have recreated the table (DROP & CREATE), then copied the old table's data into the new one, this made the table go from 65GB to 108MB.
My question is:
- What can make this table taking so much space and how can I prevent it from growing again?
回答1:
Installing the latest service pack fixed the problem.
来源:https://stackoverflow.com/questions/20960692/sql-table-growing-inconsistently