[] MySQL database system, engine, lock mechanism

A, MySQL logical architecture

1, the overall stratification

  • The connection layer
    • Establish a service connection with the client
    • Complete some connection processing, authorization and authentication and related security solutions
    • It introduces the concept of a connection pool on the layer
  • Service Layer
    • Providing core services
    • Perform the copy sql interfaces, complete cached query, analysis and optimization part of sql and built-in functions
    • All across storage engine functions are realized in this layer
    • Resolve queries and create the corresponding analytical power, and complete optimization, generate the appropriate action
    • The rational allocation of internal cache, optimization problem to solve a large number of read operations
  • Engine Layer
    • MYSQL really responsible for storage and retrieval of data through the storage engine API to communicate
    • Different different storage engine features
  • Storage layer
    • The major operational data file stored in a computer file system, completed in interactive engine

2, the logical architecture of FIG.

Here Insert Picture Description

3, logical hierarchical architecture

Here Insert Picture Description

4 benefits, hierarchical logic

  • 1. clear distribution of functions, where the problem is where to find
  • 2. The plug-in storage engine, the query processing system tasks and other tasks have been separated from the extract phase data
  • 3. You can choose the right storage engine as needed demands and business services

Second, the storage engine

1, the concept storage engine

  • MySQL data in a variety of different technologies stored in the file (or memory) in the.
  • Each of these technologies technology uses a different storage mechanism, indexing techniques, lock level and ultimately provide a wide range of different functions and capabilities.
  • By choosing different techniques, you can get extra speed or functionality, thereby improving the overall functionality of your application.
  • These different technologies and supporting related functions were called in the MySQL storage engine

View the current engine used

show engines;       #查看当前版本中默认支持的数据库引擎

show variables like '%storage_engine';     #查看当前数据库使用的引擎
  1. InnoDB

    The preferred transactional database engine, support for transaction-safe tables (ACID), supports row locking and foreign keys, InnoDB is the default MySQL engine.

    InnoDB main features are:

    1. InnoDB provides MySQL with a with a commit, rollback, crash recovery capabilities of transaction-safe storage engine.
    2. InnoDB is designed to handle huge amounts of data designed for maximum performance. Its CPU efficiency is higher than other disk-based relational database engine.
    3. InnoDB storage engine own buffer pool, and the index data may be cached in memory.
    4. InnoDB supports foreign key integrity constraints.
    5. InnoDB is used on numerous large database sites requiring high performance
    6. InnoDB supports row-level locking
  2. MyISAM

    Based on ISAM MyISAM storage engine, and its expansion. It is one of the most commonly used storage engine in the Web, data warehousing and other application environments. MyISAM has a high insert, query speed, but does not support things.

    MyISAM main features are:

    1. Large file support better
    2. When you delete, update, insert, mix, produce less debris.
    3. Each MyISAM table index is the maximum number of 64, which can be changed by recompiling. The maximum number of columns per index is 16
    4. The maximum key length is 1000 bytes.
    5. BLOB and TEXT columns can be indexed
    6. NULL is allowed in the column of the index, representing the value 0 or 1 byte for each key
    7. All numeric key values ​​are stored in the high byte first to allow a higher index of compression
    8. AUTO_INCREMENT MyISAM type table column update faster than InnoDB type AUTO_INCREMENT
    9. You can put data files and index files in different directories
    10. Each character column can have a different character sets
    11. There VARCHAR table length may be fixed or dynamic record
    12. VARCHAR and CHAR columns can be up to 64KB
    13. Only supports table locks
    14. Support FULLTEXT KEY (full-text index)
  3. MEMORY

    MEMORY storage engine to store data in the table into memory, providing quick access to data queries and references to other tables.

  4. BLACKHOLE

    A black hole as the engine is typically pseudo-host, used to relieve the pressure of a master using the master from the total intermediate machine architecture,

    Produced only log data is not saved.

2, the storage engine of choice

In general, the insertion and concurrent high performance requirements, or need the foreign key, or where necessary transaction support is necessary to select the InnoDB,

Insert less, query more scenes priority MyISAM.

3, designated engine

General added during the construction of the table

create table abc (
    name char(10)
) engine=MyISAM charset=utf8;

create table xyz (
    name char(10)
) engine=InnoDB charset=utf8;

4, InnoDB and MyISAM difference in terms of file

  1. InnoDB will be stored as a table two documents

    • Structure and index> storage table - demo.frm
    • demo.ibd -> store data, ibd storage is limited, inadequate storage automatically create ibd1, ibd2
  2. The MyISAM table is stored as a file three

    • Structure> storage table - demo.frm
    • demo.myd -> Data storage
    • Index> Storage table - demo.myi

Here Insert Picture Description

Third, the lock mechanism

Computer lock mechanism to coordinate multiple processes or threads concurrent access to a resource.

Concurrent access lock to ensure data consistency and effectiveness;

Lock conflicts is an important factor affecting the performance of concurrent access to the database.

Mysql lock is concurrent server storage layer and a control layer engine.

