Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Why does every extra index slow down INSERT, UPDATE and DELETE, and how would you load 50 million rows into a new table quickly?

Every index is a separate structure that each write must also change: an INSERT adds an entry to every index, a DELETE marks or removes entries, and an UPDATE may do both, with page splits, locks and log records for each. To load 50 million rows into a new table quickly, we load the data first with COPY in PostgreSQL or LOAD DATA in MySQL, then create indexes and constraints, give the index builds plenty of memory, and run ANALYZE at the end.

Why this question is asked

Indexes make reads fast, and it is easy to forget that someone pays for them. Every index is paid for on every write, for the whole life of the table. Interviewers ask this question to check that we understand where the cost comes from, and because bulk loading is a common task where the order of operations changes the run time from hours to minutes. In this answer, we will see what an index costs on each kind of write, follow a load done the slow way and the fast way, tune the index build, and look at what changes when the table is live.

1A Quick Recap of What an Index Is

An index is a separate data structure, usually a B-tree, a balanced tree of pages that keeps keys in sorted order with pointers to the rows. It is stored apart from the table. A table with five indexes is really six structures on disk that must always agree with each other. Keeping them in agreement is the database's job, and it does that work during every write.

2What Each Write Does to Every Index

For an INSERT, the database writes the row, then for each index it descends the tree to the right leaf page, inserts the new entry in sorted position, and if the page is full, splits it into two and updates the parent. Each change takes a short lock on the page and writes a log record, the WAL in PostgreSQL or the redo log in InnoDB, so the change survives a crash and reaches replicas. A unique index also checks that the key does not exist yet.

A DELETE must eventually remove the entry from every index; PostgreSQL leaves it for vacuum to clean, InnoDB marks it and purges it later. An UPDATE that changes an indexed column removes the old entry and inserts a new one. In PostgreSQL, an update usually writes a new row version, which needs new entries in every index unless the update qualifies as HOT. So the write cost grows roughly with the number of indexes.

Work for one INSERT
Indexes on the tableStructures changedLog records (roughly)
0table only1
3table + 3 B-trees4, more on page splits
9table + 9 B-trees10, more on page splits
Each index adds a tree descent, a sorted insert, a possible split and a log record to every write.

3Loading 50 Million Rows the Slow Way

The slow way is to create the table with all its indexes and then insert rows, worst of all one INSERT per row from the application, each in its own transaction.

SQL
CREATE TABLE events (id bigint PRIMARY KEY, user_id bigint, kind text, created_at timestamptz);
CREATE INDEX ON events (user_id);
CREATE INDEX ON events (created_at);
-- application loop: 50,000,000 x INSERT ... ; COMMIT;
-- each row: 3 random index inserts, 1 commit and log flush
-- run time: many hours

Here 50 million rows mean 150 million individual index insertions into trees that are growing in random order, plus 50 million commits, each waiting for the log to be flushed to disk. Most of the time goes into maintaining the indexes one entry at a time.

4Loading the Fast Way: Data First, Indexes Second

For a new table, we load the data first with the bulk loader and build the indexes afterwards. COPY in PostgreSQL and LOAD DATA INFILE in MySQL stream rows in a single command with far less overhead per row than INSERT statements.

SQL
CREATE TABLE events (id bigint, user_id bigint, kind text, created_at timestamptz);
COPY events FROM '/data/events.csv' WITH (FORMAT csv, HEADER true); -- COPY 50000000
SET maintenance_work_mem = '2GB'; -- memory for sorting during index builds
SET max_parallel_maintenance_workers = 4;
ALTER TABLE events ADD PRIMARY KEY (id); -- one sort, then one bottom-up build
CREATE INDEX ON events (user_id);
CREATE INDEX ON events (created_at);
ANALYZE events; -- statistics for the planner

The reason this is so much faster is how an index is built from existing data. Instead of 50 million random insertions, the database reads the table once, sorts all keys, and writes the B-tree from the bottom up, filling each leaf page in order without any splits. A sort of 50 million keys with enough memory takes minutes. maintenance_work_mem is the PostgreSQL setting that controls how much memory that sort may use; with too little, it spills to disk in many passes. Parallel workers speed up B-tree builds further.

The final ANALYZE matters as much as the load. A brand-new table has no statistics until ANALYZE or autovacuum runs, and the first queries against it would be planned on defaults.

5MySQL Notes

In MySQL we use LOAD DATA INFILE or multi-row INSERT statements in large transactions, then add secondary indexes with ALTER TABLE, which InnoDB builds by sorting. Because InnoDB stores rows in primary key order, loading the file already sorted by the primary key avoids page splits in the clustered index. Setting unique_checks = 0 and foreign_key_checks = 0 for the session speeds up loading, but only when we know the data is clean, since the checks are simply skipped and duplicates or orphans would get in.

6When the Table Is Already Live

On a table that serves traffic, we cannot drop indexes freely, because queries depend on them. Instead we batch: insert a few thousand rows per transaction with multi-row INSERT or COPY into the live table, keep each transaction short so locks and replication lag stay moderate, and pause between batches if replicas fall behind. If the load is large compared with the table, an alternative is to load into a new table, build its indexes, and swap it in, or attach it as a partition.

7Follow-up questions

Why not disable indexes during the load in PostgreSQL?

PostgreSQL has no command to suspend an index. On a new table we simply create the indexes after the load. On an existing table we could drop and recreate them, but that leaves queries without the index for the duration, so it only makes sense in a maintenance window or for a table nobody reads yet.

Does the primary key also slow the load?

Yes, it is a unique index like the others, so it pays the same insertion cost and adds a uniqueness check. That is why, for a new table, we add the primary key after COPY too. The build then also verifies uniqueness once, and fails clearly if the file contains duplicates.

8Conclusion

In this answer, we saw that every index is a separate structure that each INSERT, UPDATE and DELETE must change, with tree descents, page splits, locks and log records, so write cost grows with the number of indexes. For a bulk load into a new table, we load first with COPY or LOAD DATA, then build the primary key, indexes and constraints from sorted data, with more maintenance_work_mem in PostgreSQL, and run ANALYZE at the end. In MySQL we can relax unique and foreign key checks only for data we know is clean, and on a live table we batch inserts in moderate transactions instead of dropping indexes.

What to say in the interviewAbout 90 seconds aloud

Open with

Each index is its own B-tree, so every write has to change every index too. An insert descends each tree, puts the entry in sorted position, sometimes splits a page and updates the parent, takes short page locks and writes a log record for each change.

Then, if they want more

A delete has to remove entries, and an update to an indexed column removes and inserts. In PostgreSQL an update usually writes a new row version that needs entries in all indexes unless it is a HOT update. So with nine indexes, one insert is about ten structure changes.

For loading 50 million rows into a new table, I would create the table without indexes, load with COPY in PostgreSQL or LOAD DATA INFILE in MySQL, and only then add the primary key, indexes and constraints.

Building an index from existing data means one sort and a bottom-up build with full pages and no splits, which is far cheaper than 50 million random insertions. In PostgreSQL I would raise maintenance_work_mem for the session and allow parallel maintenance workers.

In MySQL I would load sorted by primary key, and consider turning off unique and foreign key checks only if I know the data is clean. Then I would run ANALYZE so the planner has statistics. On a live table I cannot just drop indexes, so I would batch the inserts in moderate transactions and watch replica lag.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Why does every extra index slow down INSERT, UPDATE and DELETE, and how would you load...": https://scrivio-interview-library.netlify.app/databases/indexing/d3d4f74095/