MySql InnoDB engine and lock

MySql InnoDB engine and lock

mysql locks are very high-frequency interview questions, which we often encounter but we did not notice where in your daily development. I put my own understanding of the locks share it through this post, because the lock requires a combination of transactions to understand, this article only describes the basic concepts of the lock, the same in order to understand the transaction will be more profound, introduce some basic concepts of InnoDB, but also to record their learning, welcome to discuss the exchange.

Next: Transaction and the mvcc mysql

Lock Category:

  • Lock granularity according to points

    • Global Lock: lock the entire database, a library is a building analogy, that is, lock the door this time the whole building
    • Table lock: locks of a specific Table, a table analogy for a floor in a building, in which case the lock whole layer is a layer
    • Row-level locks: the lock is a data line, each row analogy lock is a layer of a room, this time a lock is a room
  • The difference between the table and row locks
    • Table-level locking, spending big, fast locking, deadlock will not occur. Lock granularity large. Low degree of concurrency.
    • Row-level locking, a small overhead, locking slow, there will be a deadlock, a small lock granularity, high concurrency.

Table-level locking:

mysql table lock, there are two: the metadata and table locks.

Two forms of table locks:

  • Table shared read lock

  • Table exclusive write locks

    • Manually add table lock

      lock table tableName read;
    • View table lock case

      show open tables;
    • Delete table lock

      unlock tables;

Metadata lock:

  • Version 5.5 introduced the MDL, a table of data additions and deletions to time, plus MDL read lock; time to make changes to the table structure, plus MDL write lock.

Row-level locking

mysql row-level locking storage engine is implemented, mysql now the default data engine for the Innodb. This paper describes the InnoDB row lock;

InnoDB row lock is to achieve index entry lock, which means that only use the index to retrieve the data row locks, or they will use table locks.

In accordance with the terms range

  • Row lock: Lock one of the logs table.
  • Lock the gap:
    • Lock the index record intermediate values
    • A first lock value or index record in front of the last value of the index after

According to Functionally

  • Shared locks, also known as S-lock: allows a transaction to read a line of data, preventing other transactions add exclusive lock, shared lock is allowed to continue to add read
  • Exclusive lock, also known as X locks: allow the transaction to acquire an exclusive lock to update data, preventing other transactions add read shared lock and an exclusive lock to write add

For InnoDB, the statement will be automatically added to the additions and deletions to exclusive locks, X lock. For ordinary query does not add any locks.

Intent locks

InnoDB also realized the table-level locking, which is intent lock. Intent locks mysql internal use and does not require the user to intervene.

  • Intent shared locks, lock IS: Rights intends to add to the data line shared lock, a transaction must acquire IS lock the table before you add a shared lock to a data line.
  • Intent exclusive lock IX lock: Transaction data is going to give Air Canada an exclusive lock, the transaction data in a row to add exclusive lock must be acquired before the IX lock on the table.

Intent row locks and can co-exist, the role is to lock intent to enhance performance when a full table update data, or when updating the whole table to retrieve data which have row lock on the row.

Lock gap

As the name suggests, the main lock is added between the recording, the gap is not allowed to insert data. For example, id is 24, this time using the gap that would lock 234 lock these three, later also introduce a major role gap locks, lock the gap again in the introduction, when the transaction is to solve the problem of phantom read. Here to find out.

Deadlock

mysql deadlock and deadlock in our code theory is the same, the difference is, mysql refers to two different connections waiting for each other to release the lock to release the resources they hold, so the resulting deadlock. mysql also has deadlock optimization right. We then specifically later.

Then we began to introduce the transaction, the above is just a brief introduction to explain the basic concepts of the lock, combination lock as well as part of the transaction needs to understand, so there is a lock later.

Before we introduce the transaction, let's talk about InnoDB architecture, some parts of the transaction will involve the content of this section.

InnoDB disk file

InnoDB disk file

  • Table space
    • System table space is a shared table space
    • The system comprises a data dictionary table space, doule write buffer, change buffer, undo log storage area comprising user data and index table structure created in the system table
  • User table space
    • Set parameters innodb_file_per_table, for each user can create a separate user table space based on the table InnoDB engine, which is .ibd file.
    • Storing the table data, the index information.

InnoDB memory structure

  • buff pool pool
    • Data is stored on the disk, and the disk due to the difference speed cpu speed, the use of the buffer pool to improve overall performance.
    • May be provided by innodb_buffer_pool_size buffer pool size, the size of the buffer pool on the performance is going to affect.
    • Data buffer pool type:
      • Index Page
      • Data page
      • When the storage engine to work, you need to load a page unit data disk into memory, data pages and index pages are the most important type of page two types
      • undo page: mysql to achieve a multi-version snapshot can be understood as the version chain. mvcc and rollback operations are related to the undo log.
      • insert buffer: improved performance for the insertion of a non-clustered index
      • Adaptive hash indexes
      • InnoDB storage lock information
      • Data dictionary information

Memory data off the disk

FIG InnoDB data off the disc can be seen there are two ways to achieve

  • Dirty page data off the disk
  • Pre-written redo log

There are two ways to place an order, can be understood as persistence to disk. Is to ensure that the number of library sudden downtime, data loss.

Dirty pages off the disk IO will produce and is random write, time-consuming long. Frequent disk IO performance loss is very large. And data security can not be guaranteed. If the dirty page data is not enough time to drop off the plate or tray process downtime, then the data will be lost.

In view of the above, mysql complete with double safety data security, dirty pages to disk is a drop, while another is pre-written redo log, first of all we need to know redo persisted to disk is write sequence, the write speed of the order faster than random writes, when a friend will ask, dirty pages off the disk why not use it but written order?

Write speed of the order at the same time will produce disk fragmentation, disk fragmentation will greatly wasted disk resources.

redo log persistence time is written to disk when the transaction commits the redo file, in which case the dirty page data is not necessarily finished off the dish, place orders dirty pages are controlled by checkPoint checkpoint mechanism, we are here not to proceed to say.

The occurrence of database downtime:

  • Dirty page data hardly ever drive, uncommitted transactions. This produces a data loss, we all know that if the transaction is not committed another point of view these data loss is normal.
  • Dirty page data hardly ever drive, the transaction has been submitted, then redo log file already has data, it will restart when mysql data recovery from a redo log file in.

Some friends will say, that redo log file data would not be infinite?

ib_logfile0, ib_logfile1 which is named rodo log on our disk, you can see there are two files, using a circular manner of writing, if 1 is written over the full write 2,2 1, so the cycle.

redo log persisted to disk is configurable, set by the innodb_push_log_at_trx_commit InnoDB

  1. When the attribute value of 0, the transaction commits, not to redo a write operation, the main thread wait for the write time;
  2. When the property value 1, the transaction is committed, the data is written to disk, to ensure that no data loss;
  3. 2 When the property value, the transaction is committed, the data is written to the system cache, allowing the file system to judge for themselves when written to disk.

The default value is 1, the general recommendation is set to 1, it will ensure the security of data, and only when 1 will ensure transactional consistency is.


That's all for this post, and thank you Tell me. Welcome to ask questions to explore the exchange.

Next: mysql transaction and mvcc

Guess you like

Origin www.cnblogs.com/sx-wuyj/p/12540537.html