Databases 4 min read

When Does a MySQL Table Need Sharding? Understanding Size Limits and Performance

This article explains MySQL's lack of a hard row limit, the industry rule of thumb around 5 million rows, how InnoDB buffer size and hardware affect performance, and demonstrates a simple insertion test that reveals slowdown when a table grows too large.

Programmer DD
Programmer DD
Programmer DD
When Does a MySQL Table Need Sharding? Understanding Size Limits and Performance

MySQL itself does not impose a hard limit on the number of rows in a single table; the limit depends on the operating system's file size constraints, though a common rule of thumb in the industry is about 5 million rows, after which sharding may be considered.

Alibaba's "Java Development Manual" recommends sharding when a single table exceeds 5 million rows or its size exceeds 2 GB.

A Simple Test

The following test inserts rows in a loop, records the count, and prints it to the console.

private static void insertDataDemo() {
  DButil dButil = new DButil();
  myCon = dButil.getConnection();
  try {
    int i = 0;
    while(1==1) {
      i++;
      String sql = "insert into users (user_name,user_password)"
        + " value ('"+i+"','password')";
      sta = myCon.createStatement();
      sta.execute(sql);
      System.out.println(i);
    }
  } catch (Exception e) {
    e.printStackTrace();
  } finally {
    dButil.close();
  }
}

After running overnight, more than two million rows were inserted, but the insertion speed slowed to one row every 2–3 seconds, which is unacceptable.

The maximum amount of data a MySQL table can store is not directly tied to row count but to MySQL configuration and hardware. MySQL loads table indexes into memory; with a sufficient InnoDB buffer size, the entire index can reside in memory, keeping queries fast.

When a table grows beyond a certain size, the index can no longer fit in memory, causing disk I/O during queries and degrading performance. This issue is also influenced by table schema design, but ultimately stems from memory limitations.

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.

performanceshardingInnoDBmysqldatabase scalingtable size
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

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.