我们有 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 吗?(基于其他讨论,不推荐)
