MySQL Architecture Overview

MySQL Architecture Overview

Here Insert Picture Description

Entire MySQL Server consists of

  • Connection Pool: connection pooling assembly
  • Management Services & Utilities: management services and tools for assembly
  • SQL Interface: SQL interface components
  • Parser: Query Analyzer component
  • Optimizer: Optimization component
  • Caches & Buffers: Buffer cell module
  • Pluggable Storage Engines: Storage Engine
  • File System: File System

1) connection layer

The top layer is a number of clients and link services, which includes communications and most local sock-based client / server tools to achieve similar TCP / IP communication. The main connection to complete some similar process, authorization and authentication, and related security program. the concept is the introduction of the thread pool on the floor, provides a thread to secure access through authentication of the client. also on this level can achieve a secure link to an SSL-based server will secure access to each customer's end verify it has the operating authority.

2) Service Layer

The second-tier architecture mainly to complete the majority of core services, such as SQL interface, and complete the cached query, analysis and optimization of SQL, execute built-in functions are part of all functions across storage engines also achieve this level, such as process , function, etc. in this layer, the server parses the query and create a corresponding internal parse tree, and its corresponding complete list of query optimization and determine whether the use of the index, and finally generate the corresponding operation is performed. If a select statement, internal server will query cache, if the cache space is large enough so that in the settlement of a large number of read operations can be a good environment to enhance the performance of the system.

3) engine layer

Storage engine layer, the storage engine truly responsible for the storage and retrieval MySQL data, the server communicates through the API and storage engine. Different storage engines have different functions, so that we according to their needs, to select the appropriate storage engine.

4) the storage layer

Data storage layer, mainly to interact on the file system, and complete data storage and storage engine.

And compared to other databases, MySQL a little different, his architecture can be used in a variety of different scenarios and play a good role is mainly reflected in the storage engine, pluggable storage engine architecture, the query processing and other system tasks and separating the extracted data is stored. this architecture can select an appropriate memory storage engine according to the actual needs of the business.

1. Storage Engine

1.1 Storage Engine Overview

And most of the different databases, MySQL storage engine, there's a concept. For different storage requirements can choose the best storage engine.

Storage engine that stores data, indexing, querying the database update and other technologies to achieve the storage engine is based on the table, rather than on the library. Therefore, the engine may also be referred to as a storage table type.

Oracle, SqlServer, etc. Only one database storage engine .MySQL provides plug-in storage engine architecture, so there are multiple MySQL storage engine, can use the appropriate engine as needed, or write storage engines.

MySQL5.0 supported storage engine include: InnoDB, MyISAM, BDB, MEMORY, MERGE, EXAMPLE, NDB Cluster, ARCHIVE, CSV, BLACKHOLE, FEDERATED, etc., which provides transaction-safe InnoDB and BDB table, other non-transaction-safe storage engine tables.

  • View database storage engine

    show engines;
    

    result:

    [Picture dump outside the chain fails, the source station may have a security chain mechanism, it is recommended to save the pictures uploaded directly down (img-f39Bro5b-1580886242138) (E: \ Cong master class learning network folder \ mysql Advanced \ sys_2.png)]

    Engine: storage engine's name

    Support: support storage engine

    Comment: interpretation

    Transactions: Transaction support

View the default database storage engine

show variables like '%storage_engine%';

result:

InnoDB

1.2 Characteristics of various storage engines

Common storage engine, and the difference between the respective comparison engine early spring, as shown in the following table:

Feature InnoDB MyISAM MEMORY MERGE NDB
Storage Limits 64TB Have Have No Have
Transaction Security stand by
Lock mechanism Line Lock (for high concurrency) Table lock Table lock Table lock Row lock
B-tree index stand by stand by stand by stand by stand by
Hash indexes stand by
Full-text index Support (version 5.6 or later) stand by
Clustered index stand by
Data Index stand by stand by stand by
Index Cache stand by stand by stand by stand by stand by
Compressible data stand by
Bulk insert speed low high high high high
Support foreign keys stand by

Two kinds longest use from storage engine: InnoDB, MyISAM, the other two MEMORY, MERGE, can understand.

1.2.1 InnoDB

InnoDB storage engine is the default storage engine for MySQL storage engine provides .InnoDB has commit, rollback, crash recovery capabilities of transaction-safe. But the contrast MyISAM storage engine, InnoDB write processing efficiency worse, and will take up more disk space to retain the data and indexes.

