Detailed explanation of Mysql storage engine


1. Introduction to Storage Engine

A relational database table is a data structure used to store and organize information. A table can be understood as a table composed of rows and columns.
Due to the different types of tables, we may need a variety of tables in the actual development process. Different tables mean that different types of data are stored, and there will be differences in data processing.
For Mysql , it Provides many types of storage engines. The
storage engine is simply how to store data, how to index the stored data, and how to update and query data.
Because the storage of data in a relational database is stored in the form of a table, the storage engine can also be called a table type (that is, the type of storage and operation of this table)


2. MyISAM storage engine

MyISAM is based on the ISAM storage engine and extends it. It is one of the most commonly used storage engines in the Web , data warehousing, and other application environments. MyISAM has high insertion and query speed, but does not support transactions .

The main features of MyISAM are:

  1. Large files (up to 63-bit file length) are supported on file systems and operating systems that support large files.
  2. When deleting and updating and inserting operations are mixed, dynamically sized rows produce less fragmentation. This is done automatically by merging adjacent deleted blocks, and if the next block is deleted, extending to the next block.
  3. The maximum number of indexes per MyISAM table is 64, which can be changed by recompiling. The maximum number of columns per index is 16.
  4. NULL is allowed in the indexed column, this value occupies 0~1 bytes of each key.
  5. Data files and index files can be placed in different directories (InnoDB is placed in one directory)

Static MyISAM

If the length of each data column in the data table is fixed in advance, the server will automatically select this table type.
Because the space occupied by each record in the data table is the same, the efficiency of this table access and update is very high.
When data is damaged, recovery is easier.

Dynamic MyISAM

If the data appears in the table varchar, xxxtextor xxxxBLOBfield, the server will automatically select that table type.
Compared with static MyISAM, this kind of table storage space is relatively small, but because the length of each record is different, the data in the data table may be stored discretely in the memory after multiple modification of the data, which will lead to a decrease in execution efficiency. At the same time, there may be many fragments in the memory.
Therefore, this type of table should often optimize tablebe defragmented with commands or optimization tools.

Compress MyISAM

Both types of tables mentioned above can be compressed with the Myisamchk tool.
This type of table further reduces the storage occupied.
But this kind of table cannot be modified after compression.
In addition, because the data is compressed, this kind of table must be decompressed first when it is read.

No matter what kind of MyISAM table, it currently does not support transactions, row-level locks, and foreign key constraints.
MyISAM means that it is independent of the operating system, which means that it can be easily transferred from the windows server to the linux server;
whenever we create a table of the MyISAM engine, three files will be created on the local disk, and the file name is the table name .

E.g:

I create a th_Demo table of MyISAM engine, then the following three files will be generated

  1. tb_demo.frm, Storage table definition;
  2. tb_demo.MYD,Storing data;
  3. tb_demo.MYI, Storage index;

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:

  • Choose intensive tables: The
    MyISAM storage engine is very fast when filtering large amounts of data, which is its most prominent advantage.
  • Insert intensive tables: The concurrent insert feature of
    MyISAM allows data to be selected and inserted at the same time. For example: MyISAM storage engine is very suitable for managing mail or web server log data.

3. InnoDB storage engine

InnoDB is a robust transactional storage engine MySQL version 5.6 and later InnoDB is used as the default storage engine.
The InnoDB table type can be seen as a further update to MyISAM , which provides the functions of transactions, row-level locking mechanisms, and foreign key constraints.
InnoDB also introduced row-level locking and foreign key constraints.

The main features of InnoDB are:

  1. InnoDB provides MySQL with a transaction security (ACID compatible) storage engine with commit, rollback, and crash recovery capabilities. InnoDB is locked at the row level and also SELECTprovides an Oracle - like non-locking read in the statement . These features increase multi-user deployment and performance. In SQL query, you can freely mix InnoDB table types with other MySQL table types, even in the same query.
  2. InnoDB is designed for maximum performance with huge data volumes. Its CPU efficiency may be unmatched by any other disk-based relational database engine lock.
  3. The InnoDB storage engine is fully integrated with the MySQL server. The InnoDB storage engine maintains its own buffer pool for caching data and indexes in main memory. InnoDB stores its tables and indexes in a logical table space, which can contain several files (or raw disk files). This is different from MyISAM tables, for example , each table in the MyISAM table is stored in a separate file. InnoDB tables can be of any size, even on operating systems where the file size is limited to 2GB.
  4. InnoDB supports foreign key integrity constraints. When storing data in a table, the storage of each table is stored in the order of the primary key. If the primary key is not specified when the table is defined, InnoDB will generate a 6-byte ROWID for each row , and Use this as the primary key.

Generally used in the following situations, using the InnoDB storage engine is the most ideal choice:

  1. Update intensive tables. The InnoDB storage engine is particularly suitable for handling multiple concurrent update requests.
  2. Affairs. The InnoDB storage engine is a 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 MySQL supports foreign keys is InnoDB .
  5. Support automatically increase column AUTO_INCREMENT attribute.

4. Memory storage engine

