Barry Andrew
twinsen.be wrote: |
|
Hi everyone,
I'm writing a small script to clean up our database. We have a couple of databases which contain many gigabytes of data.
The script fills a few temp tables, with price-id's which can be deleted (based on a few rules). Then it deletes all related data first, before actually deleting the price records themselves.
This works fine, except for performance issues. After 12 hours, I had to cancel the running script as it was taking too many resources.
My question is, how can I increase performance Should I add 'commit' after deleting data in each table Would it help to make it a stored procedure
Script looks kinda like this:
SELECT priceId INTO #tobedeleted FROM prices WHERE ... (few rules)
DELETE FROM price_product WHERE priceId IN ... #tobedeleted
....
.... | |
Hi twinsen!
You will most definately get a massive performance increase if you re-write this as a stored procedure. You will be getting pre-compiled execution to name but one of the advantages, some of which are highlighted
here; First create your delete;
CREATE PROCEDURE stp_MyDelete
AS
DELETE FROM price_product WHERE priceId IN ... #tobedeleted;
GO
Then create your cleanup one, which will call the delete...
CRAETE PROCEDURE stp_MyCleanup
AS
SELECT
PriceId INTO #tobedeleted FROM prices WHERE ... (few rules);
GO
EXEC stp_MyDelete; GO
Now all you have to do is either call "EXEC stp_MyCleanup" or why not create this as a job and set up some alerts so you can monitor it
There is a lot you can do with sp's and they are actually a lot of fun once you get used to them.
hth
Barry