InnoDB storage engine characteristics different from other storage engines:

Transaction control:

create table goods_innodb(
	id int not null auto_increment,
	name varchar(20) not null,
	primary key(id)
)ENGINE=innodb DEFAULT CHARSET=UTF-8;
START transaction;

insert into goods_innodb(id,name) values(null,'Meta20');

commit;

[Picture dump outside the chain fails, the source station may have a security chain mechanism, it is recommended to save the pictures uploaded directly down (img-DtyinFYc-1580886242158) (E: \ Cong master class learning network folder \ mysql Advanced \ sys_3.png)]

Innodb is transactional

MySQL storage engines support foreign keys only InnoDB, when creating the foreign key, requiring the parent table must have a corresponding index, a sub table when creating a foreign key, it will automatically create a corresponding index.

The following two tables, country_innoDB is the parent table, is a group of the country_id key index, city_innodb table is the child, the country_id foreign key field, corresponding to the primary key table country_id country_innodb.

CREATE TABLE country_innodb(
	country_id INT NOT NULL AUTO_INCREMENT,
    country_name VARCHAR(100) NOT NULL,
    PRIMARY KEY(country_id)
)ENGINE=INNODB DEFAULT CHARSET=UTF8;



CREATE TABLE city_innodb(
    city_id INT NOT NULL AUTO_INCREMENT,
    city_name VARCHAR(50) NOT NULL,
    country_id INT NOT NULL,
    PRIMARY KEY(city_id),
    KEY idx_fk_country_id(country_id),
    CONSTRAINT 'fk_city_country' FOREIGN KEY(country_id) REFERENCES country_innodb(country_id) ON DELETE RESTRICT ON UPDATE CASCADE
)ENGINE=INNODB DEFAULT CHARSET=utf8;



insert into country_innodb values(null,'China'),(null,'America'),(null,'Japan');
insert into city_innodb values(null,'Xian',1),(null,'NewYork',2),(null,'BeiJing',1);

When you create an index, you can specify when you delete, update the parent table, for operating tables, including RESTRICT, CASCADE, SET NULL and NO ACTION.

Same RESTRICT and NO ACTION, is limited in sub-table means associated case, the parent table can not be updated;

When the parent table represents CASCADE update or delete, update, or delete records corresponding to the sub-table;

SET NULL indicates updated or deleted in the parent table when the table corresponding to the sub-field is SET NULL.

For the above two tables, foreign keys are designated table is ON DELETE RESTRICT ON UPDATE CASCADE mode, the record in the main table to delete the time, if there is the corresponding record in the child table can not be deleted; update records in the primary table time, if the child table has a corresponding record, the child table corresponding to the update.

ON DELETE RESTRICT --删除主表数据时,如果有关联记录,不删除;
ON UPDATE CASCADE --更新主表时,如果子表有关联记录,更新字表记录;

Storage:

InnoDB storage tables and indexes in the following two ways:

  • Using a shared space table stored, the table structure created this way .frm table stored in a file, and index data stored in the table and the space InnoDB_data_home_dir innodb_data_file_path defined, may be a plurality of files
  • Use multi-table storage space, table structure created in this way and then throw in the presence of the .frm file, but each table and index data stored in separate .ibd in.

1.2.2MyISAM

MyISAM does not support transactions do not support foreign keys, and its advantage is speed of access, the integrity of the transaction does not require or to SELECT, INSERT basically based applications can use this engine to create the table. There are two a more important point

It does not support transactions

create table goods_myisam(
	id int not null auto_increment,
	name varchar(20) not null,
	primary kry(id)
)ENGINE=myisam DEFAULT CHARSET=utf8;

Here is a rollback of the bad;

File storage

Each MyISAM storage on disk into three files, the file name and table names are the same, but the extension are:

.frm (stores the table);

.MYD (MYData, storing data);

.MYI (MYIndex, stores the index);

MEMORY

memory storage engine data table stored in memory. MEMORY Each table corresponds to an actual disk file format is .frm, only the structure of the file storage table, and its data files are stored in memory, it is a good rapid processing of data, to improve the overall rapid processing, improve the efficiency of the entire table. MEMORY type of table access is very fast, because his data is stored in memory, and uses HASH indexes by default, but once the service is closed, the data table will be lost.

1.2.4MERGE

MERGE storage engine is a combination of a set of MyISAM tables, these tables must have an identical structure MyISAM, MERGE table itself, and no data is stored, a table of type MERGE can query, update, delete operations, which are actually on the inside of MyISAM the table.

For insertion type MERGE table, by inserting INSERT_METHOD clause definition table, there may be three different values, using the FIRST or LAST value so that the insertion operation is correspondingly acts on the first or the last table, this is not defined clause, or defined to NO, showing the insertion operation can not be performed on this table MERGE.

DROP operations can be performed on the MERGE table, but this operation just removes the definition of MERGE tables, the internal table is not affected.

[Picture dump outside the chain fails, the source station may have a security chain mechanism, it is recommended to save the pictures uploaded directly down (img-r1ALJovX-1580886242160) (E: \ Cong master class learning network folder \ mysql Advanced \ sys_4.png)]

Here is an example of creating and using a MERGE table

1) create three test table payment_1990, payment_1991, payment_all, which payment_all MERGE table are two tables:

create table order_1990(
	order_id int,
    order_money double(10,2),
    order_address varchar(50),
    primary key(order_id)
)engine = myisam default charset=utf8;

create table order_1991(
	order_id int,
    order_money double(10,2),
    order_address varchar(50),
    primary key(order_id)
)engine = myisam default charset=utf8;

create table order_all(
	order_id int,
    order_money double(10,2),
    order_address varchar(50),
    primary key(order_id)
)engine =merge union(order_1990,order_1991) INSERT_METHOD=LAST default charset=utf8;

2) are inserted into two records in the table

INSERT INTO order_1990 VALUES(1,100.0,'北京');
INSERT INTO order_1990 VALUES(2,100.0,'上海');

INSERT INTO order_1991 VALUES(10,200.0,'北京');
INSERT INTO order_1991 VALUES(11,200.0,'上海');

order_all results:

[Picture dump outside the chain fails, the source station may have a security chain mechanism, it is recommended to save the pictures uploaded directly down (img-k6rINsE9-1580886242162) (E: \ Cong master class learning network folder \ mysql Advanced \ sys_5.png)]

1.3 storage engine of choice

When selecting the storage engine, the engine should select the appropriate storage system according to the characteristics of the application. For complex applications, you may also select various combinations of storage engine according to actual situation. The following are several common storage engine environment.

  • InnoDB: of MySQL's default storage engine for transaction processing procedures, foreign keys support the integrity of the transaction if the application has a relatively high demand for data consistency requirements under concurrent conditions, in addition to operations other than data insertion and query. , also contains many updates, deletes, then the InnoDB storage engine is a more appropriate choice .InnoDB storage engine in addition to effectively reduce due to deletion and update causes the lock, you can also ensure the integrity of the transaction commit and rollback for similar terms fee system or the financial system for data accuracy requirements are relatively high system, InnoDB is the most appropriate choice.
  • MyISAM: If the application is read and insert operations-based, only a few of update and delete operations, and the integrity of the transaction, concurrency requirements are not high, then choose the storage engine is very appropriate.
  • MEMORY: all data stored in a RAM (Random Access Memory (English: Random Access Memory, abbreviation: RAM), also known as main memory is a CPU to directly exchange data in an internal memory which can be read at any time (except when a refresh). , and fast, usually as the operating system or other program is running at any time from any of the specified write address .RAM temporary work data storage medium (stored) or read (extracted) information it the biggest difference is volatile ROM data, i.e., data stored in a power down will subsequently be lost .RAM for temporarily storing programs, data and intermediate results in the computer and digital systems), the need to quickly locate records and other similar data environment that provides access to a few. MEMORY flaw is that there are restrictions on the size of the table, the table can not be much of a cached in memory, followed by the table to ensure that the data can be restored, the data in the table after abnormal termination of the database can be restored. MEMORY tables are usually updated less frequently for a small table to quickly get access to the results.
  • MERGE: for a series of identical MyISAM table to logically grouped together, and their advantage as an object reference table that can break .MERGE size limits on individual MyISAM table and distributed by different tables in a plurality of on disk, can effectively improve the efficiency of access MERGE table. this is very suitable for storage, such as data warehouse VLDB environments.
Published 69 original articles · won praise 6 · views 2483

Guess you like

Origin blog.csdn.net/qq_40539437/article/details/104182922