How to efficiently delete expired rows from a large MySQL table

AYR

I have an extremely large table from which I would like to delete old rows. Example of table:

 | customer_id | first_purchase_date | last_purchase_date |
 |<primary key>|                     |   <index>          |

** I am using this example table for argument's sake. The table in question is not a customer table. The real table has grown to 28 GB in size over the past 2 months and is used to calculate something that requires only 2 weeks of historical data.

What I would like to do is delete customers from this table that have not purchased anything in the past year. I.e. delete from table where last_purchase_date < now() - interval 1 year;

Simpy deleting like this would be too costly on the database. I know that a partition can be used to truncate the old rows but I am not sure how to implement it effectively.

In addition, if a customer were to purchase something then that row could potentially move to a different partition by updating the last_purchase_date. Would this not be expensive as well?

Thank you in advance for any direction!

e4c5

You are right in thinking that partitioning is the way forward, because:

Data that loses its usefulness can often be easily removed from a partitioned table by dropping the partition (or partitions) containing only that data. Conversely, the process of adding new data can in some cases be greatly facilitated by adding one or more new partitions for storing specifically that data.

And if this doesn't work for you, it's still possible to

In addition, MySQL 5.7 supports explicit partition selection for queries. For example, SELECT * FROM t PARTITION (p0,p1) WHERE c < 5 selects only those rows in partitions p0 and p1 that match the WHERE condition. In this case, MySQL does not check any other partitions of table t; this can greatly speed up queries when you already know which partition or partitions you wish to examine. Partition selection is also supported for the data modification statements DELETE, INSERT, REPLACE, UPDATE, and LOAD DATA, LOAD XML.

Since you want to delete stuff based on date and not the primary key, what you need is a RANGE partition scheme.

First find the oldest date and create partitions based on that

ALTER TABLE sales
    PARTITION BY RANGE( TO_DAYS(last_purchase_date)) (
    PARTITION p0 VALUES LESS THAN (TO_DAYS('2018-12-31')),
    PARTITION p1 VALUES LESS THAN (TO_DAYS('2017-12-31')),
    PARTITION p2 VALUES LESS THAN (TO_DAYS('2016-12-31')),
    PARTITION p3 VALUES LESS THAN (TO_DAYS('2015-12-31')),
    ..
    PARTITION p10 VALUES LESS THAN MAXVALUE));

Choose an appropriate number of partitions but don't worry too much because you can always change partitions later. When you partition, you might even find that the delete step isn't really needed after all.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

delete rows from a table using MySQL Scheduler

From Dev

mysql delete duplicate rows from table

From Dev

Efficiently deleting rows from one table where not matching another [MySQL]

From Dev

Delete lots of rows from a very large Cassandra Table

From Dev

Delete many rows from a large percona mysql db

From Dev

How to efficiently get all the rows from a huge table in Rails?

From Dev

How to efficiently get all the rows from a huge table in Rails?

From Dev

How to delete some similar rows from a table?

From Dev

Delete rows from table

From Dev

How to delete duplicate rows from mysql

From Dev

How to run loop every 24 hours and delete rows from mysql table

From Dev

How to efficiently delete rows based on a comparison

From Dev

How to delete from table then delete what deleted rows referenced? (postgresql)

From Dev

How to Use Delete Triggers to Delete Rows From Same Table?

From Dev

Select and Delete exactly x rows from MySql table

From Dev

DELETE old rows from Mysql General Log Table (MyISAM not CSV)

From Dev

Is it possible to delete multiple rows in MySQL if target table is in from clause?

From Dev

DELETE old rows from Mysql General Log Table (MyISAM not CSV)

From Dev

Delete all rows from MySQL table using PHP

From Dev

How can one efficiently remove a range of rows from a large numpy array?

From Dev

How to delete rows from a table based on a join with another table?

From Dev

How to delete rows from one table matching another table?

From Dev

MySQL delete duplicate rows in table

From Dev

Delete all rows in a table with MySQL?

From Dev

How to delete large amount of data from Oracle table in batches

From Dev

How to delete large amount of data from Oracle table in batches

From Dev

how to delete a single value from mysql table

From Dev

How to delete mysql data from table at midnight?

From Dev

How to delete millions of record from mysql table

Related Related

  1. 1

    delete rows from a table using MySQL Scheduler

  2. 2

    mysql delete duplicate rows from table

  3. 3

    Efficiently deleting rows from one table where not matching another [MySQL]

  4. 4

    Delete lots of rows from a very large Cassandra Table

  5. 5

    Delete many rows from a large percona mysql db

  6. 6

    How to efficiently get all the rows from a huge table in Rails?

  7. 7

    How to efficiently get all the rows from a huge table in Rails?

  8. 8

    How to delete some similar rows from a table?

  9. 9

    Delete rows from table

  10. 10

    How to delete duplicate rows from mysql

  11. 11

    How to run loop every 24 hours and delete rows from mysql table

  12. 12

    How to efficiently delete rows based on a comparison

  13. 13

    How to delete from table then delete what deleted rows referenced? (postgresql)

  14. 14

    How to Use Delete Triggers to Delete Rows From Same Table?

  15. 15

    Select and Delete exactly x rows from MySql table

  16. 16

    DELETE old rows from Mysql General Log Table (MyISAM not CSV)

  17. 17

    Is it possible to delete multiple rows in MySQL if target table is in from clause?

  18. 18

    DELETE old rows from Mysql General Log Table (MyISAM not CSV)

  19. 19

    Delete all rows from MySQL table using PHP

  20. 20

    How can one efficiently remove a range of rows from a large numpy array?

  21. 21

    How to delete rows from a table based on a join with another table?

  22. 22

    How to delete rows from one table matching another table?

  23. 23

    MySQL delete duplicate rows in table

  24. 24

    Delete all rows in a table with MySQL?

  25. 25

    How to delete large amount of data from Oracle table in batches

  26. 26

    How to delete large amount of data from Oracle table in batches

  27. 27

    how to delete a single value from mysql table

  28. 28

    How to delete mysql data from table at midnight?

  29. 29

    How to delete millions of record from mysql table

HotTag

Archive