Six, mysql optimization technology - storage engine selection

Essentially, a database is a collection of data. Specifically, on a computer system, a database can be a collection of files on disk or a collection of in-memory data.

The common MySql login database is actually a database management system. They can store data and provide functions to query and update data in the database, etc., that is, to help us manage data files. How to build indexes, how to update, query data and other technologies. 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 that stores and manipulates this table).

In mysql, you can use the show engines; command to view the types of storage engines supported by the server


It can be seen that the default storage engine of the current server is InnoDB, the default of mysql5.5 and later versions is the InnoDB engine, and the default of the previous version is the MyISAM storage engine.

 

View the storage engine used by the table


The storage engine used by the table is MyISAM, and the character set is utf8

The storage engine of the table can also be viewed using the show table status command


 

The storage engine can be specified when creating a table, such as

CREATE TABLE TABLE_NAME (
    COLUMN1 ...
) engine = engine_name;

 If no storage engine is specified when the table is created, the default storage engine is used

You can also modify the storage engine after the table is built, such as

ALTER TABLE TABLE_NAME engine = engine_name;

 

Comparison of storage engines

 

Among them, the most commonly used storage engines are MyISAM and InnoDB

MyISAM

The MyISAM storage engine has fast access speed, but does not support transactions, foreign key constraints, only table locks, and databases that do not require transaction integrity or query-based databases can choose this storage engine. The MyISAM storage engine will generate three files in the data directory (the default is the data directory in the installation directory, which can be specified during installation), .frm (storage table definition), MYD (MYData, storage data), MYI (MYIndex, storage index)

 

InnoDB

InnoDB存储引擎提供了具有提交、回滚和崩溃恢复能力的事务安全,支持行锁,但是相较于MyISAM存储引擎,InnoDB写的处理效率差一些,因为需要记录事务日志,并且会占用更多的磁盘空间以保留数据和索引。InnoDB是唯一支持外键的存储引擎,可以使用set foreign_key_checks=0;临时关闭外键约束,

set foreign_key_checks=1;打开约束。默认情况下,InnoDB存储引擎会在数据目录下生成以下文件


其中ibdata1文件存储数据和索引信息,而下面两个文件存储事务信息;ibdata1是所有表共享的,mysql还能通过设置innodb_file_per_table = 1使得每张表有自己独立的存储空间


test表的存储引擎为InnoDB


到数据目录下查看文件格式


data目录下每个数据库会有一个目录,dept、emp、salgrade表时MyISAM存储引擎,而student、test表是InnoDB存储引擎

 

此外还有一种存储引擎,BLACKHOLE,黑洞存储引擎,正如show engines;命令描述的,任何写入的东西都会消失,这种存储引擎在mysql的主从复制中架构中可能会使用到。

Guess you like

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