MySQL advanced articles - talk about MySQL's storage engine

Article directory:

1. Related commands about the storage engine

2. A detailed introduction to the storage engine

2.1 InnoDB (focus, transactional storage engine with foreign key support)

2.2 MyISAM (emphasis, main non-transactional storage engine)

2.3 Memory (table placed in memory)

2.4 Archive (for data archiving)

2.5 Blackhole (discard write operations, read operations will return empty content)

2.6 CSV (when storing data, separate data items with commas)

2.7 Federated (accessing remote tables)

2.8 Merge (manage a set of tables composed of multiple MyISAM tables)

2.9 NDB (MySQL Cluster Dedicated Storage Engine)

3. Storage engine comparison

4. About InnoDB and MyISAM (emphasis)


1. Related commands about the storage engine

See what storage engine mysql provides . (I am using mysql5.7 here)

You can also use the following commands to view the information of the major storage engines in more detail.

Check out the default storage engine:

show variables like '%storage_engine%'; 
#或
SELECT @@default_storage_engine;

Of course, we also modify the default storage engine.
If the table's storage engine is not explicitly specified in the table creation statement, InnoDB will be used as the table's storage engine by default. If we want to change the default storage engine of the table, we can write the command line to start the server like this:
SET DEFAULT_STORAGE_ENGINE=MyISAM;
Or modify the my.cnf file:
default-storage-engine=MyISAM 

#重启服务 
systemctl restart mysqld
The storage engine is responsible for extracting and writing data in the table. We can set up different storage engines for different tables , that is to say, different tables can have different physical storage structures and different extraction and writing methods. .
Our previous statement to create a table did not specify the storage engine of the table, then the default storage engine InnoDB will be used . If we want to explicitly specify the storage engine of the table, we can write:
CREATE TABLE 表名( 
    建表语句
) ENGINE = 存储引擎名称;
If the table has already been created, we can also use the following statement to modify the storage engine of the table:
ALTER TABLE 表名 ENGINE = 存储引擎名称;

2. A detailed introduction to the storage engine

2.1 InnoDB ( focus, transactional storage engine with foreign key support )

MySQL has included the InnoDB storage engine since 3.23.34a . After 5.5 or greater , the InnoDB engine is used by default .
InnoDB is MySQL 's default transactional engine , which is designed to handle a large number of short-lived transactions. It can ensure the complete commit (Commit) and rollback (Rollback) of the transaction .
In addition to adding and querying, update and delete operations are also required, so the InnoDB storage engine should be preferred .
Unless there is a very specific reason to use another storage engine, the InnoDB engine should be preferred .
Data file structure: table name.frm stores the table structure ( in MySQL 8.0 , it is merged into table name.ibd ) ; table name.ibd stores data and indexes.
InnoDB is designed for maximum performance with huge data volumes . In previous releases, dictionary data was stored in metadata files, non-transactional tables, and so on. Now these metadata files are removed . For example: .frm , .par , .trn , .isl , .db.opt and so on do not exist in MySQL8.0 .
Compared with the MyISAM storage engine, InnoDB is less efficient to write and will take up more disk space to save data and indexes.
MyISAM only caches indexes and does not cache real data; InnoDB not only caches indexes but also caches real data, which requires high memory, and the size of memory has a decisive impact on performance.

2.2 MyISAM ( emphasis, main non-transactional storage engine )

MyISAM provides a large number of features, including full-text indexing, compression, spatial functions (GIS) , etc., but MyISAM does not support transactions, row-level locks, foreign keys , and there is an undoubted defect that it cannot be safely recovered after a crash .
The default storage engine before 5.5 .
The advantage is that the access speed is fast , and there is no requirement for transaction integrity or applications that focus on SELECT and INSERT .
There is additional constant storage for data statistics. Therefore , the query efficiency of count(*) is very high.
Data file structure: table name.frm storage table structure ; table name.MYD storage data (MYData) ; table name.MYI storage index (MYIndex).
Application scenarios: read-only applications or read-based services.

2.3 Memory (table placed in memory)

The logical medium used by Memory is memory , which responds very quickly , but when the mysqld daemon crashes, the data will be lost . In addition, the data required to be stored is in a format with constant data length, for example, data of Blob and Text types are not available ( the length is not fixed ) .
Main features:
        Memory supports both hash ( HASH ) indexes and B+ tree indexes .
        Memory tables are at least an order of magnitude faster than MyISAM tables .
        The size of the MEMORY table is limited . The size of the table is mainly determined by two parameters, max_rows and max_heap_table_size . Among them, max_rows can be specified when creating a table; the size of max_heap_table_size is 16MB by default , which can be expanded as needed.
        Data files are stored separately from index files.
        Disadvantages: Its data is easy to lose and its life cycle is short. Due to this flaw, special care needs to be taken when choosing the MEMORY storage engine.
Scenarios using the Memory storage engine:
        1. The target data is relatively small , and it is accessed very frequently the data is stored in the memory. If the data is too large, it will cause memory overflow . The size of the Memory table can be controlled by the parameter max_heap_table_size to limit the maximum size Memory table.
        2. If the data is temporary and must be available immediately , it can be placed in memory.
        3. It does not matter if the data stored in the memory table is suddenly lost .

2.4 Archive (for data archiving)

2.5 Blackhole (discard write operation, read operation will return empty content)

2.6 CSV ( when storing data, separate data items with commas)

2.7 Federated (accessing remote tables)

2.8 Merge (manage a set of tables composed of multiple MyISAM tables)

2.9 NDB ( MySQL cluster dedicated storage engine)


3. Storage engine comparison

In fact, the most commonly used ones are InnoDB and MyISAM , and Memory is sometimes mentioned . Among them, InnoDB is MySQL 's default storage engine.

4. About InnoDB and MyISAM (emphasis)

Many people have doubts about the choice between InnoDB and MyISAM . Which one is better?
The default storage engine before MySQL 5.5 was MyISAM , and it was changed to InnoDB after 5.5 .

Guess you like

Origin blog.csdn.net/weixin_43823808/article/details/124099884