Quantcast
Channel: SQL Server Database Engine forum
Viewing all articles
Browse latest Browse all 12963

Modifying SQL script to allow the script executed daily via SQL agent job ?

$
0
0

People,

I'm running a small physical server with SQL 2019 Standard with just 256 GB of SSD in a far remote location. Hence extending the C: drive is not possible.
At the moment the SQL DB of software called VeeamOne is taking most of the disk space.

I have already set the recovery model to Simple, but still, disk space is running low when I issue delete command manually when the disk space is reaching 50 MB free space left.

I assume by setting the recovery mode to simple, it is better than Full which taking up the disk space by growing .LDF files

Therefore I need some help from the SQL guru here to modify the below SQL script so I can always keep the last 60 days of data by deleting unused performance metrics. So I can run it as SQL Agent job every night to maintain the disk space usage in control.

Especially in this section: SET @dt = CONVERT(DATETIME, '2019-10-01 00:00:00.001' ,120)

ALTER DATABASE VeeamOne SET RECOVERY SIMPLE
GO

CHECKPOINT
DECLARE @dt DATETIME
SET @dt = CONVERT(DATETIME, '2019-10-01 00:00:00.001' ,120)

WHILE EXISTS (SELECT * FROM [monitor].[Event] WITH(NOLOCK) WHERE [time] < @dt)
BEGIN
    BEGIN TRAN
    DELETE TOP (500) FROM [monitor].[Event] WHERE [time] < @dt
    COMMIT TRAN
    CHECKPOINT
END​

WHILE EXISTS (SELECT * FROM [monitor].[perfsamplelow] WITH(NOLOCK) WHERE [timestamp] < @dt)
BEGIN
    BEGIN TRAN
    DELETE TOP (500) FROM [monitor].[perfsamplelow] WHERE [timestamp] < @dt
    COMMIT TRAN
    CHECKPOINT
END​

DBCC SHRINKFILE (N'VeeamOne_log' , 0, TRUNCATEONLY)
GO
DBCC SHRINKFILE (N'VeeamOne' , 0, TRUNCATEONLY)
GO
Thanks in advance.


/* Server Support Specialist */



Viewing all articles
Browse latest Browse all 12963

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>