Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re^3: CGI Program To Delete Old Data From MySQL Table?

by erix (Parson)
on Oct 01, 2013 at 11:19 UTC ( #1056466=note: print w/replies, xml ) Need Help??

in reply to Re^2: CGI Program To Delete Old Data From MySQL Table?
in thread CGI Program To Delete Old Data From MySQL Table?

I did think of including that optimization but I thought it was better (because conceptually easier) to show what I did show (the idea was to show the ease of use of the postgres interval datatype).

And remember that index-retrieval is not always faster. SeqScan is better than Index-retrieval when hitting a large part of the table (like when deleting 40 out of 100 rows (or keeping 60 days out of 'hundreds' of rows as the OP mentions)). Of course there is no way to know what the distribution in the OP's table is.

Indeed, for my example, it turns out seq scan is still preferred over index, with my original date data. Only if the number of deleted rows becomes small compared to the total rowcount, does Pg use the index. So yes, PostgreSQL /is/ extremely clever ;-)

The index-usable statement for postgres could be:

delete from t where d < now() - interval '2';

I tweaked my little program to accept an arg1=number of created rows and an arg2=number of rows to keep.

$ pm/ 99 60 # create table with 99 rows, keep 60 DROP TABLE SELECT 100 CREATE INDEX ANALYZE count | records_to_keep | records_to_dump -------+-----------------+----------------- 100 | 60 | 40 (1 row) QUERY PLAN -------------------------------------------------------- Delete on t (cost=0.00..2.75 rows=39 width=6) -> Seq Scan on t (cost=0.00..2.75 rows=39 width=6) Filter: (d <= (now() - '60 days'::interval)) (3 rows) DELETE 40 count | records_to_keep | records_to_dump -------+-----------------+----------------- 60 | 60 | 0 (1 row) $ pm/ 999 990 # create table with 999 rows, keep 990 DROP TABLE SELECT 1000 CREATE INDEX ANALYZE count | records_to_keep | records_to_dump -------+-----------------+----------------- 1000 | 990 | 10 (1 row) QUERY PLAN ---------------------------------------------------------------------- +----- Delete on t (cost=0.28..8.45 rows=10 width=6) -> Index Scan using d_date_idx on t (cost=0.28..8.45 rows=10 widt +h=6) Index Cond: (d <= (now() - '990 days'::interval)) (3 rows) DELETE 10 count | records_to_keep | records_to_dump -------+-----------------+----------------- 990 | 990 | 0 (1 row)

Replies are listed 'Best First'.
Re^4: CGI Program To Delete Old Data From MySQL Table?
by Jenda (Abbot) on Oct 01, 2013 at 22:29 UTC

    First, it depends on whether the index is clustered. Second, in case of a script that's apparently supposed to be run regularly and delete the old records, it's safe to assume that the number of records to delete will be small compared to the total number of record. 1/61 in case the script is run daily, 7/67 if weekly.

    Enoch was right!
    Enjoy the last years of Rome.

      We're descending a bit in database arcana (and not even the OPs database's), but what the hell:

      Small tables don't use indexes. This 60-rows is a small table. I can't get Postgres to use an index for either 1/61 or 7/67 rows. (I even tried broadening the row with more columns, but no). Index-use only starts at a few hundred rows, for my example. A quick google search makes me think Oracle behaves the same. MySQL I'm not sufficiently interested in to go lookup; it's probably the same.

      I don't see what clustered indexes have to do with the whole thing. Clustering reorganize the physical table data according to the index ordering. Obviously that can make things faster, but I rather suspect that clusteredness of an index has no bearing on plan choice.

        If the table has 60 rows, then any plan and any query will do. I was talking about one 61th of rows, not a single row in the whole of total 61 rows. If the table needs to be periodically cleared of old rows, it probably grows quicker than one row a day.

        Clustered indexes do have something to do with the thing. If you use a non-clustered index, then the index basically only contains references to the rows. Therefore for the index to be beneficial to the query execution, the number of selected rows has to be smaller compared to the total number of rows, than for a clustered index. Though with a delete it's probably a little different at least with MSSQL because deleting a whole range of rows means a fairly big change to the tree ad if the table is wide enough ...

        I just tested it on MSSQL with a 600 row table (fairly wide) and could not get it to stop using even the nonclustered index even when the condition was expected to select 98% rows. Not even with a 10000 rows table (same width, the table was created by a SELECT TOP xxx FROM a real table).

        I guess whoever ends up doing the actual job will need to test it out and find out what works best in this particular situation. In either case I would recommend writing queries so that the server may decide to use an index. No matter whether the index exists at the moment or not. Unless of course it means you have to bend backwards to write it like that.

        Enoch was right!
        Enjoy the last years of Rome.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1056466]
and the sunlight beams...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2018-04-19 16:42 GMT
Find Nodes?
    Voting Booth?