MySQL storage engine comparison summary

1. What is a storage engine

The storage engine is the core of the database. For mysql, the storage engine runs as a plug-in. MySQL is configured with many different storage engines by default, which can be preconfigured or enabled in the MySQL server. You can choose storage engines for servers, databases, and tables that give you maximum flexibility in choosing how to store your information, how to retrieve it, and what performance and functionality you need with your data.

2. What are the storage engines?

The MySQL version is later than 5 (the blog test version is MySQL5.5.15)

1. View the storage engines supported by MySQL by default

show engines \g;

insert image description here

It can be seen that there are a total of nine default storage engines, but there are only a few that can be used frequently.

2. Comparison of major storage engines

insert image description here

3. Introduction to commonly used storage engines

1、InnoDB

The default database storage engine after MySQL5.5.

This may be the most commonly used database engine, because it supports transactions, and there are not many storage engines that support transactions in MySQL. In addition to InnoDB, there is also NDB (and NDB only supports use in NDB clusters), so generally you need In the scenario where transactions are used, we generally choose the InnoDB storage engine.

(1) Advantages and disadvantages

Advantages: support ACID transactions; clustered indexes; support foreign keys; support row locks; there is buffer management, through the buffer pool, all indexes and data are cached to speed up the query;

Disadvantages: It does not support full-text indexing and does not save the number of table rows. Compared with MyISAM, InnoDB's write processing efficiency is lower, and it will take up more disk space to retain data and indexes.

(2) Storage method

① Use shared table space storage:

The table structure created in this way is saved in the .frm file, and the data and indexes are saved in the table space defined by innodb_data_home_dir and innodb_data_file_path, which can be multiple files.

② Use multi-table space storage:

The table structure created in this way is still saved in the .frm file, but the data and indexes of each table are saved separately in the .idb file. If it is a partition table, each partition corresponds to a separate .idb file, and the file name is "table name + partition name". Spread evenly across multiple disks.

To use the multi-tablespace storage method, you need to set the parameter innodb_file_per_table and restart the server before it will take effect, and it will only take effect for newly created tables. There is no size limit for the data file of the multi-table space, and there is no need to set the initial size, the maximum limit of the file, the extended size and other parameters. Even in the multi-tablespace storage mode, the shared tablespace is still necessary. InnoDB puts the internal data dictionary and work logs in this file, so it is not possible to directly copy the .idb file when backing up a table that uses the multi-tablespace feature. , you can restore the data backup to the database with the command:

ALTER TABLE tbl_name DISCARD TABLESPACE;
ALTER TABLE tbl_name IMPORT TABLESPACE;

But this can only be restored to the original database where the table is located. If you need to restore to other databases, you need to use mysqldump and mysqlimport to achieve.

(3) Data files

InnoDB's data files are determined by the storage method of the table.

① Shared tablespace file: defined by the parameters innodb_data_home_dir and innodb_data_file_path, used to store data dictionaries and logs, etc.

② .frm: store table structure definition, both shared table space storage and multi-table space storage.

③ .idb: When using multi-table space storage, it is used to store table data and indexes. If shared table space storage is used, there is no such file.

(4) Foreign key constraints

InnoDB is the only MySQL engine that supports foreign key constraints. Foreign key constraints allow the database to ensure data integrity and consistency through foreign keys, but the introduction of foreign keys will degrade the speed and performance. When creating a foreign key, the parent table must have a corresponding index, and the child table will automatically create a corresponding index when creating a foreign key.

(5) Applicable conditions

If the application has relatively high requirements for 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 should be a more suitable choice . The InnoDB storage engine not only effectively reduces the locking caused by deletion and update, but also ensures the complete submission and rollback of transactions. For systems with high data accuracy requirements such as billing systems or financial systems, InnoDB is suitable choose.

2、MyISAM

(1) Advantages and disadvantages

Advantages: full-text index, non-clustered index, fast query speed.

Disadvantages: It does not support transactions, does not support foreign keys, and only supports table locks; if the host where the database is located is down, the data files of MyISAM are easily damaged and difficult to recover;

(2) Storage method

① Static table (default): The fields are non-variable (each record is of fixed length). Storage is very fast, easy to cache, and easy to recover from failures; usually takes up more space than dynamic tables.

② Dynamic table: It occupies relatively less space, but frequent update and deletion of records will cause fragmentation. It is necessary to regularly execute the optimize table or myisamchk -r command to improve performance, and it is difficult to recover when a failure occurs.

③ Compressed table: Created using the myisampack tool, which takes up very little disk space. Because each record is compressed individually, there is very little access overhead.
The data in the static table will be filled with spaces according to the width of the column when it is stored, and these spaces will be removed before returning the data to the application. If there is a space after the content that needs to be saved, it will also be removed when the result is returned. (In fact, it is the behavior of the data type char. If there is this data type in the dynamic table, this problem will also occur.)

(Static and dynamic tables are selected automatically based on the type of columns being used.)

