in reply to CGI Program To Delete Old Data From MySQL Table?
UPDATE: Oops. You *did* mention the DBMS, in the title no less. I didn't see that. So this is a little off topic -- sorry about that.
In PostgreSQL:
delete from table where now() - column > interval '60 days';I wrote a little standalone bash to show that off. It creates 100 days worth of rows, shows some counts, DELETEs with an appropriate interval expression, then counts again:
(careful: it drops table t)
#!/bin/sh echo " drop table if exists t; -- dropping a table, be careful! create table t as select d from generate_series( current_timestamp - interval '99 days' , current_timestamp , interval '1 day' ) as f(d) ; -- UPDATE: add this for index tests: -- create index t_d_idx on t (d); select count(*) , sum( case when now() - d > interval '60 days' then 0 else 1 end ) as records_to_keep , sum( case when now() - d > interval '60 days' then 1 else 0 end ) as records_to_dump from t; delete from t where now() - d > interval '60 days' ; select count(*) , sum( case when now() - d > interval '60 days' then 0 else 1 end ) as records_to_keep , sum( case when now() - d > interval '60 days' then 1 else 0 end ) as records_to_dump from t; " | psql -X
Output:
DROP TABLE SELECT 100 count | records_to_keep | records_to_dump -------+-----------------+----------------- 100 | 60 | 40 (1 row) DELETE 41 count | records_to_keep | records_to_dump -------+-----------------+----------------- 60 | 60 | 0 (1 row)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: CGI Program To Delete Old Data From MySQL Table?
by Jenda (Abbot) on Oct 01, 2013 at 09:16 UTC | |
by erix (Prior) on Oct 01, 2013 at 11:19 UTC | |
by Jenda (Abbot) on Oct 01, 2013 at 22:29 UTC | |
by erix (Prior) on Oct 02, 2013 at 08:04 UTC | |
by Jenda (Abbot) on Oct 02, 2013 at 09:03 UTC |