Question

I'm having some trouble with some ancient database i have to maintain. It's been around for ages (No, i did not design it, no, it's not possible to make any changes on it)

it goes like this: There are 2 tables, "Documents" and "Versions". Documents is a pretty simple table, just a primary key, a varchar storing the document name and the user who created that document.Versions has a foreign key to the document it belongs to, an image field where the actual document is stored (mainly word documents and pdfs), the extension, and another field that keeps the version number.

Whenever the application (an ANCIENT VB6 application) consumes a document, a new version is generated.

Every night, a job is run on the database, in order to delete all versions except the 5 latest ones of each document. This has been working like, forever.

DELETE FROM t_ad_Versions WHERE VersionNumber < dbo.MaxVersion(codDocument)-4

Problem is, eventhough everyday the versions table has this job running in order to discar the oldest entries, the database is reaching an alarming size (Currently 300+ GB).

In order to reduce said size, someone eventually realized that there's no need to keep versioning pdf elements. So, i was commanded to apply a simple modification:

the job will delete older versions of word docs, preserving the latest 5 versions. For any other kind of file, ALL versions except the latest one will be deleted. So, i changed the stored procedure the job is associated to to this:

DELETE FROM t_ad_Versions WHERE VersionNumber < dbo.MaxVersion(codDocument)-4 
AND extension  LIKE 'do%';

DELETE FROM t_ad_Versions WHERE VersionNumber < dbo.MaxVersion(codDocument) 
AND extension  NOT LIKE 'do%';

However, after applying the change, the following day the job history notified the following error

"The instance of the SQL Server Database Engine cannot obtain a LOCK resource at this time. Rerun your statement when there are fewer active users. Ask the database administrator to check the lock and memory configuration for this instance, or to check for long-running transactions. [SQLSTATE HY000] (Error 1204). The step failed."

I'm kinda lost now, any ideas?

Was it helpful?

Solution

You can try to delete row by 100 or any number that works fast.

declare @rows int
set @rows = 1
while @rows > 0
BEGIN
DELETE TOP (100)  FROM t_ad_Versions WHERE VersionNumber < dbo.MaxVersion(codDocument)-4 AND extension  LIKE 'do%';
set @rows = @@ROWCOUNT
END
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top