18 / 19

What happens to indexes during bulk inserts, and how can you optimize performance for such operations?

What Happens to Indexes During Bulk Inserts and How to Optimize Performance

Bulk inserts can put heavy load on MySQL because every inserted row must also update all related indexes. The more indexes a table has, the slower bulk inserts become. MySQL provides several techniques to optimize bulk insert performance, especially for large datasets.

1. What Happens to Indexes During Bulk Inserts
  1. 1

    Each row insertion requires updating every B-tree index.

  2. 2

    Indexes may become fragmented due to many random insert positions.

  3. 3

    Large secondary indexes slow down bulk inserts significantly.

  4. 4

    InnoDB logs index changes in the redo log, increasing I/O load.

  5. 5

    Unique indexes require additional checks, making inserts slower.

2. Why Bulk Inserts Slow Down with Many Indexes
  1. 1

    Each index requires a B-tree search + write.

  2. 2

    More indexes = more writes and more page splits.

  3. 3

    Unique indexes add extra validation overhead.

  4. 4

    Large indexes may not fit in memory, causing extra disk access.

3. Ways to Optimize Bulk Insert Performance
  1. 1

    a. Disable or Drop Indexes Before Bulk Inserts (Best for MyISAM)

  2. 2

    • MyISAM supports fast index rebuilding after bulk insert.

  3. 3

    • Much faster than updating indexes row-by-row.

  4. 4
  5. 5

    b. For InnoDB: Use ALTER TABLE … DISABLE KEYS (No Effect for InnoDB)

  6. 6

    • DISABLE KEYS works only for MyISAM, not InnoDB.

  7. 7
  8. 8

    c. Temporarily Drop Secondary Indexes and Recreate Them After Insert

  9. 9

    • Very effective for massive inserts.

  10. 10

    • Rebuilding one large index is faster than updating millions of rows.

  11. 11
  12. 12

    d. Insert Rows in Primary Key Order

  13. 13

    • Reduces page splits in the clustered index.

  14. 14

    • Improves buffer pool efficiency.

  15. 15
  16. 16

    e. Use Bulk Insert Statements

  17. 17

    • Use multi-row insert batches (e.g., 5k–20k rows per batch).

  18. 18
  19. 19

    f. Disable Foreign Key Checks Temporarily

  20. 20
  21. 21

    SET FOREIGN_KEY_CHECKS = 0;

  22. 22

    SET FOREIGN_KEY_CHECKS = 1;

  23. 23
  24. 24

    • Reduces integrity-check overhead.

  25. 25
  26. 26

    g. Set InnoDB to Use Minimal Logging

  27. 27

    innodb_flush_log_at_trx_commit = 2 during imports.

  28. 28

    innodb_buffer_pool_size should be large to fit indexes.

  29. 29
  30. 30

    h. Use LOAD DATA INFILE Instead of INSERT

  31. 31

    • Fastest option for bulk loading.

  32. 32

    • Optimized internal engine operations.

Example: Best Performing Bulk Load Workflow (InnoDB)
4. How MySQL Handles Index Rebuilding
  1. 1

    Rebuilding an index is a single large operation with sequential writes.

  2. 2

    Bulk rebuilding is faster than incrementally updating per-row.

  3. 3

    Rebuilds reduce fragmentation and produce more compact indexes.

In summary, bulk inserts can become slow due to index maintenance overhead. You can significantly improve performance by minimizing indexes during import, inserting in primary key order, using multi-row inserts, and leveraging optimized loading mechanisms like LOAD DATA INFILE.

Difficulty: 6/10
Topics: index maintenance, bulk insert performance, index disable/enable strategies

Scenario Questions

0-2 years experience
  1. 1

    You're loading 100K rows into a table with three indexes, and it's taking 30 seconds. How would you make it faster without changing the data?

  2. 2

    If you disable an index before a bulk insert and forget to rebuild it, what might break when users query the table later?

  3. 3

    You're told to use LOAD DATA INFILE instead of INSERT statements — why does that help with index performance?

2-5 years experience
  1. 1

    Our nightly data import job is timing out — the table has 5 indexes and we're inserting 2M rows. What steps would you take to diagnose and fix this?

  2. 2

    A feature team added a new index last week, and now our bulk imports are 4x slower. How would you investigate and decide whether to keep the index?

  3. 3

    We're seeing lock waits during bulk inserts even though we're using transactions. What role might indexes play, and how would you test your hypothesis?

5-8 years experience
  1. 1

    You need to import 500M rows into a high-traffic table with 8 indexes. How would you design the process to minimize downtime and avoid impacting OLTP queries?

  2. 2

    In a sharded MySQL setup, bulk inserts are causing replication lag due to index updates. What architectural changes or operational workarounds would you consider?

  3. 3

    How would you implement a zero-downtime bulk load strategy for a critical table where indexes must remain online, and what are the performance tradeoffs?

8+ years experience
  1. 1

    Our legacy system does bulk inserts on a 2TB table with 10 indexes every hour — it's become a bottleneck for the entire data pipeline. How would you redesign this at the architecture level?

  2. 2

    You're migrating from MyISAM to InnoDB and bulk inserts are now 10x slower. What systemic changes would you propose to the data ingestion pipeline, and how would you justify the cost to engineering leadership?

  3. 3

    How would you design a cross-team standard for bulk data ingestion that balances developer velocity, operational risk, and long-term maintenance for tables with varying index requirements?

Follow-up Questions

  • What happens if you disable a primary key index during a bulk load?
  • How would you monitor the impact of index rebuilds on production traffic?
  • When would you choose to keep indexes enabled instead of disabling them?