Storage engine of mysql series

This article mainly introduces the storage engine of mysql, and explains the characteristics and differences of the main storage engines.

One, storage engine introduction

Insert picture description here

The database storage engine is the underlying software component of the database. The database management system uses the data engine to create, query, update and delete data. Different storage engines provide different storage mechanisms, indexing techniques, locking levels and other functions, and specific functions can also be obtained by using different storage engines.

The core of MySQL is the storage engine, and the features supported by different storage engines are different.

The storage engine in MySQL is a plug-in storage engine.

After MySQL 5.5.5, InnoDB is the default storage engine.

The control level of the storage engine in MySQL is the table level. We can specify the storage engine used by the table when creating the table.

2. Types of storage engines supported by MySQL

The storage engines supported by MySQL 5.7 include InnoDB, MyISAM, Memory, Merge, Archive, Federated, CSV, BLACKHOLE, etc.

You can use the SHOW ENGINES statement to view the engine types supported by the system, and the result is shown in the figure.
Insert picture description here
The value of the Support column indicates whether a certain engine can be used, YES indicates that it can be used, NO indicates that it cannot be used, and DEFAULT indicates that the engine is the current default storage engine.

The official website describes the characteristics of each storage engine:

Feature MyISAM Memory InnoDB Archive NDB
Transaction support No No Yes No Yes
Lock granularity Table Table Row Row Row
Storage limit 256TB RAM 64TB None 384EB
MVCC (Multi Version Control) No No Yes No No
Foreign key support No No Yes No Yes (note 5)
Clustered index No No Yes No No
B-tree indexes Yes Yes Yes No No
Full text search support Yes No Yes (note 6) No No
Geospatial data type support Yes No Yes Yes Yes
Geospatial index support Yes No Yes (note 7) No No
Hash indexes No Yes No (note 8) No Yes
T-tree index No No No No Yes
Index cache Yes N/A Yes No Yes
Cluster support No No No No Yes
Compressed data Yes No Yes Yes No
Data cache No N/A Yes No Yes
data encryption Yes (note 3) Yes (note 3) Yes (note 4) Yes (note 3) Yes (note 3)
Backup and point-in-time recovery Yes Yes Yes Yes Yes
Replication support (master-slave support) Yes Limited (note 9) Yes Yes Yes
Update statistics for data dictionary Yes Yes Yes Yes Yes

Note:
According to the importance of the characteristics and the difference, the order has been adjusted to a certain extent.

3. Introduction to the types of key engines

1. InnoDB storage engine

Since MySQL5.5, the default built-in storage engine of MySQL has been InnoDB. Its main features are:
(1) Disaster recovery is better;
(2) Support transactions. The default transaction isolation level is repeatability, which is achieved through MVCC (Concurrent Version Control).
(3) The lock granularity used is row-level lock, which can support higher concurrency;
(4) Support foreign keys;
(5) With some hot backup tools, it can support online hot backup;
(6) There is buffer management in InnoDB , Through the buffer pool, all the indexes and data are cached to speed up the query speed;
(7) Support clustered index. For InnoDB type tables, the physical organization of the data is a clustered table. All data is organized according to the primary key. The data and the index are put together, and they are all located on the leaf nodes of the B+ number, and querying through the clustered index can reduce back-to-table queries.
(8) InnoDB does not save the specific number of rows in the table, and a full table scan is required when executing select count(*) from table.
(9) Support B-tree index and full-text search (InnoDB storage engine starts to support full-text search after MySQL 5.6)
(10 ) Hash index is not supported, but adaptive hash index is built-in.

2. MyISAM storage engine

Before version 5.5, MyISAM was the default storage engine for MySQL. The storage engine has poor concurrency and does not support transactions, so there are fewer usage scenarios. The main features are:

(1) Does not support transactions;
(2) Does not support foreign keys. If you forcibly add foreign keys, no error will be prompted, but the foreign keys do not work;
(3) Cluster indexes are not supported, and data query caching will only be cached Index does not cache data like InnoDB, and uses the operating system's own cache;
(4) The default lock granularity is table-level lock, so the concurrency is very poor, the lock is fast, and the lock conflict is less, so it is not easy Deadlock occurs;
(5) Support full-text indexing (after MySQL5.6, InnoDB storage engine also supports full-text indexing), but MySQL's full-text indexing is basically not used. For full-text indexing, there are other mature solutions. For example: ElasticSearch, Solr, Sphinx, etc.
(6) If the host where the database is located goes down, the MyISAM data files are easily damaged and difficult to recover;

