简体   繁体   中英

MySQL MyISAM - Do inserts without locking

How do I do ALWAYS do inserts into MySQL MyISAM database without locking the table?

When I load a 100,000 records my other processes appear to be locked.

Basically, can I force the use of concurrent_inserts=2 even if there are holes in the table so there is NO table locking during inserts?

Can concurrent_inserts be set for the session dynamically? see http://dev.mysql.com/doc/refman/5.0/en/concurrent-inserts.html

What other methods are there for non-locking inserts?
Will "insert delayed" prevent table locking during inserts? see http://dev.mysql.com/doc/refman/5.5/en/insert-delayed.html

If you perform your inserts in small batches, using INSERT DELAYED will let the clients selecting from the table basically get first priority so they don't block. If you insert thousands of rows in one shot you will potentially lock the table for a while. Another thing that can help speed the inserts is to temporarily tell the DB to skip unique checks. That will prevent the constant analysis of any unique keys, but won't prevent locking of the table. You must be certain that what you insert is unique.

When doing many inserts, InnoDB is often a better choice because it will lock the row and not the table. Your inserts will take longer but your clients will see better concurrent performance.

I have had the same issue. I tried to insert 2,500,000 records into a MyISAM table. It is an independent table, so the lock doesn't worried me. But the insert process just frozen the whole database server.

So what I did:

  • remove all the INDEX-es (add these at the end of the process)
  • do 2500 rows per insert
  • use INSERT DELAYED INTO

That's it.

IIRC MyISAM locks tables whilst any insert happens .. Just re-read and confirmed that :

To achieve a very high lock speed, MySQL uses table locking (instead of page, row, or column locking) for all storage engines except InnoDB, BDB, and NDBCLUSTER

Taken from : http://dev.mysql.com/doc/refman/5.0/en/table-locking.html

If you do know of any non-locking MyISAM inserts, please let me know - as it's a problem I face daily (I don't like InnoDB space consumption and speed)

MySQL does support parallel insertion in same table for MyISAM tables.

MyISAM

MySQL uses table-level locking for MyISAM, MEMORY, and MERGE tables, allowing only one session to update those tables at a time, making them more suitable for read-only, read-mostly, or single-user applications.

But, the above mentioned behavior of MyISAM tables can be altered by concurrent_insert system variable in order to achieve concurrent write. You can set value for the property as 1 or 2 , depending upon your requirement. Refer link for details.

Hence, as a matter of fact, MySQL does support concurrent insert for InnoDB and MyISAM storage engine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM