MySQL Advanced - Comparison of MySQL Architecture and Common Storage Engines

This article introduces the MySQL architecture and the comparison of storage engines (InnoDB, MyISAM, Memory)

MySQL Architecture

insert image description here

connection layer

The top layer is some clients and connection services, which mainly complete some connection processing, authorization and authentication, and related security solutions. The server also verifies the operating authority it has for each client that accesses securely.

Provides the ability to establish a connection with the MySQL server, and supports almost all mainstream server-side languages, such as Java, C, C++, Python, etc., and each language establishes a connection with MySQL through its own API interface.

service layer

The database service layer is the core of the entire database server, mainly including system management and control tools, connection pool, SQL interface, parser, query optimizer and cache.

The second-tier architecture mainly completes most of the core service functions, such as SQL interface, cached query, SQL analysis and optimization, and execution of some built-in functions. All cross-storage engine functions are also implemented at this layer, such as stored procedures, functions, and so on.

connection pool

It is mainly responsible for storing and managing the connection information between the client and the database. A thread in the connection pool is responsible for managing the connection information between a client and the database.

System Management and Control Tools

Provide management and control functions of the database system, such as backing up and restoring data in the database, ensuring the security of the entire database, providing security management, coordinating and managing the cluster of the entire database, etc.

SQL interface

It is mainly responsible for receiving various SQL commands sent by the client, sending the SQL commands to other parts, receiving the result data returned by other parts, and returning the result data to the client.

parser

It is mainly responsible for parsing the requested SQL into a "parse tree", and then further grammatically verifying the "parse tree" according to some rules in MySQL to confirm whether it is legal.

query optimizer

In MySQL, if the "parse tree" passes the syntax check of the parser, the optimizer will convert it into an execution plan, and then interact with the storage engine, and interact with the underlying data files through the storage engine.

cache

MySQL's cache is composed of a series of small caches. For example: MySQL table cache, record cache, permission cache in MySQL, engine cache, etc. The cache in MySQL can improve the query performance of data. If the query result can hit the cache, MySQL will directly return the result information in the cache.

engine layer

The storage engine is really responsible for the storage and retrieval of data in MySQL, and the server communicates with the storage engine through the API. Different storage engines have different functions, so we can choose the appropriate storage engine according to our needs.

storage layer

Mainly data (such as: redolog, undolog, data, index, binary log, error log, query

Logs, slow query logs, etc.) are stored on the file system and complete the interaction with the storage engine.

storage engine

The storage engine is the implementation of technologies such as storing data, building indexes, and updating/querying data . The storage engine is based on tables rather than libraries, so storage engines can also be called table engines.
The default storage engine is InnoDB.

-- 查询建表语句
show create table account;
-- 建表时指定存储引擎
CREATE TABLE 表名(
    ...
) ENGINE=INNODB;
-- 查看当前数据库支持的存储引擎
show engines;

InnoDB

InnoDB is a general-purpose storage engine that takes into account high reliability and high performance. After MySQL 5.5, it InnoDBis the default MySQL 引擎.

Features:

  • DML operations follow the ACID model and support transactions
  • Row-level locks to improve concurrent access performance
  • Support foreign key constraints to ensure data integrity and correctness

document:

  • xxx.ibd: xxx represents the table name. Each table of the InnoDB engine will correspond to such a table space file, which stores the table structure (frm, sdi), data and indexes of the table.

Parameter: innodb_file_per_table, determines whether multiple tables share a table space or each table corresponds to a table space

InnoDB logical storage structure:
insert image description here

  • Tablespace : The highest level of the logical structure of the InnoDB storage engine. The ibd file is actually a tablespace file, which can contain multiple segments.
  • Segment : A tablespace is composed of various segments. Common segments include data segment, index segment, and rollback segment. The management of segments in InnoDB is done by the engine itself, without manual control. A segment contains multiple areas.
  • Area : Area is the unit structure of table space, and the size of each area is 1M. By default, the InnoDB storage engine page size is 16K, that is, there are 64 consecutive pages in an area .
  • Page : A page is the smallest unit of an area, and a page is also the smallest unit of InnoDB storage engine disk management. The default size of each page is 16KB. In order to ensure the continuity of pages, the InnoDB storage engine applies for 4-5 areas from the disk each time.
  • Row : The InnoDB storage engine is row-oriented, that is to say, data is stored by row. In addition to the fields specified when defining the table, each row contains two hidden fields (described in detail later).

MyISAM

MyISAM was the default storage engine in the early days of MySQL.

Features:

  • Does not support transactions, does not support foreign keys
  • Table locks are supported, but row locks are not supported
  • fast access

document:

  • xxx.sdi: store table structure information
  • xxx.MYD: storage data
  • xxx.MYI: storage index

Memory

The table data of the Memory engine is stored in memory. Due to hardware problems and power failure problems, these tables can only be used as temporary tables or caches.

Features:

  • Stored in memory, fast
  • hash index (default)

document:

  • xxx.sdi: store table structure information

the difference

insert image description here
When selecting a storage engine, an appropriate storage engine should be selected according to the characteristics of the application system. For complex application systems, multiple storage engines can also be selected for combination according to the actual situation.

  • InnoDB : It is the default storage engine of Mysql, which supports transactions, foreign keys, and row-level locks. If the application has relatively high requirements on the integrity of the transaction, requires data consistency under concurrent conditions, and data operations include many update and delete operations in addition to insert and query, then the InnoDB storage engine is a more suitable choice .
  • MyISAM : If the application is mainly read and insert operations, with few update and delete operations, and the integrity and concurrency requirements of transactions are not very high, then this storage engine is very suitable to choose.
  • MEMORY : Store all data in memory, with fast access speed, usually used for temporary tables and caches. The disadvantage of MEMORY is that there is a limit on the size of the table. Too large a table cannot be cached in memory, and data security cannot be guaranteed.

Guess you like

Origin blog.csdn.net/baidu_33256174/article/details/130693788