Home  >  Article  >  Database  >  A detailed introduction to the four commonly used storage engines in Mysql and how to choose correctly

A detailed introduction to the four commonly used storage engines in Mysql and how to choose correctly

王林
王林forward
2019-08-24 18:07:132564browse

There are two main categories of MySQL storage engines:

1. Transaction security table: InnoDB, BDB.

2. Non-transaction safe table: MyISAM, MEMORY, MERGE, EXAMPLE , NDB Cluster, ARCHIVE, CSV, BLACKHOLE, FEDERATED, etc.

MySQL default The storage engine is MyISAM (default is InnoDB in version 5.7).

Set the parameters of the default storage engine in the configuration file: default-table-type.

Query the storage engine supported by the current database:

show engines;
show variables like 'have%';

View the current default storage engine:

show variables like '%table_type%';

Specify the storage engine when creating a new table:

create table(...) engine=MyISAM;

The following is a detailed introduction to four commonly used storage engines: MyISAM, InnoDB, MEMORY and MERGE.

1. MyISAM

1. Data files:

The MyISAM data table is stored on the disk as 3 files, and their file names are the same as the table names. The same, the extensions are:

(1).frm: storage data table structure definition.

(2).MYD: Store table data.

(3).MYI: Storage table index.

Among them, data files and index files can be placed in different directories to evenly distribute IO and obtain faster speeds. Specify the paths of index files and data files, which need to be specified through the data directory and index directory statements when creating the table. (The file path needs to be an absolute path and have access permissions)

The MyISAM type table may be damaged for various reasons. The damaged table may not be accessible, and a prompt will be prompted that it needs to be repaired or accessed. Returns incorrect results. You can use the check table statement to check the health of the MyISAM table, and use the repair table statement to repair the damaged MyISAM table.

2. Storage format:

(1) Static table (default): fields are non-variable length (each record is fixed length ). Storage is very fast, easy to cache, and easy to recover from failures; it usually takes up more space than dynamic tables.

(2) Dynamic table: It takes up relatively little space, but frequent updates and deletions of records will cause fragmentation. You need to regularly execute the optimize table or myisamchk -r command to improve performance, and restore comparison when a failure occurs. difficulty.

(3) Compressed table: Created using the myisampack tool, it takes up very little disk space. Because each record is compressed individually, there is very little access overhead.

When the static table data is stored, spaces will be filled in according to the column width definition, and these spaces will be removed before returning the data to the application. If there are spaces after the content that needs to be saved, they will also be removed when the results are returned. (In fact, it is the behavior of the data type char. If there is this data type in the dynamic table, this problem will also occur)

(Static tables and dynamic tables are automatically selected based on the type of the column being used.)

3. Advantages and Disadvantages:

(1) Advantages: Fast access.

(2) Does not support transactions or foreign keys.

4. Applicable situation:

If the application is mainly based on read operations and insert operations, there are only a few update and delete operations, and the integrity of the transaction is , the concurrency requirements are not very high, so choosing this storage engine is very suitable. MyISAM is one of the most commonly used storage engines in Web, data warehouse and other application environments.

2. InnoDB

#1. Storage method:

InnoDB has the following two storage tables and indexes Method:

(1) Use shared table space storage: The table structure created in this way is saved in the .frm file, and the data and indexes are saved in innodb_data_home_dir and innodb_data_file_pathThe defined table space can contain multiple files.

(2) Use multi-table space storage: The table structure created in this way is still saved in the .frm file, but the data and indexes of each table are saved separately in the .idb file. If it is a partitioned table, each partition corresponds to a separate .idb file. The file name is "table name partition name". You can specify the location of the data file of each partition when creating the partition to evenly distribute the IO of the table. Distributed across multiple disks.

To use the storage method of multiple table spaces, you need to set the parameter innodb_file_per_table and restart the server before it can take effect, and it will only take effect for newly created tables. There is no size limit for data files in multiple table spaces. There is no need to set the initial size, nor the maximum limit, extended size and other parameters of the file. Even in the storage mode of multiple table spaces, shared table spaces are still necessary. InnoDB places the internal data dictionary and work log in this file, so it is not possible to directly copy the .idb file when backing up tables that use the multi-table space feature. , you can restore the data backup to the database through the command:

ALTER TABLE tbl_name DISCARD TABLESPACE;
ALTER TABLE tbl_name IMPORT TABLESPACE;

However, this can only be restored to the database where the table originally resides. If you need to restore to other databases, you need to use mysqldump and mysqlimport to achieve it.

2. Data files:

The data files of InnoDB are determined by the storage method of the table.

(1)共享表空间文件:由参数innodb_data_home_dirinnodb_data_file_path定义,用于存放数据词典和日志等。

(2).frm:存放表结构定义。

