[Interview question] MySQL lock mechanism

MySQL lock mechanism

Insert picture description here

Usually have

  • Row locks
    Row-level locks are the finest locking granularity in Mysql, which means that only the rows of the current operation are locked. Row-level locks can greatly reduce conflicts in database operations. The locking granularity is the smallest, but the locking overhead is also the largest. There may be a deadlock situation.

  • Table locks
    Table-level locks are the largest type of lock in mysql locks, which means that the current operation locks the entire table, and the resource overhead is less than row locks, and there will be no deadlocks, but the probability of lock conflicts is high. .

  • Page locks
    Page-level locks are a type of lock that has a locking granularity between row-level locks and table-level locks in MySQL.




Common execution engines support these two locks

Insert picture description here




Method to realize

InnoDB row locks are 通过给索引上的索引项加锁implemented. MySQL is different from Oracle, which is implemented by locking the corresponding data rows in the data block.

InnoDB's row lock implementation features mean:只有通过索引条件检索数据,InnoDB才使用行级锁,否则,InnoDB将使用表锁!




According to the way of use, row locks and table locks can be divided into

  • Shared lock (read lock): Allows a transaction to read a row, preventing other transactions from obtaining an exclusive lock on the same data set.

  • Exclusive lock (write lock): Allows the transaction that obtains the exclusive lock to update data, and prevents other transactions from obtaining the shared read lock and exclusive write lock of the same data set.

Guess you like

Origin blog.csdn.net/qq_42380734/article/details/108713516