MySQL storage engine summary

Preface
What is stored in the database is a table that is inextricably linked, so the quality of the table design will directly affect the entire database. When designing a table, we all pay attention to a question, what storage engine to use. Wait a minute, storage engine? What is a storage engine?

 

What is a storage engine?
A relational database table is a data structure used to store and organize information. A table can be understood as a table consisting of rows and columns, similar to Excel's spreadsheet form. Some tables are simple, some are complex, some are not used to store any long-term data at all, and some are very fast to read, but poor to insert data; and in the actual development process, we may need A variety of tables, different tables, means that different types of data are stored, and there will be differences in data processing, then. For MySQL, it provides many types of storage engines, we can choose different storage engines according to the needs of data processing, so as to maximize the use of the powerful functions of MySQL. This blog post will summarize and analyze the characteristics of each engine, as well as the applicable occasions, and will not be entangled in deeper things. My learning method is to first learn to use it, know how to use it, and then to know how it can be used. The following is a brief introduction to the storage engines supported by MySQL.

 

MyISAM
MyISAM tables are operating system independent, which means they can be easily ported from a Windows server to a Linux server; whenever we build a table for the MyISAM engine, three files are created on the local disk, the file names are show. For example, I have created a tb_Demo table of the MyISAM engine, then the following three files will be generated:
1.tb_demo.frm, which stores table definitions;
2.tb_demo.MYD, which stores data;
3.tb_demo.MYI, which stores indexes.
MyISAM tables cannot handle transactions, which means that tables with transaction processing requirements cannot use the MyISAM storage engine. The MyISAM storage engine is particularly suitable for use in the following situations:
1. Select intensive tables. MyISAM storage engine is very fast when sifting through large amounts of data, which is its most prominent advantage.
2. Insert intensive table. The concurrent insert feature of MyISAM allows simultaneous selection and insertion of data. For example: the MyISAM storage engine is great for managing mail or Web server log data.

 

InnoDB
InnoDB is a robust transactional storage engine, which has been used by many Internet companies and provides a powerful solution for users to operate very large data storage. MySQL version 5.6.13 installed on my computer, InnoDB is the default storage engine. InnoDB also introduces row-level locking and foreign key constraints. Using InnoDB is the most ideal choice in the following situations:
1. Update-intensive tables. The InnoDB storage engine is particularly suitable for handling multiple concurrent update requests.
2. Transactions. The InnoDB storage engine is the standard MySQL storage engine that supports transactions.
3. Automatic disaster recovery. Unlike other storage engines, InnoDB tables can automatically recover from disasters.
4. Foreign key constraints. The only storage engine that supports foreign keys in MySQL is InnoDB.
5. Support to automatically increase the column AUTO_INCREMENT attribute.
In general, if transaction support is required and there is a high frequency of concurrent reads, InnoDB is a good choice.

 

The starting point of MEMORY
using the MySQL Memory storage engine is speed. For the fastest response time, the logical storage medium used is system memory. While storing table data in memory does provide high performance, when the mysqld daemon crashes, all Memory data is lost. Gaining speed also comes with some drawbacks. It requires the data stored in the Memory data table to be in a constant-length format, which means that variable-length data types such as BLOB and TEXT cannot be used. VARCHAR is a variable-length type, but because it is in the MySQL internally treats it as a fixed-length CHAR type, so it can be used.
The Memory storage engine is generally used in the following situations:
1. The target data is small and accessed very frequently. Storing data in memory will cause memory usage. You can control the size of the Memory table through the parameter max_heap_table_size. By setting this parameter, you can limit the maximum size of the Memory table.
2. If the data is temporary and must be available immediately, it can be stored in the memory table.
3. If the data stored in the Memory table is suddenly lost, it will not have a substantial negative impact on the application service.
Memory supports both hash indexes and B-tree indexes. The advantage of B-tree index over hash index is that partial query and wildcard query can be used, and operators such as <, > and >= can also be used to facilitate data mining. Hash indexes are very fast for "equality comparisons", but much slower for "range comparisons", so hash index values ​​are suitable for use in = and <> operators, but not in < or > operators , is also not suitable for use in the order by clause.
The version to use can be specified using the USING clause at table creation time. E.g:

create table users
(
    id smallint unsigned not null auto_increment,
    username varchar(15) not null,
    pwd varchar(15) not null,
    index using hash (username),
    primary key (id)
)engine=memory;

 

The above code creates a table with a hash index on the username field. The following code creates a table using the BTREE index.

create table users
(
    id smallint unsigned not null auto_increment,
    username varchar(15) not null,
    pwd varchar(15) not null,
    index using btree (username),
    primary key (id)
)engine=memory;

 

MERGE
The MERGE storage engine is a combination of a set of MyISAM tables that must be identical in structure, and although its use is not as prominent as other engines, it is useful in some cases. To put it bluntly, the Merge table is an aggregator of several identical MyISAM tables; there is no data in the Merge table, and Merge-type tables can be queried, updated, and deleted. These operations are actually operations on the internal MyISAM table. The usage scenarios of the Merge storage engine.
For information such as server logs, a common storage strategy is to divide the data into many tables, and each name is associated with a specific time point. For example, you can use 12 identical tables to store server log data, each named with the name corresponding to each month. When it is necessary to generate a report based on data from all 12 log tables, it means that a multi-table query needs to be written and updated to reflect the information in these tables. Instead of writing these potentially error-prone queries, it is better to combine these tables and use a single query, and then delete the Merge table without affecting the original data. Deleting the Merge table just deletes the definition of the Merge table and has no effect on the internal tables.

 

ARCHIVE
Archive means archive. After archiving, many advanced functions are no longer supported, and only the most basic functions of insert and query are supported. Before MySQL version 5.5, Archive did not support indexes, but after MySQL 5.5, it began to support indexes. Archive has a good compression mechanism. It uses the zlib compression library, which compresses in real time when records are requested, so it is often used as a repository.
How to choose the right storage engine?
(1) The selection criteria can be divided into:
(2) Whether to support transactions;
(3) Whether to use hot backup;
(4) Crash recovery: whether the crash can be accepted;
(5) Whether foreign key support is required;
and then according to the standard, Select the corresponding storage engine.

 

Summary
This article summarizes several commonly used storage engines. For actual work, it needs to be determined according to the specific situation. It is the best learning method to apply it in combination with actual project examples.

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326312106&siteId=291194637