Noun classification

  • Row-level locking

    • A lock is a row level locking Mysql finest granularity locking, the lock only for the representation of the lines of the current operation.
    • Only row-level locking InnoDB engine support.
    • Row-level locking conflict can greatly reduce database operations. The minimum size of its lock, but the lock of the largest overhead.
    • Features: a large overhead, locking slow; there will be a deadlock; locking granularity smallest and lowest probability of lock conflicts, have the highest degree of concurrency.
  • Table-level locking

    • MySQL table-level lock is locked in the largest size of a lock
    • To lock an entire table of the current operation, it is simple, low resource consumption, supported by most of the MySQL engine.
    • Features: Small overhead, lock fast; not deadlock; lock large size, the probability of lock conflicts issue of the highest and lowest degree of concurrency.
  • Page lock

    • Overhead and speed lock between the table and row locks; deadlock occurs; particle size between locking table and row locks, general concurrency
    • Page locks have since been replaced by BDB InnoDB, we only discuss the issue InnoDB MyISAM table locks and row locks in the BDB engine
  • Shared lock (read lock)

    • Other users can concurrently read the data, but no transaction can modify the data until all shared locks have been released.
  • Exclusive lock (write lock)

    • If the transaction data T to A plus exclusive lock, other transactions can not be A plus for any type of blockade.
    • He holds an exclusive lock transaction not only read data, but also to modify the data.
  • Optimistic locking (Optimistic Lock)

    Assuming that does not happen concurrency violation is a violation of data integrity checks only when the commit operation. Optimistic locking does not solve the problem of dirty read.

    Optimistic locking, by definition, is very optimistic, pick up data every time that others are not modified, so it will not be locked, but when the update will determine what others during this time did not go to update the data, you can use mechanism version number. Optimistic locking is suitable for the types of applications to read, so you can improve throughput if the database provided similar write_condition optimistic locking mechanisms are actually provided like.

  • Pessimistic locking (Pessimistic Lock)

    Assume will happen concurrent conflict, shielding everything possible violations of operational data integrity.

    Pessimistic locking, by definition, is very pessimistic, pick up data every time the thought that others will modify, so every time she took the data will be locked, so people want to take this data will block until it got the lock. Traditional relational database inside to use a lot of this locking mechanism, such as row locks, table locks, etc., read lock, write lock, are locked before doing the first operation.

Here Insert Picture Description

Table lock operation

Biased in favor of myisam storage engine (also innodb in), small overhead, locked fast, no deadlock, locking large particle size, the highest probability of lock conflicts, concurrent support for the worst.

Features: a person can only read a table, others line up.

Examples

#查看表中有没有被锁
show open tables;

#加上读写锁
lock table stuinfo read; 
lock table stuinfo write;

#解除锁
unlock tables;

Add a read lock (shared lock), when it is time to open the lock state

  • Among the current terminal is connected
    • You can use the query to query the current table, but can not perform other operations
    • Can not operate on other tables
  • In other operating terminal
    • It can be used to query the current query tables, you can use other operations, but will be closed after the lock has been blocked to the implementation of
    • Can not operate on other tables

Adding write lock (exclusive lock), when it is time to open the lock state

  • When the current terminal link
    • You can query and manipulate the currently active table is locked
    • It can not be locked from the active query (automatic lock) other tables
  • In other operating terminal
    • You can use the query to query the current table, but will be closed after the lock has been blocked to the implementation of
    • You can use other type of action-type operations, but will be closed after the lock has been blocked to the implementation of

Row lock

Bias InnoDB storage engine, large overhead, locking slow, there will be a deadlock; locking the smallest size, the probability of lock conflicts of the bottom, have the highest degree of concurrency.

InnoDB and MyISAM biggest difference: First, support services, and second, using a row-level locking.

Concurrent transactions in specific emerging issues

  • 1. Update loss problem

Two or more transactions select the same row, and then selected based on the initial value to update the row, because each transaction is not aware of the existence of other transactions, lost update problem occurs, the last update covering the other firms do updates.

  • 2. dirty read
  • 3. Non-repeatable read
  • 4. Magic Reading

View isolation level

  • Read uncommitted (read uncommitted), another is a transaction can read uncommitted transaction data
  • Read committed (read committed, non-repeatable read), a transaction have to wait to read the data after the transaction commits another
  • Repeatable read (repeatable, high efficiency, has various problems can be prevented, and the default level), that is, at the start of the read data (open transaction), no longer allowed to modify operation.
  • The Serializable (serialized, inefficient, very effective, is not the default level), at this level, the implementation of the transaction serialization order to avoid dirty reads, non-repeatable read and phantom read.
select @@global.tx_isolation,@@tx_isolation;

Setting the isolation level

We need to be submitted at the time of the automatic test transaction to close, the default is automatically submitted at a later mysql5.5 affairs

show variables like 'autocommit';    #查看事务的开启状态

set autocommit = 0    #0是关闭,1是开启
#全局设置
set global transaction isolation level Repeatable read; 

#当前会话终端设置
set session transaction isolation level Repeatable read;

And using a gap of row locks lock

Published 116 original articles · won praise 10 · views 1365

Guess you like

Origin blog.csdn.net/weixin_44727383/article/details/104954622