June 24, 2013 at 12:41 am
Hi ,
Environment: SQL Server 2008 Enterprise Edition with sufficient hardware.
I have a table with > 60 Million records on which I am performing delete operation using a stored proc. I am deleting the records by batch using TOP clause by joining with a #temp table (with no indexes). I have 1 Cluster Index and 3 non-cluseter index on this main table. Issue is the block itself is taking 95% of the stored proc's total execution. Below is the query that I am using to delete records.
WHILE 1 = 1
BEGIN
DELETE TOP(10000) t
FROM EXPORT.SE_VARCS_DBF t -- Main table with > 60 Million Recs
INNER JOIN #PatchIdentifiers p -- Temp Table
ONt.M_SCNTYPE = p.M_SCNTYPE AND
t.M_FAMILY0 = p.M_FAMILY0 AND
t.M_MLABEL0 = p.M_MLABEL0 AND
t.M_SLABEL0 = p.M_SLABEL0
WHERE t.ContainerID = @TargetContainerID
IF @@ROWCOUNT = 0
BREAK
END
Could you please help me out with the advice.
Thanks,
Arjun.
June 24, 2013 at 1:47 am
ArjunaReddy (6/24/2013)
Hi ,Environment: SQL Server 2008 Enterprise Edition with sufficient hardware.
I have a table with > 60 Million records on which I am performing delete operation using a stored proc. I am deleting the records by batch using TOP clause by joining with a #temp table (with no indexes). I have 1 Cluster Index and 3 non-cluseter index on this main table. Issue is the block itself is taking 95% of the stored proc's total execution. Below is the query that I am using to delete records.
WHILE 1 = 1
BEGIN
DELETE TOP(10000) t
FROM EXPORT.SE_VARCS_DBF t -- Main table with > 60 Million Recs
INNER JOIN #PatchIdentifiers p -- Temp Table
ONt.M_SCNTYPE = p.M_SCNTYPE AND
t.M_FAMILY0 = p.M_FAMILY0 AND
t.M_MLABEL0 = p.M_MLABEL0 AND
t.M_SLABEL0 = p.M_SLABEL0
WHERE t.ContainerID = @TargetContainerID
IF @@ROWCOUNT = 0
BREAK
END
Could you please help me out with the advice.
Thanks,
Arjun.
Performance issues generally require a lot more information. Have a read through this --> http://www.sqlservercentral.com/articles/SQLServerCentral/66909/%5B/url%5D.
June 24, 2013 at 1:55 am
Can you just put the PKs of the main table to be deleted in the temp table, then index it, then join on that.
Also, you need to surround your deletes with a begin/commit tran:
Begin tran
delete ...
commit tran
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
June 24, 2013 at 3:53 am
Please find the execution plan for the delete statement. Looking at the plan INDEX delete operation is taking most of the time, Please let me know if there is any way to reduce.
June 24, 2013 at 4:32 am
Can you post the actual plan (not the estimated plan) for this batch, please?
-- Determine what is in the patch container
SELECT TOP(1000) DISTINCT M_SCNTYPE, M_FAMILY0, M_MLABEL0, M_SLABEL0, ContainerID = @TargetContainerID
INTO #PatchIdentifiers
FROM Export.SE_VARCS_DBF
WHERE ContainerID = @PatchContainerID
-- Remove patch identifiers from target in batches
DELETE t
FROM Export.SE_VARCS_DBF t
INNER JOIN #PatchIdentifiers p
ONt.M_SCNTYPE = p.M_SCNTYPE AND
t.M_FAMILY0 = p.M_FAMILY0 AND
t.M_MLABEL0 = p.M_MLABEL0 AND
t.M_SLABEL0 = p.M_SLABEL0 AND
t.ContainerID = p.TargetContainerID
Cheers
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
June 24, 2013 at 4:46 am
I insist to ignore top clause as there are around 60 million records. when u perform top it default sorts data. I insist use between clause that on your identity column that helps may more.
June 24, 2013 at 4:50 am
Aadhar Joshi (6/24/2013)
I insist to ignore top clause as there are around 60 million records. when u perform top it default sorts data.
No it doesn't. A TOP just limits the resultset to that number, to get a sort, you need something like an ORDER BY, which isn't valid on a delete
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply