Skip to main content
Documents
Clauses StatementsKeywordsExpressionsFunctionsIndexesPerformance Optimization

MySQL Disabling Index for Bulk Inserts Indexes

Indexes in MySQL are data structures that improve the speed of data retrieval operations on a database table. They allow quick access to rows in a table, much like an index in a book, but they can slow down write operations such as bulk inserts.

Usage

Indexes are used to enhance the performance of SELECT queries and other data retrieval operations by providing a quick way to locate data. However, during bulk inserts, disabling indexes can significantly improve performance because index maintenance can be costly. The overhead comes from the need to update the index structure for each inserted row.

ALTER TABLE table_name DISABLE KEYS;
-- Perform bulk insert operations here
ALTER TABLE table_name ENABLE KEYS;

In this syntax, `DISABLE KEYS` temporarily disables all non-unique indexes, while `ENABLE KEYS` re-enables them after the insert operation. **Note:** This operation is only applicable to MyISAM tables and does not affect primary keys or unique indexes.

Examples

1. Basic Index Disabling

ALTER TABLE sales DISABLE KEYS;
-- Perform your bulk insert here
ALTER TABLE sales ENABLE KEYS;

This example demonstrates disabling and then re-enabling indexes for the `sales` table around a bulk insert operation.

2. Using DISABLE KEYS during Data Import

ALTER TABLE products DISABLE KEYS;
LOAD DATA INFILE 'data.csv' INTO TABLE products;
ALTER TABLE products ENABLE KEYS;

Here, indexes on the `products` table are disabled during a file import to enhance the speed of the operation.

3. Disabling Indexes for a Large Bulk Insert with Multiple Statements

ALTER TABLE orders DISABLE KEYS;
INSERT INTO orders (order_id, customer_id, order_date) VALUES (1, 101, '2023-01-01'), (2, 102, '2023-01-02'), ...;
ALTER TABLE orders ENABLE KEYS;

This example shows how to disable indexes for a large bulk insert operation with multiple `INSERT` statements on the `orders` table.

Tips and Best Practices

  • Use for large datasets. Disable indexes only when inserting large volumes of data to benefit from reduced overhead.
  • Re-enable indexes. Always remember to re-enable indexes after completing bulk insert operations to ensure optimal query performance. Be aware that rebuilding indexes during the `ENABLE KEYS` operation can be resource-intensive and may cause downtime.
  • Monitor performance. Evaluate the performance impact of disabling and re-enabling indexes, especially on tables with heavy read operations.
  • Backup before operation. Consider taking a backup of your data before performing bulk operations with index changes to prevent data loss.

**Note:** Disabling indexes is only applicable for MyISAM tables and does not affect primary keys or unique indexes. For InnoDB tables, consider other optimization strategies as `DISABLE KEYS` is not applicable.

SQL Upskilling for Beginners

Gain the SQL skills to interact with and query your data.
Start Learning for Free