Database Index Size Calculator: PostgreSQL and MySQL B-tree Estimates
By FindMySum · Last reviewed
An index size calculator estimates how much disk and memory a B-tree index will take before you create it. Enter the number of rows and the size of the indexed key, choose PostgreSQL or MySQL, and the calculator works through the database's page layout to give the number of pages, the tree's height, the size in megabytes and the cost per row. It is useful when planning a new index on a large table, comparing key types such as bigint and UUID, or checking whether an existing index has bloated.
How a B-tree index is stored
Both PostgreSQL and MySQL's InnoDB store B-tree indexes in fixed-size pages: 8 KB in PostgreSQL and 16 KB in InnoDB by default. The leaf pages hold one entry per row, in key order, each pointing to the row. Above them, internal pages hold one entry per page below, until a single root page covers everything. Because each page holds hundreds of entries, even huge indexes are only three or four levels deep.
The size of an index is therefore the number of pages times the page size, and the number of pages depends on how many entries fit on one.
PostgreSQL
In PostgreSQL every page starts with a 24-byte header, and B-tree pages end with 16 bytes of special space for sibling links. Each entry is an index tuple, an 8-byte header plus the key, padded to a multiple of 8 bytes on 64-bit systems, and a 4-byte line pointer in the page's item array. When an index is built, leaf pages are filled to the fill factor (90% by default) and internal pages to 70%.
For the default example, a bigint key: 8 + 8 = 16 bytes, already a multiple of 8, plus the 4-byte pointer, makes 20 bytes per entry. A leaf page has 8,192 − 40 = 8,152 usable bytes, less the 819 bytes the 90% fill factor leaves free, so 366 entries fit. Ten million rows need 27,323 leaf pages; the internal levels add 98 pages and the metapage 1, for 27,422 pages or about 225 MB, 22.5 bytes per row.
The padding has a surprising consequence: an index on a 4-byte integer is exactly the same size as one on an 8-byte bigint. The check against real databases is reassuring here: a million-row integer index built this way is 2,745 pages, the figure PostgreSQL itself reports.
MySQL InnoDB
InnoDB secondary index records have a 5-byte header, the indexed columns and, crucially, the table's primary key, which InnoDB uses to find the row in the clustered index. Pages carry about 128 bytes of headers, trailer and boundary records, plus a 2-byte directory slot for every few records. With a bigint key and a bigint primary key, each record is 21 bytes and a full 16 KB page holds 755 of them; ten million rows take 13,268 pages, about 217 MB.
Because the primary key is copied into every secondary index, a wide primary key inflates them all. A UUID stored as 36 characters of text costs 37 bytes per record per index; stored as BINARY(16) it costs 16.
Why real indexes are often bigger
The calculator gives the size of an index built in one pass, or filled with keys in increasing order, such as an auto-increment ID or a timestamp. Indexes filled in random order behave differently. When a new key lands on a full page, the page splits into two half-full pages, and over time leaf pages settle around 70% full. Random UUIDv4 primary keys are the classic case. Choose a 70% fill factor in the advanced options to model it.
Updates and deletes add more: dead entries remain until vacuum (PostgreSQL) or purge (InnoDB) removes them, and freed space inside a page is only reused by keys that belong there. Rebuilding the index with REINDEX or OPTIMIZE TABLE returns it to the compact size.
Using the estimate
Two numbers matter most. The total size tells you the disk you need and how long building or rebuilding the index will take. The leaf size compared with your memory (PostgreSQL's shared_buffers and the operating system cache, or InnoDB's buffer pool) tells you whether lookups will be served from memory. The bar chart shows how the size grows as the table does: linearly with the rows, with the tree adding a level only every few hundred times the row count.
More questions
Do indexes need to fit in memory?
Ideally the parts you use often should. The upper levels of a B-tree are small and stay cached; the leaf pages are where most of the size is. If the leaves of a busy index don't fit in the buffer pool or shared buffers, lookups start reading from disk. Compare the index size with your cache memory, not your disk.
How much does a composite index cost?
Add the sizes of all its columns to get the key size. A PostgreSQL index on (user_id bigint, created_at timestamp) has a 16-byte key, padded tuples of 24 bytes and 28-byte entries, about 40% larger than an index on user_id alone. Put the column you filter on with equality first.
What is index deduplication in PostgreSQL?
Since PostgreSQL 13, a B-tree can store a repeated key once with a list of row pointers, instead of repeating the key for every row. Indexes on columns with few distinct values, such as status or country, shrink a lot. Unique and nearly unique columns don't benefit, which is what this calculator assumes.
Are partial indexes smaller?
Yes. A partial index only includes rows matching its WHERE clause, so enter only those rows. An index on unprocessed jobs (WHERE processed = false) can stay tiny even when the table holds millions of finished ones.
Why does my PostgreSQL index grow even though the row count is stable?
Updates create new row versions, and every index gets an entry for the new version unless the update is a HOT update that changes no indexed column and fits on the same page. Old entries are cleaned up by vacuum, but the pages they free are reused only within the same index. Frequent updates to indexed columns cause this bloat; REINDEX CONCURRENTLY rebuilds it.
Sources
This article explains how the calculator works and is general information, not financial, tax, legal or medical advice. Rules change; check the sources above or a qualified professional before you act.