(3) Data files

The MyISAM data table is stored on the disk as three files, the file names are the same as the table name, and the extensions are:

① .frm: store the definition of the data table structure.

② .MYD: Store table data.

③ .MYI: storage table index.

Among them, data files and index files can be placed in different directories to evenly distribute IO and obtain faster speed. To specify the path of the index file and data file, you need to specify it through the data directory and index directory statements when creating the table. (The file path needs to be an absolute path and have access permissions)

A MyISAM table may be damaged for various reasons. The damaged table may not be accessible, and it will prompt that it needs to be repaired or return an incorrect result after accessing. You can use the check table statement to check the health of the MyISAM table, and use the repair table statement to repair the damaged MyISAM table.

frm and MYI can be stored in different directories. The MYI file is used to store the index, but only saves the pointer of the page where the record is located, and the structure of the index is a B+ tree structure. The following picture is the mechanism of MYI file saving:

insert image description here

It can be seen from this picture that the storage engine searches for record pages through the B+ tree structure of MYI, and then searches for records based on the record pages. And it supports full-text index, B-tree index and data compression.

(4) Applicable conditions

If the application is mainly read and insert operations, with only a few update and delete operations, and the requirements for transaction integrity and concurrency are not very high, then this storage engine is very suitable. MyISAM is one of the most commonly used storage engines in web, data warehouse, and other application environments.

3、MEMORY

(1) Advantages and disadvantages

Advantages: ①Separate static and dynamic data, ②Use data with close structure to optimize queries, ③You can access less data when querying, ④Easier to maintain large data sets, ⑤You can modify the merge table by modifying the .mrg file , of course, you can also use alter to modify. After modification, you need to refresh the table cache through flush tables. This method can dynamically increase or decrease sub-tables. ⑥Memory storage engine stores the data of the table in memory, which can be operated and managed as conveniently as sessions or caches , give full play to the characteristics of the memory engine - fast speed, low latency; only read or write-based access mode.

Disadvantages: Once the service is shut down, the data in the table will be lost; there is a limit on the size of the table.

(2) Storage method

Data is stored in memory.

(3) Data files

Each MEMORY table corresponds to only one .frm disk file, which is used to store the structure definition of the table, and the table data is stored in memory. By default, HASH indexes are used instead of BTREE indexes.

(4) Applicable conditions

The Memory storage engine is mainly used in code tables whose content changes infrequently, or as an intermediate result table of statistical operations, so as to efficiently analyze the intermediate results and obtain the final statistical results.

4、MRG_MYISAM (MERGE)

(1) Engine principle

The Merge storage engine is a combination of a group of MyISAM tables. These MyISAM tables must have the same structure. The merge table itself has no data. The merge type table can be queried, updated, and deleted. These operations are actually for the internal actual MyISAM tables are made.

Define the insert operation of the merge table through the insert_method clause: use first or last to make the insert operation act on the first or last table accordingly, if not defined or defined as No, it means that the merge table cannot be inserted. The drop operation on the merge table only deletes the definition of merge, and has no effect on the internal tables.

(2) Storage method

(3) Data files

① .frm: storage table definition.

② .MRG: Store the information of the combination table, including which tables the merge table consists of and the basis for inserting new data. You can modify the merge table by modifying the .mrg file, but after modification, you need to refresh it through flush tables.

(4) Applicable conditions

Used to logically group a series of identical MyISAM tables together and refer to them as one object. The advantage of the MERGE table is that it can break through the limit on the size of a single MyISAM table, and by distributing different tables on multiple disks, the access efficiency of the MERGE table can be effectively improved. This is ideal for VLDB environments such as data warehousing.

5、ARCHIVE

The Archive storage engine is basically used for data archiving; its compression ratio is very high, and its storage space is about 10-15 times that of InnoDB, so it is very suitable for storing historical data, because it does not support indexes and cannot cache Indexes and data, so it is not suitable as a storage engine for concurrent access tables. The archive storage engine uses row locks to achieve high concurrent insert operations, but it does not support transactions, and its design goal is only to provide high-speed insert and compression functions.

6. BLACKHOLE

Black hole engine, students who have studied the Internet have probably heard of black hole routers, and have been exposed to /dev/null in the Linux system. This engine is the same as this one. The table does not save the database. Take the pressure off the master.

7、FEDERATED

Mainly used to link with the remote database engine

8、CSV

The CSV storage engine can process CSV files as MySQL tables. The storage format is an ordinary CSV file

Features: ① store data in CSV format, ② all columns must not be null, ③ does not support indexes, not suitable for large tables, not suitable for online processing, ④ can directly edit data files

9、PERFORMANCE_SCHEMA

It is used to monitor the resource consumption, resource waiting, etc. of the MySQL server in a lower-level running process

10、NDB

(also known as NDBCLUSTER) - This clustered database engine is especially suited for applications that require maximum uptime and availability.

Guess you like

Origin blog.csdn.net/weixin_44816664/article/details/130480880