Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting records having a date older than 3 days, for rolling 3 day job?

Prior to executing a sensitive sql command I wanted to do a sanity check.

I am trying to delete records that have a [LoadDt] date value older than 3 days and my code is:

delete IntraDayStats
where DATEDIFF(dd, LoadDt, dateadd(d,-3, getdate()) ) >= 3 

I want to schedule this as a sql job so that my IntraDayStats table has a rolling 3 day history. The job will run nightly.

like image 215
kacalapy Avatar asked Jan 03 '11 14:01

kacalapy


1 Answers

where DATEDIFF(dd, LoadDt, dateadd(d,-3, getdate()) ) >= 3 

is not sargable (an index won't be used), use

where LoadDt < getdate()- 3 

Next time if you want to check, make the DELETE a SELECT and see what you get back

like image 128
SQLMenace Avatar answered Sep 26 '22 23:09

SQLMenace