(3).idb:使用多表空间存储方式时,用于存放表数据和索引,若使用共享表空间存储则无此文件。

3. 外键约束:

InnoDB是MySQL唯一支持外键约束的引擎。外键约束可以让数据库自己通过外键保证数据的完整性和一致性,但是引入外键会使速度和性能下降。在创建外键的时候,要求父表必须有对应的索引,子表在创建外键的时候也会自动创建对应的索引。

外键约束使用示例:

CREATE TABLE `dep` (
 `id` smallint(6) NOT NULL AUTO_INCREMENT,
 `name` varchar(20) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `emp` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(20) DEFAULT NULL,
 `dep_id` smallint(6) NOT NULL,
 PRIMARY KEY (`id`),
 KEY `idx_fk_dep_id` (`dep_id`),
 CONSTRAINT `fk_emp_dep` FOREIGN KEY (`dep_id`) REFERENCES `dep` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

KEY :定义索引约束名称。

CONSTRAINT:定义外键约束名称。(在数据库中应是唯一的,若不指定系统会自动生成一个约束名)

ON:指定父表操作对子表的影响(不定义默认采用restrict)。

Restrictno action:在子表有相关记录的情况下父表不能更新或删除。

Cascade:在父表更新或删除时,同时更新或删除子表对应的记录。

Set null:在父表更新或删除的时候,子表的对应字段被设置为null。

当某个表被其他表创建了外键参照,那么这个表的对应索引或者主键禁止被删除。在导入多个表的数据时,如果需要忽略表的导入顺序,可以暂时关闭外键的检查;在执行load data和alter table操作的时候,也可以通过暂时关闭外键约束来加快处理的速度。

关闭命令:

set foreign_key_checks=0;

开启命令:

set foreign_key_checks=1;

4. 优劣势:

(1)优势:提供了具有提交、回滚和崩溃恢复能力的事务安全。

(2)劣势:相比MyISAM,InnoDB写的处理效率差一些,并且会占用更多的磁盘空间以保留数据和索引。

5. 适用情况:

如果应用对事务的完整性有比较高的要求,在并发条件下要求数据的一致性,数据操作除了插入和查询以外,还包括很多的更新、删除操作,那么InnoDB 存储引擎应该是比较合适的选择。InnoDB 存储引擎除了有效地降低由于删除和更新导致的锁定,还可以确保事务的完整提交和回滚,对于类似计费系统或者财务系统等对数据准确性要求比较高的系统,InnoDB 都是合适的选择。

三、MEMORY

1. 数据文件:

每个MEMORY表只对应一个.frm磁盘文件,用于存储表的结构定义,表数据存放在内存中。默认使用HASH索引,而不是BTREE索引。

2. 优劣势:

(1)优势:访问速度非常快,因为数据是存在内存中的。

(2)劣势:一旦服务关闭,表中的数据就会丢失;对表的大小有限制。

3. 适用情况:

Memory存储引擎主要用在那些内容变化不频繁的代码表,或者作为统计操作的中间结果表,便于高效地对中间结果进行分析并得到最终的统计结果。

四、MERGE

1. 引擎原理:

Merge存储引擎是一组MyISAM表的组合,这些MyISAM表必须结构完全相同,merge表本身并没有数据,对merge类型的表可以进行查询、更新、删除的操作,这些操作实际上是对内部的实际的MyISAM表进行的。

通过insert_method子句定义merge表的插入操作:使用first或last可以使插入操作被相应地作用在第一或最后一个表上,不定义或定义为No表示不能对这个merge表进行插入操作。对merge表进行drop操作只是删除了merge的定义,对内部的表没有任何影响。

2. 数据文件:

(1).frm:存储表定义。

(2).MRG:存储组合表的信息,包括merge表由哪些表组成、插入新数据时的依据。可以通过修改.mrg文件来修改merge表,但是修改后要通过flush tables刷新。

3. 使用示例:

CREATE TABLE `m1` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(20) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `m2` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(20) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `m` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(20) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MRG_MyISAM DEFAULT CHARSET=utf8 INSERT_METHOD=LAST UNION=(`m1`,`m2`);

4. 适用情况:

用于将一系列等同的MyISAM 表以逻辑方式组合在一起,并作为一个对象引用它们。MERGE 表的优点在于可以突破对单个MyISAM 表大小的限制,并且通过将不同的表分布在多个磁盘上,可以有效地改善MERGE 表的访问效率。这对于诸如数据仓储等VLDB环境十分适合。

以上内容为大家详细介绍了四个常用数据库存储引擎,更多相关问题请访问PHP中文网:Mysql视频教程

The above is the detailed content of A detailed introduction to the four commonly used storage engines in Mysql and how to choose correctly. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete