Document directory
- 7.3 lock
- 7.3.1 Lock Mechanism
- 7.3.2 lock table
7.3 lock 7.3.1 Lock Mechanism
MySQL currently supportsISAM
,MyISAM
,MEMORY
(HEAP
) Table-level locks for Type tables,BDB
Table supports page-level locks,InnoDB
Table supports row-level locks.
Most of the time, we can use experience to guess what kind of lock is more suitable for the application, but it is often difficult to say that a lock is better than others, it is determined by the application, different locks may be required in different places.
To determine whether to use a row-Level Lock storage engine, you need to check what the application is doing and how the query and update statements are used. For example, many web applications perform a large number of queries and seldom delete them. They only insert records into specific tables based on index updates. Use Basic MySQLMyISAM
The table is suitable.
The storage engine for table-level locks in MySQL releases deadlocks. To avoid deadlock, you can request a lock before any query and lock the table in the order of the request.
Used in MySQLWRITE)
The implementation mechanism of the table lock is as follows:
- If the table is not locked, add a write lock.
- Otherwise, put the request in the write lock queue.
Used in MySQLREAD)
The implementation mechanism of the table lock is as follows:
- If no write lock is applied to the table, a read lock is applied.
- Otherwise, put the request in the read lock queue.
After the lock is released, the thread in the write lock queue can use this lock resource before it is the turn to read the thread in the lock queue.
This means that if there are many update operations in the tableSelect
You must wait until all updates are complete before starting.
Starting from MySQL 3.23.33, you can use status variablesTable_locks_waited
AndTable_locks_immediate
To analyze the lock table contention in the system:
mysql> SHOW STATUS LIKE 'Table%';+-----------------------+---------+| Variable_name | Value |+-----------------------+---------+| Table_locks_immediate | 1151552 || Table_locks_waited | 15324 |+-----------------------+---------+
After MySQL 3.23.7 (3.23.25 on Windows ),MyISAM
As long as there is no conflict in the tableInsert
The lock table can be freely executed in parallel without having to use the lock table.Insert
AndSelect
Statement. That is, it can be read from other clients.MyISAM
Insert a new record at the same time as the table record. If there are no spare disk blocks in the middle of the data file, there will be no conflict, in this case, all new records are written at the end of the data file (empty may occur when the table is deleted or updated ). When the empty space is filled with new data, the parallel insertion feature is automatically re-enabled.
If you want to do a lotInsert
AndSelect
Operation, but parallel insertion is not possible, you can insert records into the temporary table, and then regularly update the data in the temporary table to the actual table. You can use the following command:
mysql> LOCK TABLES real_table WRITE, insert_table WRITE;mysql> Insert INTO real_table Select * FROM insert_table;mysql> TRUNCATE TABLE insert_table;mysql> UNLOCK TABLES;
InnoDB
Use row-level locks,BDB
Use a page-Level Lock. ForInnoDB
AndBDB
The storage engine may cause deadlocks. This is becauseInnoDB
The row lock is automatically captured,BDB
The page lock will be captured when the SQL statement is executed, rather than at the beginning of the transaction.
Row-level locks have the following advantages:
- Reduce the conflict lock when multiple threads request different records.
- Reduce data changes during transaction rollback.
- It is possible to lock a single row of records for a long time.
Row-level locks have the following Disadvantages:
- It consumes more memory than page-level locks and table-level locks.
- When used in a large number of tables, it is slower than page-level locks and table-level locks because they need to request more resources.
- When most data needs to be frequently processed
GROUP BY
If you want to scan the entire table frequently, it is obviously worse than other locks.
- Using Higher-Level locks makes it easier to support different types of applications, because the cost of such locks is much lower than that of Row-level locks.
Table-level locks are superior to page-level locks and row-level locks in the following situations:
Table-level locks differ from row-level locks or page-level locks in the following ways:
Make the version of one write and multiple reads (for example, concurrent inserts in MySQL ). That is to say, databases/Tables support different attempts based on the time points at which data is accessed. Other names include time travel, write replication, or on-demand replication.
Original article: Versioning (such as we use in MySQL for concurrent inserts) where you can have one writer at the same time as your readers. this means that the database/table supports different views for the data depending on when you started to access it. other names for this are time travel, copy on write, or copy on demand.
On-demand replication is much better than page-level or row-level locks in many cases. However, in the worst case, more memory is used than other normal locks.
Application-level locks can be used to replace row-level locks, for exampleGET_LOCK()
AndRELEASE_LOCK()
. However, they are suggested locks (Original: These are advisory locks), so they can only be used in secure and trusted applications.
7.3.2 lock table
In order to have a quick lock, apart from InnoDB
AndBDB
Both storage engines use table-level locks instead of page-level, row-level, and column-level locks ).
ForInnoDB
AndBDB
Table. MySQL is only usedLOCK TABLES
The table-Level Lock is used only when the table is locked. We recommend that you do not use these two types of tables.LOCK TABLES
BecauseInnoDB
Automatic adoption of Row-level locks,BDB
Use Page-level locks to ensure transaction isolation.
If the data table is large, table-level locks are much better than Row-level locks in most applications, but there are some traps.
Table-level locks allow many threads to read data from the data table at the same time. However, if another thread wants to write data, it must first obtain exclusive access. When updating data, you must wait until the update is complete before other threads can access the table.
Update operations are generally considered to be more important than reading, so they have a higher priority. However, it is best to check whether the data table has a high value first.Select
But the update operation is not very 'url '.
The table lock is waiting on a thread because the disk space is full, but the remaining disk space is required for the thread to continue processing. In this case, all the threads that want to access the problematic table will be set to the waiting state until there is any available disk space.
Table locks are unfavorable in the following scenarios:
- A client submits
Select
Operation.
- Other clients have submitted
Update
Operation, the client will waitSelect
The execution can be started only after the execution is completed.
- Other clients have submitted
Select
Request. BecauseUpdate
The priority is higherSelect
, SoSelect
Will wait Update
After the task is completed, the task starts to run. It is also waiting for the first task.Select
Operation.
The following describes how to reduce resource contention caused by table locks:
- Let
Select
The speed should be as fast as possible, which may require the creation of some summary tables.
- Start
mysqld
Use Parameters--low-priority-updates
. This will give the update operation a lower prioritySelect
. In this case, in the preceding assumption, the secondSelect
InInsert
Previously executed, and there is no need to wait for the firstSelect
.
- Executable
SET LOW_PRIORITY_UpdateS=1
Command to specify that all update operations are put in a specified link. For details, see "14.5.3.1SET
Syntax ".
- Use
LOW_PRIORITY
Attribute to reduceInsert
,Update
,Delete
.
- Use
HIGH_PRIORITY
To improveSelect
Statement priority. For details, see "14.1.7Select
Syntax ".
- Starting from MySQL 3.23.7, you can start
mysqld
Specify System Variablesmax_write_lock_count
It is a relatively low value, which can force temporary increase of the number of table inserts to allSelect
Operation priority. It allowsWRITE
After the lock reaches a certain numberREAD
Lock.
- When
Insert
AndSelect
You can useMyISAM
Table, which supports concurrentSelect
And Insert
Operation.
- When both insert and delete operations are performed on the same table,
Insert DELAYED
It may be useful. For details, see "14.1.4.2Insert DELAYED
Syntax ".
- When
Select
AndDelete
When a problem occurs during use together,Delete
OfLIMIT
Parameters may be useful. For details, see "14.1.1Delete
Syntax"
- Run
Select
UseSQL_BUFFER_RESULT
Helps reduce the duration of the lock table. For details, see "14.1.7Select
Syntax ".
- Source code can be modified'Mysys/thr_lock.c'Only one queue is used. In this case, the write lock and read lock have the same priority, which may be helpful to some applications.
The following are some suggestions for MySQL locks:
- As long as a large number of update and query operations are not mixed in the same table, the current user is not a problem.
- Run
LOCK TABLES
To improve the speed (a lot of update operations are put in a lock much faster than a lot of updates without a lock ). Splitting data into multiple tables may also be helpful.
- When MySQL encounters a speed problem caused by the lock table, it converts the table type
InnoDB
OrBDB
May help improve performance. For more information, see "16InnoDB
Storage Engine "and" 15.4BDB
(BerkeleyDB
) Storage Engine ".