在 oracle SQL 中删除超过 24 小时的记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29097124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
delete records older than 24 hours in oracle SQL
提问by Andrew
I want to delete all the records which is older than 24 hours. I am using the below query for the same but sometimes doesn't run perfectly. I want to know whether my query is right or wrong ? Or which is better way to do so.
我想删除所有超过 24 小时的记录。我正在使用以下查询,但有时无法完美运行。我想知道我的查询是对还是错?或者哪种方法更好。
delete from TEMP_SERVICE_OPTION where EVENT_DATE < TRUNC(SYSDATE) - 1;
回答by Gordon Linoff
If you want older than 24 hoursthen do:
如果您想要超过24 小时,请执行以下操作:
where event_date < sysdate - 1
If you want before yesterday, then do:
如果你想在昨天之前,那么做:
where event_date < trunc(sysdate) - 1
As for performance, that depends on how many rows are being deleted. If your table only has thousands of rows, then this is fine. If it has millions of rows, then you might want an index on event_date
. Or, you might even want to take a different approach -- selecting the data into a temporary table, truncating the original table, and then re-inserting it.
至于性能,这取决于删除了多少行。如果您的表只有数千行,那么这很好。如果它有数百万行,那么您可能需要在event_date
. 或者,您甚至可能想要采用不同的方法——将数据选择到临时表中,截断原始表,然后重新插入。
回答by paul
Your query is deleting records where the EVENT_DATE
is before yesterday.
您的查询正在删除EVENT_DATE
昨天之前的记录。
If you want to delete records that are over 24 hours old, try:
如果要删除超过 24 小时的记录,请尝试:
delete from TEMP_SERVICE_OPTION where (SYSDATE - EVENT_DATE) > 1;