I have several tables in a database. One table (tbl_transactions) has thousands of orphaned records that are not linked to any of the remaining tables. I need to run a script that will delete these records in order to regain some lost space in my database. I tried to run a script that deleted all the records, but the log file consumed 20 GB of space, thus filling the HDD and the script did not complete. My script looks like this:
delete tbl_Transactions
where not exists (select *
From tbl_SocketConnections
where tbl_Transactions.TransactionID = tbl_SocketConnections.TransactionID)
And Not Exists(Select *
From tbl_ProtocolCommands
where tbl_Transactions.TransactionID = tbl_ProtocolCommands.TransactionID)
And Not Exists(Select *
From tbl_EventRules
where tbl_Transactions.TransactionID = tbl_EventRules.TransactionID)
There are several other tables, but the pattern repeats. Can someone advise on how I can limit the scope of this script to say 1000 records at a time?