Comparison of mysql commonly used storage engines

MyISAM

It is the default storage engine before MySQL 5.5

Advantage

Fast access

Applicable scene

There is no requirement for the integrity of the transaction, or the application based on select and insert can basically use MYISAM.

It is widely used in Web and data warehouse.

Features

  1. Does not support transactions and foreign keys.
  2. Each myisam is stored as 3 files on the disk, the file name is the same as the table name, and the extensions are:
Extension/suffix effect
.frm Storage table definition
.MYD MYData, store data
.MYI MYIndex, storage index

Data files and index files can be placed in different directories, evenly distributed IO, to speed up access.

Specify the storage path through data directory and index directory when creating the table

  1. The myisam table also supports three different storage formats
    (1) static table (fixed)
    default storage format
    . The fields in the static table are non-variable length fields, and each record has a fixed length. When the table does not contain variable length columns ( VARCHAR, BLOB, or TEXT), use this format.
    Advantages : fast storage, easy to recover from failure
    Disadvantages : occupy more space than dynamic tables, static tables will fill up spaces according to the pre-defined column width when storing data, but these spaces will be removed when accessing.
    Note: If the data itself has There are spaces, and the trailing spaces in the data itself will be removed when returning.
    (2) The dynamic table
    contains variable-length fields, such as varchar, text, blob, if a MyISAM table contains any Variable-length fields (varchar, blob, text), or specify row_format=dynamic when the table is created, then the table uses dynamic format storage.
    Advantages : Small space occupation
    Disadvantages : Frequent update and delete operations will generate fragments and need to be used regularly The optimize table statement or the myisamchk -r command can improve performance, and it is difficult to recover after a failure
    (3) The compressed table
    is created by the myisampack tool and occupies a very small disk space because each record is compressed separately.

InnoDB

The default storage engine after MySQL5.5

Application scenarios:

If the application has high requirements for transaction integrity, and requires data consistency under concurrent conditions, and data operations include read, insert, delete, and update, then InnoDB is the best choice. It is widely used in systems that require high data accuracy, such as billing systems and financial systems.

advantage:

Provides transaction security with Commit, Rollback, and crash recovery capabilities, and supports foreign keys.

Disadvantages:

Compared with MyISAM, the processing efficiency of writing is a little bit worse, and it will take up more disk space to store data and indexes

Features:

  1. Auto-growth column

The automatic growth column of the innoDB table must be an index, if it is a composite index, it must also be the first column of the composite index

The automatic growth column of the MyISAM table can be other columns of the composite index

Set up automatic growth column: when creating a table, add auto_increment after the field

You can use alter table emp auto_increment=n to forcefully set the initial value of the auto-increment column, the default is 1, but the mandatory value is stored in memory, so it will become invalid after the database restarts and needs to be reset

  1. Foreign key constraint

Only innoDB in MySQL storage engine supports foreign key constraints

Note: When a table is referenced by a foreign key created by another table, the index and primary key corresponding to the table must not be deleted

When importing data from multiple tables, if you want to ignore the import sequence before the table, or when performing load data and alter table operations, in order to improve the processing speed, you can temporarily turn off the foreign key constraint, the command is

mysql> set foreign_key_checks=0;

After execution, set it to 1 again to turn on the foreign key.

View foreign key information

show create table 或show table status

  1. Storage method

InnoDB stores data and indexes in two ways: shared table space storage and exclusive table space storage, controlled by the parameter innodb_file_per_table, 0 means shared space, which is also the default, 1 means exclusive space

The table structure (description) of the two methods are saved in the .frm file

Shared table space:

The data and indexes of all tables in each database are saved in a file, which is a file named ibdata1 with a size of 10M in the data directory by default. The storage path can be specified by the parameter innodn_data_file_path=/data/ibdata1:2000M.

advantage:

(1) The table space can be divided into multiple files and placed on different disks to distribute IO and improve performance. innodn_data_file_path=/data/ibdata1:2000M;/db/ibdata2:2000M:autoextend

autoextend means that if the specified 2000M space is used up, the file will grow automatically.

That is to say, using shared space storage, the size of the storage space is not limited by the file size under the file system, but depends on its own limitations. The official document shows that the maximum limit of the table space is 64TB.

(2) Put the table data and table structure together for easy management

Disadvantages: Since all data and indexes are mixed and stored in one file, after a large number of delete operations on a table, a large number of gaps will be generated in the table space

Exclusive table space storage:

Each table has its own independent table space, the structure of the table is still in the .frm file, and there is a file with the suffix .ibd, which saves the data and indexes of this table.

Advantages:
Each table has its own independent table space, which enables a single table to move and
reclaim space in different databases . The drop table will be automatically recycled; after deleting the data, you can also recycle the unused table space through alter table emp engine=innodb. The efficiency and performance will be better

Disadvantages: Since the data of each table is stored in a separate file, it will be limited by the size of the file system

MEMORY

The MEMORY storage engine uses data stored in memory to create tables, and each memory table corresponds to a disk file. The format is .frm

Features:

Because his data is stored in memory, and the HASH index is used by default, its access speed is very fast, and it also causes his shortcomings, that is, once the database service is closed, the data will be lost, and the size of the table limit

The amount of data that can be stored in each memary table is restricted by the max_heap_table_size variable. Its initial value is 16MB. The maximum number of rows in the table can be specified by max_rows when defining the Memary table

Applicable scene:

The code table with infrequent content changes is used as the intermediate result table of statistical operations, which is convenient to take advantage of its fast speed to efficiently analyze the intermediate results.

MERGE

The Merge table is a combination of a set of MyISAM tables. The structure of these myisam tables must be exactly the same. The MERGE table itself has no data. The operation on it is actually the operation on the internal MYISAM table.

The MERGE table keeps two files on the disk, the .frm file stores the definition of the table, and the .mrg file stores the information of the combined table

Application scenarios:

Used to logically combine a series of MyISAM tables and reference them as an object

advantage:

Break through the size limit of a single MyISAM table, and improve access efficiency by distributing different tables on multiple disks

Guess you like

Origin blog.csdn.net/zhangshaohuas/article/details/108948848