Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Stored procedure taking too long to run Expand / Collapse
Author
Message
Posted Monday, December 30, 2013 10:18 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Today @ 8:21 AM
Points: 547, Visits: 1,877
petr.caslavka (12/30/2013)
This is definitely a better way of doing it. However, I am not sure about the TRUNCATE TABLE ps_micros_e7_time_clock_details statement. I don't want/need to truncate the table altogether as there are stores that don't have any duplicates.

thanks,

Petr


Yeah, I don't really get this statement. You were basically deleting it all, but slower, in the original query. Also, you are overwriting values of poll_date and batch_id, so I am that even for the non-duplicates these could have erroneous values you want to overwrite.

Truncate and reload is likely to be the quickest approach if you can't follow Craig's advice on adding a unique identifier.

If you don't need to update the singleton rows at all, and expect a very low # of the total records to be duplicates, you could try replacing the distinct from the select into a temp table with a group by all the fields, with a HAVING count(1) > 1 clause. Then delete from the main table with a join to the temp table. Then insert back.

But my guess is Luis' answer will perform better.


Post #1526663
Posted Tuesday, December 31, 2013 3:29 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:41 AM
Points: 6,748, Visits: 12,843
The good ol' ROW_NUMBER() trick might be worth a try. A few tests on a similar-sized table here indicates relative costs of 20% for the table scan, 30% for the sort and 50% for the table delete. That's about an order of magnitude nicer than I was expecting to see.

Also, see point 10 of this Brad McGehee article. Deleting from a heap is bad news.


BEGIN TRANSACTION 

-- Use a CTE to identify dupe rows for this @StoreOID
;WITH Deleter AS (
SELECT rn = ROW_NUMBER() OVER(PARTITION BY
store_oid, Seq, EmplSeq, JobSeq, OvertimeRuleSeq,
ReasonDefSeq, ClockInStatus, ClockInTimeUtc, ClockOutStatus, ClockOutTimeUtc,
InAdjustEmplSeq, OutAdjustEmplSeq, RegularSeconds, Overtime1Seconds,
Overtime2Seconds, Overtime3Seconds, Overtime4Seconds, AccumulatedDailySeconds,
AccumulatedPeriodSeconds, RegularPay, Overtime1Pay, Overtime2Pay,
Overtime3Pay, Overtime4Pay, RegularRate, AccumulatedDays, ConsecutiveDays,
Computed, BreakSeconds, PaidBreakSeconds, PaidBreakPay, ReportingTimeSeconds, ReportingTimePay, AdjustedClockInTime, AdjustedClockOutTime
ORDER BY (SELECT NULL)),
store_oid, Seq, EmplSeq, JobSeq, OvertimeRuleSeq,
ReasonDefSeq, ClockInStatus, ClockInTimeUtc, ClockOutStatus, ClockOutTimeUtc,
InAdjustEmplSeq, OutAdjustEmplSeq, RegularSeconds, Overtime1Seconds,
Overtime2Seconds, Overtime3Seconds, Overtime4Seconds, AccumulatedDailySeconds,
AccumulatedPeriodSeconds, RegularPay, Overtime1Pay, Overtime2Pay,
Overtime3Pay, Overtime4Pay, RegularRate, AccumulatedDays, ConsecutiveDays,
Computed, BreakSeconds, PaidBreakSeconds, PaidBreakPay, ReportingTimeSeconds, ReportingTimePay, AdjustedClockInTime, AdjustedClockOutTime

FROM ps_micros_e7_time_clock_details
WHERE store_oid = @StoreOID
)
-- and delete them
DELETE FROM Deleter WHERE rn > 1
IF @@ERROR <> 0 GOTO ERR_HANDLER

COMMIT TRANSACTION



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

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
Exploring Recursive CTEs by Example Dwain Camps
Post #1526707
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse