MySQL error 1151 occurs when the number of background INSERT DELAYED threads exceeds the configured limit. This legacy feature is deprecated in modern MySQL versions and typically requires adjusting max_delayed_threads or refactoring to use regular inserts.
MySQL Error 1151 (ER_TOO_MANY_DELAYED_THREADS) indicates that the database has reached its maximum limit for concurrent INSERT DELAYED handler threads. INSERT DELAYED is a MySQL feature that allows INSERT statements to be queued and executed in the background, improving application responsiveness during high-traffic scenarios. When too many delayed insert operations are queued simultaneously, MySQL refuses new delayed insert requests to prevent resource exhaustion. This error signals that your delayed insert workload exceeds the configured thread capacity.
Before adjusting configurations, verify whether your MySQL version still supports INSERT DELAYED. In MySQL 8.0+, the DELAYED keyword is deprecated and ignored—the server treats DELAYED inserts as regular inserts.
SELECT VERSION();If you are using MySQL 8.0 or later, consider refactoring your application to remove INSERT DELAYED entirely.
Connect to your MySQL instance and check the current number of active delayed insert threads and the configured limit:
SHOW STATUS LIKE "Delayed_writes";
SHOW STATUS LIKE "Delayed_insert_threads";
SHOW VARIABLES LIKE "max_delayed_threads";This shows you how many delayed threads are currently in use and the maximum allowed. If the current count is at or near the max_delayed_threads limit, you have found the bottleneck.
If you are still using a MySQL version that supports INSERT DELAYED (5.1 through 5.7), you can temporarily increase the max_delayed_threads limit. Add or modify the following in your MySQL configuration file (my.cnf or my.ini):
[mysqld]
max_delayed_threads = 50Then restart the MySQL service:
sudo systemctl restart mysql
# or on some systems:
sudo service mysql restartMonitor the impact with:
SHOW STATUS LIKE "Delayed_insert_threads";Note: Be cautious about setting this too high, as each thread consumes server memory and resources.
The long-term solution is to eliminate INSERT DELAYED from your application code. Convert delayed insert calls to regular, non-delayed inserts:
Before (INSERT DELAYED):
INSERT DELAYED INTO logs (user_id, action, timestamp)
VALUES (123, "login", NOW());After (Regular INSERT):
INSERT INTO logs (user_id, action, timestamp)
VALUES (123, "login", NOW());If you need non-blocking inserts for performance, consider these alternatives:
- Use connection pooling to handle concurrent requests efficiently
- Implement a message queue (Redis, RabbitMQ) for async logging
- Batch multiple inserts into a single transaction
- Use prepared statements to reduce overhead
For MySQL versions prior to 8.0, you can fine-tune related parameters to handle delayed inserts more efficiently:
[mysqld]
delayed_insert_limit = 200 # Rows processed before checking for SELECT statements
delayed_insert_timeout = 600 # Seconds before idle thread terminates
delayed_queue_size = 2000 # Per-table queue size limitThese parameters help the server batch delayed operations more efficiently:
- Higher delayed_insert_limit allows more rows to be processed in one batch
- Higher delayed_insert_timeout keeps threads alive longer if traffic is intermittent
- Higher delayed_queue_size allows more rows to be queued per table
After making changes, restart MySQL and monitor performance.
Set up monitoring to track delayed insert performance and ensure the error does not reoccur:
-- Check delayed insert statistics
SHOW STATUS LIKE "Delayed%";
-- Monitor in real-time (run periodically)
WATCH -n 1 "mysql -e \"SHOW STATUS LIKE 'Delayed%';\"";Key metrics to track:
- Delayed_writes: Total number of INSERT DELAYED statements executed
- Delayed_insert_threads: Current active delayed insert threads
- Delayed_errors: Failed delayed insert operations
If thread count approaches your configured limit again, you need further optimization or a migration strategy.
INSERT DELAYED is a deprecated MySQL feature that was removed in version 8.0. If you encounter this error on MySQL 8.0+, your application is still attempting to use DELAYED inserts, but MySQL silently converts them to regular inserts and generates a deprecation warning (ER_WARN_LEGACY_SYNTAX_CONVERTED). For production systems, the best approach is to migrate away from DELAYED inserts entirely using async message queues or batch inserts. INSERT DELAYED only works with MyISAM, MEMORY, ARCHIVE, and BLACKHOLE table types—modern applications should use InnoDB instead. If you are on an older MySQL version (5.7 or earlier) and cannot immediately refactor, increasing max_delayed_threads provides temporary relief, but plan a migration to regular or asynchronous inserts as a long-term solution.
ERROR 1064: You have an error in your SQL syntax
How to fix "ERROR 1064: You have an error in your SQL syntax" in MySQL
ERROR 1054: Unknown column in field list
Unknown column in field list
ER_WINDOW_RANGE_FRAME_NUMERIC_TYPE (3589): RANGE frame requires numeric ORDER BY expression
RANGE frame requires numeric ORDER BY expression in MySQL window functions
CR_ALREADY_CONNECTED (2058): Handle already connected
How to fix "CR_ALREADY_CONNECTED (2058): Handle already connected" in MySQL
ER_WINDOW_DUPLICATE_NAME (3591): Duplicate window name
How to fix ER_WINDOW_DUPLICATE_NAME (3591) in MySQL window functions