Databases 6 min read

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.

Java Tech Enthusiast
Java Tech Enthusiast
Java Tech Enthusiast
MySQL Large Table Data Deletion Optimization Strategy

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performance tuningmysqlDatabase Optimizationpaginationdata cleanuplarge table deletion
Java Tech Enthusiast
Written by

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!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.