Key point: Comparison of InnoDB and MyISAM

1. Transaction support: InnoDB supports transactions, MyISAM does not support transactions.
2. Lock granularity: InnoDB is a row-level lock, MyISAM is a table-level lock.
Therefore, MyISAM is more prone to deadlock than InnoDB, the probability of lock conflicts is greater, and the cost of locking is also greater. Because each row needs to be locked, InnoDB supports higher concurrency than MyISAM;
3. Foreign key support: InnoDB supports foreign keys, MyISAM does not support foreign keys.
4. Clustered index support: InnoDB supports clustered index, MyISAM does not support clustering.
5. Data security and backup: In terms of backup and disaster recovery, InnoDB supports online hot backup and has a very mature online hot backup solution;
6. Query performance: MyISAM's query efficiency is higher than InnoDB, because InnoDB is in the query process. The data cache needs to be maintained, and the query process is to locate the data block where the row is located, and then locate the row to be found from the data block; and MyISAM can directly locate the memory address where the data is located, and the data can be found directly;
7. Statistics of the total number of records in the table: SELECT COUNT(*) statement, if the number of rows is above tens of millions, MyISAM can quickly find out, but InnoDB queries are particularly slow, because MyISAM stores the number of rows separately, and InnoDB needs Zhu to go Count the number of rows; so if you use InnoDB and need to query the number of rows, you need to perform special processing on the number of rows, such as offline query and cache;
8. Storage files: MyISAM table structure files include: .frm (table structure definition) ,.MYI (index), .MYD (data); and InnoDB's table data files are: .ibd and .frm (table structure definition);

3. MEMORY storage engine

Store data in memory, similar to Redis and memcached on the market. In order to improve data access speed, the main features are:

(1) The supported data types are limited, such as: TEXT and BLOB types are not supported. For string type data, only fixed-length rows are supported, and VARCHAR will be automatically stored as CHAR type;
(2) The supported lock granularity is Table-level locks. Therefore, when the amount of access is relatively large, the table-level lock will become the bottleneck of the MEMORY storage engine;
(3) Since the data is stored in the memory, all data will be lost after the server restarts;
(4) When querying, if Use a temporary table, and there are BLOB, TEXT type fields in the temporary table, then the temporary table will be converted to a MyISAM type table, the performance will be reduced sharply;

Fourth, how to choose the right storage engine

1. Does the usage scenario require transaction support;
2. Does it need to support high concurrency? InnoDB has a much higher concurrency than MyISAM;
3. Does it need to support foreign keys;
4. Does it need to support online hot backup;
5. Efficient data buffering, InnoDB Both data and indexes are buffered, while MyISAM only buffers indexes;
6. Indexes are not the same for different storage engines;
7. Do you need to support clustering.

In general, the current mainstream MySQL usage scenarios use the InnoDB storage engine, which is why MySQL officially adopted InnoDB as the default storage engine after MySQL 5.5.5.

Five, storage engine settings

1. Specify the storage engine when building the table

CREATE TABLE t1 (i INT) ENGINE = INNODB;

2. Set the default storage engine

InnoDB is the default storage engine after MySQL 5.5.5. If you need to modify the default storage engine, you can modify the value of default-storage-engine in the my.cnf configuration file.

default_storage_engine=NDBCLUSTER;

3. Modify the storage engine of the table

ALTER TABLE t ENGINE = InnoDB;

to sum up

This article mainly introduces the mysql storage engine and the characteristics of each storage engine.
According to the characteristics of each storage engine, point out the applicable usage scenarios.
The core is: the difference between InnoDB and MyISAM, high-frequency questions in interviews.
Note that the granularity of MySQL storage engine control is at the table level , that is, different tables in the same database can be set with different storage engines.

More exciting, follow me.
Legend: Follow the old man to learn java

Guess you like

Origin blog.csdn.net/w1014074794/article/details/108570082