MySQL Large Table Data Deletion Optimization Strategy
To efficiently purge old user‑operation logs while retaining the most recent three months and preserving type‑c entries, the article recommends abandoning offset pagination and MyBatis PageHelper, using primary‑key‑based pagination with a tracked startId, in‑memory filtering, intelligent startId recalculation, and optional sleep intervals to avoid database overload.
This article discusses the optimization strategy for deleting data from large MySQL tables. The scenario involves a table storing user operation logs that grows over time and requires periodic cleanup. The business requirements specify keeping only the last 3 months of data while excluding certain log types (type = 'c') from deletion.
The article presents three approaches: an initial basic method using direct SQL queries with both date and type conditions, a failed optimization attempt that removed the type condition from SQL but introduced pagination issues, and a successful final solution. The successful approach uses primary key-based pagination with startId tracking, filters data in memory, and implements intelligent startId calculation to minimize scanning overhead for subsequent deletions.
Key technical insights include avoiding MyBatis PageHelper for large-scale deletions, using primary key-based pagination instead of offset-based pagination, and optimizing the startId calculation to reduce scanning scope after the first deletion run. The solution also recommends adding sleep intervals during initial large-scale deletions to prevent database overload.
Java Tech Enthusiast
Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.