Skip to content

Money, consistency and distributed data · Part 8 of 10

Tables with hundreds of millions of rows: a staged plan

When a transactions table keeps growing and slowing, work from cheap to expensive: fix queries, archive old data, partition by time, offload reads, and shard only as a last resort.

· 3 min read

mysqlpartitioningscalability

A transactions table that has grown to hundreds of millions of rows does not need a dramatic redesign on the first day it feels slow. Most of the improvement comes from simple steps, so take them in order, cheapest first, and measure after each one.

Start by finding the real bottleneck

Check that the slowest queries use the right indexes, with EXPLAIN as covered earlier in this series. Many "the table is too big" problems are a missing composite index or a query that scans instead of seeks. Also check that the working set, meaning the hot indexes and recent pages, fits in the InnoDB buffer pool. A table can be huge and fast if most queries touch only recent data through a good index.

Archive and purge

Transactions older than the period users actually query, perhaps 12 to 24 months, are rarely read. Move them to cheaper storage such as object storage or an analytics warehouse, then remove them from the primary database. Never delete a hundred million rows in one statement: it creates a giant transaction, holds locks, bloats the undo log and stalls replication. Delete in small batches and pause between them.

Code
-- repeat until zero rows are affected, sleeping a little between batches
DELETE FROM transactions
WHERE created_at < '2024-01-01'
ORDER BY id
LIMIT 5000;

Partition by time

Partitioning splits one logical table into several physical ones. With range partitioning by month, queries that filter on the date touch only the relevant partitions (partition pruning), and removing old data becomes DROP PARTITION, which is nearly instant and creates no large transaction.

Code
CREATE TABLE transactions (
  id          BIGINT NOT NULL AUTO_INCREMENT,
  wallet_id   BIGINT NOT NULL,
  amount      BIGINT NOT NULL,
  created_at  DATETIME NOT NULL,
  PRIMARY KEY (id, created_at),
  KEY idx_wallet_time (wallet_id, created_at)
) ENGINE=InnoDB
PARTITION BY RANGE COLUMNS (created_at) (
  PARTITION p2026_01 VALUES LESS THAN ('2026-02-01'),
  PARTITION p2026_02 VALUES LESS THAN ('2026-03-01'),
  PARTITION pmax     VALUES LESS THAN (MAXVALUE)
);

ALTER TABLE transactions DROP PARTITION p2026_01;   -- archive done, instantly

Know the limits before you commit to it.

  • Every primary key and unique key must include the partitioning column, so the key above is (id, created_at). Uniqueness of id alone is no longer enforced by the database.
  • InnoDB partitioned tables do not support foreign keys.
  • Queries that do not filter on the partitioning column scan all partitions and can be slower than before.
  • You must create new partitions ahead of time, normally with a scheduled job.

Offload reads and analytics

Reporting queries that scan large ranges do not belong on the primary. Send reads that tolerate slight staleness to a read replica, remembering that replication lag means a user might not see a payment they made a second ago. Send analytics to a columnar warehouse such as BigQuery through change data capture or periodic export. OLTP and analytics need different storage layouts and should not fight for the same resources.

Sharding as a last resort

Sharding divides the data across several databases by a key such as the wallet or user id. It scales writes beyond one machine, and it also brings cross-shard queries, resharding, distributed transactions and operational overhead. Reach for it only after the earlier steps are exhausted and measurements show a single primary is the limit.

Takeaway

Fix the queries and indexes first, archive and purge old rows in small batches, partition by time for pruning and cheap retention, move reads and analytics off the primary, and shard last. Each step is more complex than the one before, so stop as soon as the numbers are good enough.

A knowledge article, drafted with AI assistance and checked with automated tests. It is general background, not a description of my own experience or of any client's work.

Comments

Leave a comment

No account or email needed. Comments appear after review. A one-way hash of your IP address is kept for 30 days to fight spam, then erased.