我们在删除数据的 while 循环中使用“sp_spaceused”。
我们这样做是为了保持在 10GB SQL Express Edition 限制之下。
每晚运行的删除例程
PROCEDURE [dbo].[DeletePeriod4Data]
@LIMIT_InMB AS FLOAT
AS
BEGIN
SET NOCOUNT ON
DECLARE @MinBegin AS DATETIME
DECLARE @SizePeriod4_InMB AS FLOAT
EXEC [dbo].[GetDatabaseStatsPeriod4MB]
@Size_InMB = @SizePeriod4_InMB OUTPUT
WHILE @SizePeriod4_InMB > @LIMIT_InMB
BEGIN
SELECT @MinBegin = MIN(MONTH.ProdData.[Begin])
FROM MONTH.ProdData
PRINT 'deleting from period 4, month and year of: '
PRINT @MinBegin
DELETE
FROM Month.ProdData
WHERE
Year = DATEPART(YEAR, @MinBegin)
AND
Month = DATEPART(MONTH, @MinBegin)
EXEC [GetDatabaseStatsPeriod4MB]
@Size_InMB = @SizePeriod4_InMB OUTPUT
END
END
这是返回过时数字的罪魁祸首存储过程。
PROCEDURE [dbo].[GetDatabaseStatsPeriod4MB]
@Size_InMB float OUTPUT
AS
BEGIN
CREATE TABLE #t (name SYSNAME, rows CHAR(11), reserved VARCHAR(18),
data VARCHAR(18), index_size VARCHAR(18), unused VARCHAR(18))
DBCC UPDATEUSAGE(0); -- <-- helps?
EXEC sp_msforeachtable @command1=
'INSERT INTO #t EXEC sp_spaceused ''?'', @updateUsage=''TRUE''',
@whereand=' and schema_name(schema_id) = ''Month'' '
SELECT @Size_InMB =
SUM(CONVERT(INT, SUBSTRING(data, 1, LEN(data)-3)))/1024.0
FROM #t
DROP TABLE #t
PRINT 'PERIODE 4 Size (Month)'
PRINT @Size_InMB
END
我们尝试的是
- 将参数“@updateUsage=TRUE”添加到 sp_spaceused
- 添加了对“DBCC UPDATEUSAGE(0);”的调用
- 将参数添加到“DBCC UPDATEUSAGE(0) WITH NO_INFOMSGS, COUNT_ROWS;”
- 在我调用“UpdateUsage”之前使用延迟<br> WAITFOR DELAY '00:00:05' ---- 5 秒延迟
重现问题的通用代码
CREATE TABLE #t (name SYSNAME, rows CHAR(11), reserved VARCHAR(18),
data VARCHAR(18), index_size VARCHAR(18), unused VARCHAR(18))
EXEC sp_msforeachtable @command1=
'INSERT INTO #t EXEC sp_spaceused ''?'', @updateUsage=''TRUE'''
SELECT SUM(CONVERT(INT, SUBSTRING(data, 1, LEN(data)-3)))/1024.0
FROM #t
DROP TABLE #t
-- Delete something in your DB (Northwind)
-- Run the above again