Basic introduction of lock Mysql

  Database locking mechanism is simple, it is the database to ensure data consistency, the various shared resources in a regular accessed concurrently become the order of the design. For any kind of database it needs to have the appropriate locking mechanism, so naturally MySQL is no exception. Due to its own MySQL database architecture, there are a variety of data storage engine, the storage engine characteristics of each application scenarios for which are not the same, in order to meet the needs of their specific application scenarios, each storage engine locking mechanism are for each face a particular scene and optimize the design, so the locking mechanism of each storage engine is also quite different. Each MySQL storage engine uses three types (levels) of the locking mechanism: table-level locking, row-level locking and page-level locking.

Lock the classification
table lock

  Table-level locking is MySQL storage engine in each maximum granularity of locking mechanism. The greatest feature of the locking mechanism is to implement logic is very simple, minimal negative impact on the system brings. So acquiring the lock and release the lock quickly. Due to a table-level lock will lock the entire table, it can be good to avoid deadlock plaguing us.

  Advantages: small overhead, lock fast; not deadlock  

  Disadvantages: large-grained locking, the probability of lock conflicts of the highest and lowest degree of concurrency

Page locks

  Page-level locking in MySQL is a relatively unique locking levels, in other database management software also is not too common. Page-level locking granularity locking features between the row level locking table level lock, it is required to acquire the lock overhead resources, and can provide concurrent processing is also interposed between the two above. In addition, the page-level locking and row-level locking, as a deadlock occurs.

  Advantages: the cost and the time boundaries between the locking table and row locks; lock granularity boundary between the table and row locks, general concurrency

  Disadvantages: there will be a deadlock

Row lock

  Row-level locking biggest feature is to lock the object is very small particles, is currently locked in major database management software to achieve the minimum granularity. Since the locking granularity is small, so the probability of occurrence locked resource contention is minimal, the application can give the greatest possible number of concurrent processing capability and improve concurrent applications that require high overall system performance.

  Advantages: Lock the smallest size and lowest probability of lock conflicts, but also the highest degree of concurrency

  Disadvantages: large overhead, locking slow; produce deadlock

MyISAM

  myisam is a type of non-transactional table locks only supports storage engine. Locking mechanism used entirely provided by MySQL table-level locking implementation.

  MySQL table-level locking has two modes: a shared table read locks (Table Read Lock) and exclusive write locks table (Table Write Lock). Compatibility lock modes:
  1. read MyISAM tables, other users will not block read requests for the same table, but will block the write requests to the same table;
  2. MyISAM tables write operation, the other will be blocked users read and write operations of the same table;

  MyISAM locking manner:

  MyISAM before executing the query (SELECT), will be automatically added to all the tables involved in read lock.

  Before the update operation (UPDATE, DELETE, INSERT, etc.), is automatically added to the table relates to the write lock, the process does not require user intervention, and therefore, users generally do not need to directly command LOCK TABLE MyISAM explicit lock table .

 

Guess you like

Origin www.cnblogs.com/htyj/p/11772380.html