Database Capacity Planning and Storage
What you will learn
- What capacity planning is and why it matters
- How to measure the size of rows and indexes
- How to project growth with simple arithmetic
- What else to plan: backups, headroom, memory
- When a database should be moved or split
1. The disk always fills up sooner than you think
A full disk is one of the most common reasons databases stop. Writes fail, backups fail and sometimes the whole application goes down. It is also one of the easiest problems to predict. Capacity planning means estimating how much storage, memory and processing the database will need, and acting before it runs out.
You do not need advanced mathematics, only measurements and multiplication.
2. Measure bytes per row
Instead of guessing, build a sample and measure it. Here we create the same table with 1,000, 10,000 and 100,000 rows, once with no extra indexes and once with two indexes:
import sqlite3, os, tempfile
def build(n, with_index):
path = os.path.join(tempfile.mkdtemp(), "m.db")
con = sqlite3.connect(path)
con.execute("CREATE TABLE orders (id INTEGER PRIMARY KEY, customer INTEGER, placed TEXT, total INTEGER)")
if with_index:
con.execute("CREATE INDEX idx_customer ON orders(customer)")
con.execute("CREATE INDEX idx_placed ON orders(placed)")
con.executemany("INSERT INTO orders (customer, placed, total) VALUES (?, ?, ?)",
[(i % 1000, f"2026-{1 + i % 12:02d}-{1 + i % 28:02d}", i % 500) for i in range(n)])
con.commit()
size = os.path.getsize(path)
con.close()
return size
print("rows table only with 2 indexes")
for n in (1000, 10000, 100000):
a, b = build(n, False), build(n, True)
print(f"{n:<9} {a:>10} B {b:>10} B ({a / n:.1f} and {b / n:.1f} bytes per row)")
rows table only with 2 indexes
1000 32768 B 73728 B (32.8 and 73.7 bytes per row)
10000 245760 B 561152 B (24.6 and 56.1 bytes per row)
100000 2473984 B 5738496 B (24.7 and 57.4 bytes per row)
What the numbers tell us:
- Once the table has some size, the cost settles at about 25 bytes per row for the table alone. Small tables show higher figures per row because of fixed overhead, so measure with a realistic amount of data.
- Two indexes more than doubled the size, to about 57 bytes per row. Indexes are not free (lesson 7.7).
- The figures belong to this table, this data and this SQLite version. Your own table will differ: text columns, long notes and many columns all change the size.
The same measurement on the sample data. This lesson runs on the sample database you chose in lesson 4.1 (use the Example data picker at the top). Copy a table with CREATE TABLE ... AS SELECT, and see how many bytes the copy added to the database (page count × page size). The copy lives in your browser's copy of the data; Reset data removes it.
SELECT page_count * page_size AS bytes_before FROM pragma_page_count(), pragma_page_size();
CREATE TABLE order_items_copy AS SELECT * FROM order_items;
SELECT page_count * page_size AS bytes_after FROM pragma_page_count(), pragma_page_size();
SELECT COUNT(*) AS rows_copied FROM order_items_copy;
bytes_before
------------
536576
(1 row)
bytes_after
-----------
663552
(1 row)
rows_copied
-----------
5790
(1 row)
SELECT page_count * page_size AS bytes_before FROM pragma_page_count(), pragma_page_size();
CREATE TABLE transactions_copy AS SELECT * FROM transactions;
SELECT page_count * page_size AS bytes_after FROM pragma_page_count(), pragma_page_size();
SELECT COUNT(*) AS rows_copied FROM transactions_copy;
bytes_before
------------
528384
(1 row)
bytes_after
-----------
917504
(1 row)
rows_copied
-----------
7415
(1 row)
SELECT page_count * page_size AS bytes_before FROM pragma_page_count(), pragma_page_size();
CREATE TABLE attendance_copy AS SELECT * FROM attendance;
SELECT page_count * page_size AS bytes_after FROM pragma_page_count(), pragma_page_size();
SELECT COUNT(*) AS rows_copied FROM attendance_copy;
bytes_before
------------
655360
(1 row)
bytes_after
-----------
839680
(1 row)
rows_copied
-----------
6012
(1 row)
Divide the growth in bytes by the number of rows copied to get the bytes per row of this table. The projection in the next section is then one line of SQL:
SELECT 2000 * 60 * 365 AS bytes_per_year, ROUND(2000 * 60 * 365 / 1024.0 / 1024.0, 1) AS mib_per_year, ROUND(50 * 1024 * 1024 / (2000.0 * 60 * 365), 1) AS years_until_50_mib_disk_is_full;
bytes_per_year mib_per_year years_until_50_mib_disk_is_full
-------------- ------------ -------------------------------
43800000 41.8 1.2
(1 row)
SELECT 2000 * 60 * 365 AS bytes_per_year, ROUND(2000 * 60 * 365 / 1024.0 / 1024.0, 1) AS mib_per_year, ROUND(50 * 1024 * 1024 / (2000.0 * 60 * 365), 1) AS years_until_50_mib_disk_is_full;
bytes_per_year mib_per_year years_until_50_mib_disk_is_full
-------------- ------------ -------------------------------
43800000 41.8 1.2
(1 row)
SELECT 2000 * 60 * 365 AS bytes_per_year, ROUND(2000 * 60 * 365 / 1024.0 / 1024.0, 1) AS mib_per_year, ROUND(50 * 1024 * 1024 / (2000.0 * 60 * 365), 1) AS years_until_50_mib_disk_is_full;
bytes_per_year mib_per_year years_until_50_mib_disk_is_full
-------------- ------------ -------------------------------
43800000 41.8 1.2
(1 row)
3. Project the growth
Now a made-up shop. It gets about 2,000 new order rows a day, and we use a round 60 bytes per row, rounded up from the measurement above to leave some room:
rows_per_day = 2000
bytes_per_row = 60 # round figure for the example, taken from the measurement above
days = 365
growth = rows_per_day * bytes_per_row * days
print("one year of growth:", growth, "bytes =", round(growth / 1024 / 1024, 1), "MiB")
disk = 50 * 1024 * 1024
print("years until a 50 MiB disk is full:", round(disk / growth, 1))
one year of growth: 43800000 bytes = 41.8 MiB
years until a 50 MiB disk is full: 1.2
Multiply: rows per day × bytes per row × days. The disk size here is deliberately tiny so that the answer is dramatic: the made-up disk would last about 1.2 years. Real disks are far bigger, but the method is the same: current size, growth rate, space left, so time left.
Check the growth rate against reality every month (lesson 12.3). Growth is rarely flat. A marketing campaign, a new feature or a new customer can double it.
4. The space you forget
The data is not the only thing that needs room.
| Also needs space | Why |
|---|---|
| Indexes | often as large as the table, or more (above) |
| Free pages and bloat | freed space is reserved, not returned (lesson 12.2) |
| Backups | each copy is about the size of the database, and you keep several (lesson 8.2) |
| Temporary space | sorting and rebuilding (VACUUM, adding an index) use extra space while they work |
| Logs | change logs and slow query logs also grow |
| Headroom | keep a safe margin; a disk at 95% leaves no room to fix anything |
A common rule of thumb is to act when the disk reaches about 70 to 80% full, not 100%, because buying disk, moving data or rebuilding all take time. The exact figure is a judgement for your situation.
Memory matters too. A database is fast when the data it uses often (the "hot" data) fits in memory. If the hot part grows beyond memory, queries start reading the disk and slow down. The cure is more memory, better indexes, or keeping old data elsewhere.
5. When the plan says "too big"
Choices when growth outruns the machine, from simplest to hardest:
- Delete or archive old data that nobody needs online (lesson 10.3).
- Trim indexes nobody uses, and large columns nobody reads.
- Buy a bigger machine (scaling up), often the cheapest and simplest answer.
- Move heavy reports to a separate analytics database (chapter 13).
- Spread the data over several machines (partitioning, sharding; lessons 9.6 and 14.3), the hardest answer, which brings new problems. Do it last.
Worked example: a yearly capacity check
A tailor shop's database is 400 MiB today. A year ago it was 250 MiB. The disk is 1,000 MiB.
| Step | Calculation | Result |
|---|---|---|
| Growth in the last year | 400 − 250 | 150 MiB per year |
| Backups kept (3 copies) | 3 × 400 | 1,200 MiB, more than this disk |
| Free space on the disk, ignoring backups | 1,000 − 400 | 600 MiB |
| Years of data space left at this rate | 600 ÷ 150 | 4 years |
Spare room needed while VACUUM runs |
about one extra copy: 400 | the plan needs at least 800 MiB |
The result: the data would fit for years, but the backups on the same disk would not fit at all. The fix is to store backups on a different disk or place (also safer, lesson 8.2). The figures are made up; the habit is to check all the uses of space.
Common mistakes
- Guessing the size instead of measuring a realistic sample.
- Forgetting indexes, which can be as big as the data.
- Keeping backups on the same disk as the database.
- Waiting until the disk is nearly full.
- Assuming growth is constant, and never re-checking it.
Key takeaways
- Capacity planning means estimating storage and memory needs before they run out.
- Measure bytes per row on a realistic sample; indexes can more than double the size.
- Projection: rows per day × bytes per row × days; compare with the space left.
- Plan for backups, temporary space, logs and headroom, and act well before the disk is full.
- Prefer simple answers (archive, trim, bigger machine) before splitting data across machines.
Practice
1. In the lesson, how many bytes per row did the table alone need at 100,000 rows, and with two indexes?
Show answer
About 24.7 bytes per row for the table alone and about 57.4 with two indexes.
2. A shop adds 5,000 rows a day at 80 bytes per row. How many MiB does it grow in 30 days? (1 MiB = 1,048,576 bytes.)
Show answer
5,000 × 80 × 30 = 12,000,000 bytes, which is about 11.4 MiB.
3. Why should you not wait until the disk is 100% full?
Show answer
Writes and backups start failing, and fixing the problem (moving data, rebuilding) itself needs free space and time. Acting at about 70 to 80% leaves room.
4. List four things besides the table data that use disk space.
Show answer
Indexes, free pages or bloat, backups, temporary space during rebuilds and sorts, and logs.
5. Put these in order from simplest to hardest: sharding, archiving old data, buying a bigger machine.
Show answer
Archiving old data, buying a bigger machine, sharding.
6. Why does the lesson say to re-check the growth rate every month?
Show answer
Growth is rarely constant. A campaign, feature or new customer can change it, so an old projection can be wrong.