Chapter 1 MySQL architecture and history

Storage / MySQL extract the most important and distinctive feature is its storage engine architecture that is designed to query processing (Query Processing) and other system tasks (Server Task) and data phase separation. This processing and storage can be designed to separate in use to select the data stored in accordance with the performance, characteristics, and other needs manner

1.1 MySQL Logical Architecture

MySQL server logic chart

  • Most systems have the top service, the connection processing, authorization and authentication, security, etc.
  • The second layer contains most of the core functionality of MySQL
  • The third layer contains a storage engine. The storage engines to store and retrieve data in MySQL
1.1.1 Connection Management and Security

Each client will have a connecting thread in the server process, this connection will only execute a query in this separate thread, the thread can only take turns running in a CPU core or CPU. The server is responsible for caching thread, there is no need for each new connection to create or destroy threads.
When a client connects to a MySQL server, the server need to be authenticated. Authentication based on user name, password information, and the original host. If theSecure SocketsSSL) is connected, it may also be used X.059 certificate authentication. Once the client connection is successful, the server will continue to verify that the client has permission to perform a particular query (for example, whether to allow a client to execute a SELECT statement against the database world Country table).

1.1.2 Optimization and Execution

MySQL will parse the query and create an internal data structure, and then subjected to various optimizations, including rewrite the query to determine the reading order of the table, and select the appropriate indexes.
For SELECT statements, prior to resolve the query, the server will check the query cache

1.2 Concurrency Control

1.2.1 Read-Write Lock

When concurrent read and write process, the problem can be solved by implementing a lock system consists of two types of locks thereof. These two types of locks are usually called shared lock (shared lock) and exclusive lock (exclusive lock), also called a read lock (read lock) and a write lock (write lock).
Read locks are shared, or is not blocking each other. Multiple clients read from the same resource at the same time, without any interference.Write lock is exclusive, which means that a write lock block other write locks and read locks

1.2.2 lock granularity

Sharing resources for improving concurrency way to lock the object is to make more selective. Locking need to consume resources. The so-called lock strategy is to seek a balance between security locks overhead and data, this balance will certainly affect performance.
MySQL offers a variety of options, each MySQL storage engines can implement your own strategy and lock lock granularity

Table lock

MySQL table lock is the most basic lock strategy, and is a cost-date strategy. It will lock the entire table. The server is like ALTER TABLE statement using the table lock, and ignore the storage engine locking mechanism

Row-level locking

Row-level locking can maximize support parallel processing (also brings the greatest locking overhead). As we all know, to achieve a row-level locking in InnoDB and XtraDB, as well as some other storage engine. In row-level locking storage engine layer only achieved, but achieved no MySQL server layer. Server layer completely unaware storage engine locks for

1.3 Transaction

The transaction is a set of atomic SQL query, or a stand-alone unit of work. Statement within the transaction, either all executed succeed or fail.

ACID transaction
  • Atomicity (Atomicity)
  • Consistency (consistency)
  • Isolation (Isolation)
  • Persistence (durability)
    a realization of the ACID database, the database does not implement ACID compared to typically need more CPU processing power, more memory and more disk space. The user can, depending on whether the business transaction needs to choose the right storage engine
1.3.1 Isolation Level

It defines four levels of isolation in the SQL standard

  • READ UNCOMMITTED (uncommitted read), the transaction can read uncommitted data, which is also known as a dirty read (Dirty Read)
  • READ COMMITTED (read committed), there will be non-repeatable read
  • REPEATABLE READ (repeatable read), to solve dirty reads and non-repeatable read problem, but a problem occurs phantom read
  • SERIALIZABLE (serialization), read magic to solve the problem, it will lock on each row of data is read, it may cause problems with a lot of timeouts and lock contention.
  • Isolation Levels
1.3.2 Deadlock

Deadlock refers to two or more transactions occupy each other on the same resources, and the other occupied resource lock requests, leading to a vicious circle phenomenon. When multiple transactions try to lock resources in a different order, it may produce a deadlock. When multiple transactions simultaneously locking the same resource, will produce a deadlock
in order to solve this problem, the database system to achieve a variety of deadlock detection and deadlock timeout mechanism.InnoDB approach to the deadlock that will hold at least row exclusive lock transaction rollback

1.3.3 Transaction Log

The transaction log can help improve the efficiency of the transaction. Use a transaction log, simply in order to modify its storage engine memory copy when you modify the data table, and then modify the behavior of the logged to persistent on your hard disk in the transaction log, rather than each time the modified data to the disk itself persistent
If you modify the data has been recorded in the transaction log and the persistent, but the data itself is not written back to disk, the system crashes, the storage engine can automatically restore this part of the modified data on restart

The 1.3.4 MySQL affairs

