# Concurrent Inserts

The [MyISAM](https://gitbook-v2-q3lwp8faq-gitbook.vercel.app/url/mariadb.com/docs/server/server-usage/storage-engines/myisam-storage-engine) storage engine supports concurrent inserts. This feature allows [SELECT](https://gitbook-v2-q3lwp8faq-gitbook.vercel.app/url/mariadb.com/docs/server/reference/sql-statements/data-manipulation/selecting-data/select) statements to be executed during [INSERT](https://gitbook-v2-q3lwp8faq-gitbook.vercel.app/url/mariadb.com/docs/server/reference/sql-statements/data-manipulation/inserting-loading-data/insert) operations, reducing contention.

Whether concurrent inserts can be used or not depends on the value of the [concurrent\_insert](https://gitbook-v2-q3lwp8faq-gitbook.vercel.app/url/mariadb.com/docs/server/server-management/variables-and-modes/server-system-variables#concurrent_insert) server system variable:

* `NEVER` (0) disables concurrent inserts.
* `AUTO` (1) allows concurrent inserts only when the target table has no free blocks (no data in the middle of the table has been deleted after the last [OPTIMIZE TABLE](https://gitbook-v2-q3lwp8faq-gitbook.vercel.app/url/mariadb.com/docs/server/ha-and-performance/optimization-and-tuning/optimizing-tables/optimize-table)). This is the default.
* `ALWAYS` (2) always enables concurrent inserts, in which case new rows are added at the end of a table if the table is being used by another thread.

If the [binary log](https://gitbook-v2-q3lwp8faq-gitbook.vercel.app/url/mariadb.com/docs/server/server-management/server-monitoring-logs/binary-log) is used, [CREATE TABLE ... SELECT](https://gitbook-v2-q3lwp8faq-gitbook.vercel.app/url/mariadb.com/docs/server/reference/data-definition/create/create-table#create-table-select) and [INSERT ... SELECT](https://gitbook-v2-q3lwp8faq-gitbook.vercel.app/url/mariadb.com/docs/server/reference/sql-statements/data-manipulation/inserting-loading-data/insert-select) statements cannot use concurrent inserts. These statements acquire a read lock on the table, so concurrent inserts will need to wait. This way the log can be safely used to restore data.

Concurrent inserts are not used by replicas with the row based [replication](https://github.com/mariadb-corporation/docs-server/blob/test/server/reference/sql-statements/data-manipulation/inserting-loading-data/broken-reference/README.md) (see [binary log formats](https://gitbook-v2-q3lwp8faq-gitbook.vercel.app/url/mariadb.com/docs/server/server-management/server-monitoring-logs/binary-log/binary-log-formats)).

If an [INSERT](https://gitbook-v2-q3lwp8faq-gitbook.vercel.app/url/mariadb.com/docs/server/reference/sql-statements/data-manipulation/inserting-loading-data/insert) statement contain the [HIGH\_PRIORITY](https://gitbook-v2-q3lwp8faq-gitbook.vercel.app/url/mariadb.com/docs/server/reference/sql-statements/data-manipulation/changing-deleting-data/high_priority-and-low_priority) clause, concurrent inserts cannot be used. [INSERT ... DELAYED](https://gitbook-v2-q3lwp8faq-gitbook.vercel.app/url/mariadb.com/docs/server/reference/sql-statements/data-manipulation/inserting-loading-data/insert-delayed) is usually unneeded if concurrent inserts are enabled.

[LOAD DATA INFILE](https://gitbook-v2-q3lwp8faq-gitbook.vercel.app/url/mariadb.com/docs/server/reference/sql-statements/data-manipulation/inserting-loading-data/load-data-into-tables-or-index/load-data-infile) uses concurrent inserts if the `CONCURRENT` keyword is specified and [concurrent\_insert](https://gitbook-v2-q3lwp8faq-gitbook.vercel.app/url/mariadb.com/docs/server/server-management/variables-and-modes/server-system-variables#concurrent_insert) is not `NEVER`. This makes the statement slower (even if no other sessions access the table) but reduces contention.

[LOCK TABLES](https://gitbook-v2-q3lwp8faq-gitbook.vercel.app/url/mariadb.com/docs/server/reference/sql-statements/transactions/lock-tables) allows non-conflicting concurrent inserts if a `READ LOCAL` lock is used. Concurrent inserts are not allowed if the `LOCAL` keyword is omitted.

## Notes

The decision to enable concurrent insert for a table is done when the table is opened. If you change the value of [concurrent\_insert](https://gitbook-v2-q3lwp8faq-gitbook.vercel.app/url/mariadb.com/docs/server/server-management/variables-and-modes/server-system-variables#concurrent_insert) it will only affect new opened tables. If you want it to work for also for tables in use or cached, you should do [FLUSH TABLES](https://gitbook-v2-q3lwp8faq-gitbook.vercel.app/url/mariadb.com/docs/server/reference/sql-statements/administrative-sql-statements/flush-commands/flush) after setting the variable.

## See Also

* [INSERT](https://gitbook-v2-q3lwp8faq-gitbook.vercel.app/url/mariadb.com/docs/server/reference/sql-statements/data-manipulation/inserting-loading-data/insert)
* [INSERT DELAYED](https://gitbook-v2-q3lwp8faq-gitbook.vercel.app/url/mariadb.com/docs/server/reference/sql-statements/data-manipulation/inserting-loading-data/insert-delayed)
* [INSERT SELECT](https://gitbook-v2-q3lwp8faq-gitbook.vercel.app/url/mariadb.com/docs/server/reference/sql-statements/data-manipulation/inserting-loading-data/insert-select)
* [HIGH\_PRIORITY and LOW\_PRIORITY](https://gitbook-v2-q3lwp8faq-gitbook.vercel.app/url/mariadb.com/docs/server/reference/sql-statements/data-manipulation/changing-deleting-data/high_priority-and-low_priority)
* [INSERT - Default & Duplicate Values](https://gitbook-v2-q3lwp8faq-gitbook.vercel.app/url/mariadb.com/docs/server/reference/sql-statements/data-manipulation/inserting-loading-data/insert-default-duplicate-values)
* [INSERT IGNORE](https://gitbook-v2-q3lwp8faq-gitbook.vercel.app/url/mariadb.com/docs/server/reference/sql-statements/data-manipulation/inserting-loading-data/insert-ignore)
* [INSERT ON DUPLICATE KEY UPDATE](https://gitbook-v2-q3lwp8faq-gitbook.vercel.app/url/mariadb.com/docs/server/reference/sql-statements/data-manipulation/inserting-loading-data/insert-on-duplicate-key-update)

<sub>*This page is licensed: CC BY-SA / Gnu FDL*</sub>

{% @marketo/form formId="4316" %}
