0

我们有 Microsoft SQL Server 2012 数据库,用于存储来自排队系统的统计数据。排队系统每 10 分钟向数据库发送一次统计信息,数据库大小每周增长约 1 GB。

我们不需要超过 1 年的数据。

我们创建了一个 SQL 脚本来删除旧数据。脚本执行后,DB 大小变大。

use statdb

-- Execute as stat user
-- The following three settings are used to control what will be used

-- Amount of days of stat data which should be kept
DECLARE @numberOfDaysToKeep int=365

-- If set to 1, also the aggregated data will be removed, otherwise only the events will be removed
DECLARE @DeleteAggregateData int = 1

-- If set to 1, also the hardware monitoring data will be removed.
DECLARE @DeleteAgentDcData int = 1

-- Do not change anything below this line
DECLARE @DeleteBeforeDate int = (SELECT id FROM stat.dim_date WHERE full_date > DATEADD(day,-1-@numberOfDaysToKeep,GETDATE()) AND full_date < DATEADD(day,0-@numberOfDaysToKeep,GETDATE()))

-- Remove CFM events
DELETE FROM stat.fact_visit_events where date_key < @DeleteBeforeDate
DELETE FROM stat.fact_sp_events where date_key < @DeleteBeforeDate
DELETE FROM stat.fact_staff_events where date_key < @DeleteBeforeDate

-- ...continue to delete from other tables

我们希望将数据库大小保持在恒定大小。MS SQL 服务器是否使用可用空间(删除后)或数据库大小是否会以与删除前相同的速度增长?

或者我需要在脚本之后运行 SHRINK 吗?(基于其他讨论,不推荐)

4

1 回答 1

0

您没有提供信息是您的数据库处于 FULL 恢复模式还是 SIMPLE 恢复模式。当然,您必须缩小日志文件和数据文件。在 SSMS 中,您可以看到有多少可用空间可用于数据文件和日志文件。见下图。

在此处输入图像描述

有一个选项可以使用 T-SQL 缩小数据文件和日志文件。查看更多关于DBCC SHRINKFILE (Transact-SQL)

于 2019-04-24T19:16:04.803 回答