MySQL provides two transactional storage engine: InnoDB Cluster and the NDB
MySQL defaults to auto-commit mode. Can be enabled or disabled by setting the AUTOCOMMIT variable automatic submission mode
MySQL isolation level can be set by executing the command SET TRANSACTION ISOLATION

Mix in a transaction storage engine

MySQL server layer does not manage the transaction, the transaction is the underlying storage engine to achieve. So in the same transaction, we use a variety of storage engines are not reliable

Implicit and explicit locking

InnoDB uses a two-phase locking protocol. During the transaction, the lock can be executed at any time, the lock then execute COMMIT or ROLLBACK only time will release, and all locks are released at the same time.
Note: In addition to the transaction disabled AUTOCOMMIT, than you can use LOCK TABLES, any other time do not explicitly execute LOCK TABLES, regardless of what storage engine

More than 1.4 version concurrency control

MVCC is a variant of row-level locking. By storing the data in the snapshot is a point in time to achieve.
InnoDB is MVCC, is achieved by saving two hidden columns after each line record. The two columns to create a time line to save, a save the expiration time line (or delete time). Of course, not actual time values are stored, but the system version number. Each start a new transaction, the system will automatically increment the version number. System version number will be the start time of the transaction as the version number of the transaction, and for each version of the query to the rows were compared.
FIG Specific operation is as follows:
InnoDB is MVCC
MVCC only work in two REPEATABLE READ and READ COMMITTED isolation level. The other two are not compatible with the isolation level and MVCC, because READ COMMITED always read the latest data line, rather than in line with the current transaction version of the row. The SERIALIZABLE will read lock on all rows

1.5 MySQL storage engine

Use SHOW TABLE STATUS command displays the list of information

1.5.1 InnoDB storage engine

MySQL InnoDB is the default transactional engine, is designed to handle a large number of short-term (short-lived) the majority of transactions, short-term situation is normal affairs submitted, less likely to be rolled back. InnoDB performance and automatic crash recovery features, making it very popular in demand for non-transactional storage in

InnoDB Overview

InnoDB data storage space in the table, the table space is managed by the InnoDB a black box, a series of data files.
InnoDB uses MVCC supports high concurrency, and achieved four standard isolation levels. The default level is REPEATABLE READ (repeatable read), and the policy to prevent phantom read lock through the gap (next-key locking). InnoDB gap locking allows not only locking the query line involved, but also on the gap in the index were locked to prevent insertion of phantom rows
InnoDB table is created based on a clustered index, clustered index on the primary key query has high performance. However, it must contain two primary key index columns,主键应当尽可能的小。InnoDB的存储格式是平台独立的,也就是说可以将数据和索引文件从Intel平台复制到PowerPC或者Sun SPARC平台。

1.5.2 MyISAM 存储引擎

MyISAM提供了大量的特性,包括全文索引、压缩、空间函数(GIS)等,但MyISAM不支持事务和行级锁,而且有一个毫无疑问的缺陷就是崩溃后无法安全恢复。
如果表在创建并导入数据以后,不会再进行修改操作,那么这样的表或许适合采用MyISAM压缩表

1.5.3 MySQL内建的其他存储引擎

Archive引擎、Blackhole引擎、CSV引擎、Federated引擎、Memory引擎、Merge引擎、NDB集群引擎

1.5.4 第三方存储引擎

MySQL从2007年开始提供了插件式的存储引擎API
OLTP类引擎、面向列的存储引擎、社区存储引擎

1.5.5 选择合适的引擎

大部分情况下,InnoDB都是正确的选择。除非需要用到某些InnoDB不具备的特性,并且没有其他办法可以替代,否则都应该优先选择InnoDB引擎
如果要用到全文索引,建议优先考虑InnoDB加上Sphinx的组合,而不是MyISAM。
选择引擎时考虑的因素:事务、备份、崩溃恢复、特有的特性

1.5.6 转换表的引擎

三种方法:

  • ALTER TABLE,需要执行很长时间
  • 导出与导入,可以使用mysqldump工具将数据导出到文件,然后修改存储引擎选项
  • 创建与查询,综合前两种方法,用INSERT…SELECT语法导数据

1.6 MySQL时间线

版本3.23 —— 被认为是MySQL真正“诞生”的时刻
版本5.5 —— Oracle收购Sun以后发布的首个版本

1.7 MySQL的开发模式

现在Oracle为付费用户单独提供了一些服务器插件,而MySQL本身还是遵循开源模式

1.8 总结

MySQL has a layered architecture. Top service and query execution engine server layer, the lower is the storage engine. Although there are many different effect plugins API, but the storage engine API is the most important.If you can understand when the MySQL storage engine and query service layer directly address how the interaction back and forth through the API, MySQL will be able to grasp the essence of the core infrastructure.

Released four original articles · won praise 1 · views 2895

Guess you like

Origin blog.csdn.net/gxl8052/article/details/104236254