The starting point for using the MySQL Memory storage engine is speed. In order to get the fastest response time, the logical storage medium used is system memory. Although storing table data in memory does provide high performance, when the MySQL daemon crashes, all memory data will be lost. While gaining speed, it also brings some drawbacks. It requires the data stored in the Memory data table to use a format with a constant length, 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 MySQL internally regards it as a CHAR type with a fixed length , so it can be used.

The main features of MEMORY are:

  1. Each table of the Memory table can have up to 32 indexes, each index has 16 columns, and a maximum key length of 500 bytes.
  2. The Memory storage engine implements the epitome of HASH and BTREE .
  3. There can be non-unique key values in a Memory table.
  4. The Memory table uses a fixed record length format.
  5. Memory does not support BLOB or TEXT columns.
  6. Memory supports AUTO_INCREMENT columns and indexes on columns that can contain NULL values.
  7. The Memory table is shared among all clients (just like any other non- TEMPORARY table).
  8. The memory of the Memory table is stored in the memory. The memory is shared between the Memory table and the internal table created when the server is idle during query processing.
  9. When the contents of the Memory table are no longer needed , to release the memory used by the Memory table, you should execute DELETE FROMOR TRUNCATE TABLE, or delete the entire table (use DROP TABLE)

Generally, in the following situations, using the Memory storage engine is the most ideal choice:

  1. The target data is small and is accessed very frequently. Storing data in the memory will cause the use of memory. The size of the Memory table can be max_heap_table_sizecontrolled by parameters . Setting this parameter can limit the maximum size of the Memory table.
  2. If the data is temporary and the requirement must be immediately available, it can be stored in a memory table.
  3. If the data stored in the Memory table is suddenly lost, it will not have a substantial negative impact on application services. Memory supports both hash index and B-tree index. 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 index "equality compare" very fast, but on the "Range Comparison" speed is much slower, and therefore suitable for use in the hash index =and the <>operator, it is not suitable for the operator, the use is also not suitable for In the order byclause.

5. Archive storage engine

Archive means archiving. After archiving, many advanced functions are no longer supported, and only the most basic insertion and query functions are supported. Prior to MySQL 5.5 , Archive did not support indexes, but it began to support indexes in MySQL 5.5 or later. Archive has a good compression mechanism. It uses the zlib compression library and compresses in real time when the record is requested, so it is often used as a warehouse.


6. Merge storage engine

The Merge storage engine is a combination of a set of MyISAM tables. The structure of these MyISAM tables must be exactly the same. Although its use is not as prominent as other engines, it is very useful in some cases. Plainly, Merge table is one of several identical MyISAM table aggregator; Merge table and there is no data on the Merge type of table can query, update, delete operations, which are actually on the inside of MyISAM operating table. Usage scenarios of the Merge storage engine. For server log information, a commonly used storage strategy is to divide the data into many tables, and each name is related to a specific time. For example: 12 identical tables can be used to store server log data, and each table is named with a name corresponding to each month. When it is necessary to generate reports based on the data of all 12 log tables, this means that multiple table queries need to be written and updated to reflect the information in these tables. Instead of writing these queries that may cause errors, it is better to merge these tables to use a query, and then delete the Merge table without affecting the original data. Deleting the Merge table only deletes the definition of the Merge table and has no effect on the internal tables.


7. Summary

Different storage engines have their own characteristics to meet different needs, as shown in the following table:

Features MyISAM Memory InnoDB Archive
Storage limit 256TB RAM 64TB None
Support affairs No No Yes No
Support full-text index Yes No No No
Support number index Yes Yes Yes No
Support hash index No Yes No No
Support data caching No N/A Yes No
Support foreign keys No No Yes No

InnoDB : If you want to provide transaction security (ACID compatible) capabilities such as commit, rollback, and crash recovery capabilities, and require concurrency control, InnoDB is a good choice

The difference between InnoDB and MyISAM :

  1. InnoDB supports things, but MyISAM does not support things.
  2. InnoDB supports row-level locks, while MyISAM supports table-level locks.
  3. InnoDB supports MVCC, but MyISAM does not.
  4. . InnoDB supports foreign keys, but MyISAM does not.
  5. InnoDB does not support full-text indexing, while MyISAM does .

MyISAM : If the data table is mainly used to insert and query records, the MyISAM (but does not support transactions ) engine can provide higher processing efficiency

Memory : If the data is only temporarily stored, the amount of data is not large, and high data security is not required, you can choose the Memory engine that stores the data in memory . MySQL uses this engine as a temporary table to store the intermediate results of the query. The data processing speed is very fast but the security is not high.

Archive : If you only have a INSERTsum SELECToperation, you can choose Archive . Archive supports highly concurrent insert operations, but it is not transaction safe. Archive is very suitable for storing archived data, such as recording log information can use Archive .

Which engine to use requires a flexible choice. Multiple tables in a database can use different engines to meet various performance and actual needs. Using a suitable storage engine will improve the performance of the entire database.


Guess you like

Origin blog.csdn.net/baidu_41847368/article/details/114632039