当前位置:网站首页>MySQL advanced part 2: storage engine
MySQL advanced part 2: storage engine
2022-07-05 06:13:00 【Dawnlighttt】
List of articles
Storage engine
Storage engine features
Unlike most databases , MySQL There's a storage engine concept in , We can choose the best storage engine for different storage requirements .
The storage engine is to store data , Index , Update query data and so on . The storage engine is table based , Not library based . So a storage engine can also be called a table type .
Oracle,SqlServer There is only one storage engine for databases .MySQL Provides plug-in storage engine architecture . therefore MySQL There are multiple storage engines , You can use the engine as needed , Or write a storage engine .
MySQL5.0 Supported storage engines include : InnoDB 、MyISAM 、BDB、MEMORY、MERGE、EXAMPLE、NDB Cluster、ARCHIVE、CSV、BLACKHOLE、FEDERATED etc. , among InnoDB and BDB Provide transaction safety watch , Other storage engines are non transactional security tables .
You can specify show engines , To query the storage engine supported by the current database :
If you do not specify a storage engine when creating a new table , Then the system will use the default storage engine ,MySQL5.5 The previous default storage engine was MyISAM,5.5 Then it's changed to InnoDB.
see Mysql Database default storage engine , Instructions :
show variables like '%storage_engine%' ;
Various storage engine features
Here are some common storage engines , And compare the differences between the storage engines , As shown in the following table :
characteristic | InnoDB | MyISAM | MEMORY | MERGE | NDB |
---|---|---|---|---|---|
Storage limits | 64TB | Yes | Yes | No, | Yes |
Transaction security | Support | ||||
Locking mechanism | Row lock ( Suitable for high concurrency ) | Table locks | Table locks | Table locks | Row lock |
B Tree index | Support | Support | Support | Support | Support |
Hash index | Support | ||||
Full-text index | Support (5.6 After the version ) | Support | |||
Cluster index | Support | ||||
Data index | Support | Support | Support | ||
The index buffer | Support | Support | Support | Support | Support |
Data can be compressed | Support | ||||
Space use | high | low | N/A | low | low |
Memory usage | high | low | secondary | low | high |
Batch insertion speed | low | high | high | high | high |
Support foreign keys | Support |
Next, we will focus on the two longest used storage engines : InnoDB、MyISAM , The other two MEMORY、MERGE , Understanding can .
InnoDB
InnoDB The storage engine is Mysql The default storage engine for .InnoDB The storage engine provides with commit 、 Roll back 、 Crash resilience transaction security . But contrast MyISAM Storage engine for ,InnoDB The processing efficiency of writing is poor , And it will take up more disk space to keep data and index .
InnoDB Storage engine is different from other storage engines :
Transaction control
create table goods_innodb(
id int NOT NULL AUTO_INCREMENT,
name varchar(20) NOT NULL,
primary key(id)
)ENGINE=innodb DEFAULT CHARSET=utf8;
start transaction;
insert into goods_innodb(id,name)values(null,'Meta20');
commit;
test , Found in InnoDB There are transactions in ;
Foreign key constraints
MySQL The only storage engine that supports foreign keys is InnoDB , When creating a foreign key , The parent table is required to have a corresponding index , When creating a foreign key, the child table , The corresponding index will also be created automatically .
In the following two tables , country_innodb It's the father's watch , country_id Index for primary key ,city_innodb A watch is a subtable ,country_id The field is foreign key , Corresponding to country_innodb Primary Key country_id .
create table country_innodb(
country_id int NOT NULL AUTO_INCREMENT,
country_name varchar(100) NOT NULL,
primary key(country_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
create table city_innodb(
city_id int NOT NULL AUTO_INCREMENT,
city_name varchar(50) NOT NULL,
country_id int NOT NULL,
primary key(city_id),
key idx_fk_country_id(country_id),
CONSTRAINT `fk_city_country` FOREIGN KEY(country_id) REFERENCES country_innodb(country_id) ON DELETE RESTRICT ON UPDATE CASCADE
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ON DELETE RESTRICT: Delete main table , If there are related records , Don't delete
-- ON UPDATE CASCADE: Update main table , If the child table has associated records , Update sub table records
insert into country_innodb values(null,'China'),(null,'America'),(null,'Japan');
insert into city_innodb values(null,'Xian',1),(null,'NewYork',2),(null,'BeiJing',1);
When the index is created , Can be specified in delete 、 When updating the parent table , The corresponding operation on the sub table , Include RESTRICT、CASCADE、SET NULL and NO ACTION.
RESTRICT and NO ACTION identical , It is limited to the case that the sub table has associated records , The parent table cannot be updated ;
CASCADE Indicates that when the parent table is updated or deleted , Update or delete the record corresponding to the sub table ;
SET NULL When the parent table is updated or deleted , The corresponding fields of the sub table are SET NULL .
For the two tables created above , The foreign key specification of the child table is ON DELETE RESTRICT ON UPDATE CASCADE The way of , When deleting records in the main table , If the sub table has corresponding records , Delete is not allowed , When the main table is updating records , If the sub table has corresponding records , Then the sub table is updated .
The data in the table is shown in the figure below :
Foreign key information can be viewed in the following two ways :
show create table city_innodb ;
Delete country_id by 1 Of country data :
delete from country_innodb where country_id = 1;
Update main table country Table fields country_id :
update country_innodb set country_id = 100 where country_id = 1;
After the update , The data information of the sub table is :
storage
InnoDB There are two ways to store tables and indexes :
①. Use shared table space to store , The table structure of the table created in this way is saved in .frm In file , Data and indexes are stored in innodb_data_home_dir and innodb_data_file_path In the defined tablespace , It can be multiple files .
②. Using multi table space storage , The table structure of the table created in this way still exists .frm In file , But the data and index of each table are stored separately in .ibd in .
MyISAM
MyISAM Unsupported transaction 、 Foreign keys are also not supported , The advantage is the speed of access , There is no requirement for the integrity of the transaction or to SELECT、INSERT Basically, all major applications can use this engine to create tables . There are two more important features :
Unsupported transaction
create table goods_myisam(
id int NOT NULL AUTO_INCREMENT,
name varchar(20) NOT NULL,
primary key(id)
)ENGINE=myisam DEFAULT CHARSET=utf8;
Pass the test , We found that , stay MyISAM In the storage engine , There is no transaction control ;
File storage
Every MyISAM Stored on disk as 3 File , The file name is the same as the table name , But the extended names are :
.frm ( Storage table definition );
.MYD(MYData , Store the data );
.MYI(MYIndex , Storage index );
MEMORY
Memory The storage engine stores the table data in memory . Every MEMORY The table actually corresponds to a disk file , The format is .frm , Only the structure of the table is stored in this file , And its data files , It's all stored in memory , This is conducive to the rapid processing of data , Improve the efficiency of the whole watch .MEMORY Table access of type is very fast , Because his data is stored in memory , And by default HASH Indexes , But once the service is shut down , The data in the table will be lost .
MERGE
MERGE The storage engine is a set of MyISAM Combination of tables , these MyISAM Tables must be exactly the same structure ,MERGE The table itself does not store data , Yes MERGE Types of tables can be queried 、 to update 、 Delete operation , These operations are actually internal MyISAM Table .
about MERGE Insert operation of type table , It's through INSERT_METHOD Clause defines the inserted table , There can be 3 Different values , Use FIRST or LAST Value so that the insert operation is applied to the first or last table accordingly , Do not define this clause or define it as NO, That means you can't do this MERGE Tables perform insert operations .
It can be done to MERGE table DROP operation , But this operation just deletes MERGE The definition of the table , It has no effect on the internal table .
Here's an example of creating and using MERGE Examples of tables :
1) establish 3 A test table order_1990, order_1991, order_all , among order_all It's from the first two tables MERGE surface :
create table order_1990(
order_id int ,
order_money double(10,2),
order_address varchar(50),
primary key (order_id)
)engine = myisam default charset=utf8;
create table order_1991(
order_id int ,
order_money double(10,2),
order_address varchar(50),
primary key (order_id)
)engine = myisam default charset=utf8;
create table order_all(
order_id int ,
order_money double(10,2),
order_address varchar(50),
primary key (order_id)
)engine = merge union = (order_1990,order_1991) INSERT_METHOD=LAST default charset=utf8;
2) Insert records into two tables respectively
order_1990 Data in :
order_1991 Data in :
order_all Data in :
4) Go to order_all Insert a record in , Because in MERGE Table definition ,INSERT_METHOD The choice is LAST, Then the inserted data will be inserted into the last table .
insert into order_all values(100,10000.0,' Xi'an ');
Choice of storage engine
When choosing a storage engine , The appropriate storage engine should be selected according to the characteristics of the application system . For complex applications , You can also select a variety of storage engines to combine according to the actual situation . Here are some common storage engine usage environments .
- InnoDB : yes Mysql The default storage engine for , For transactional applications , Support foreign keys . If the application has higher requirements for transaction integrity , Data consistency is required under concurrent conditions , Data operation except insert and query , It also contains a lot of updates 、 Delete operation , that InnoDB The storage engine is a better choice .InnoDB The storage engine can effectively reduce the lock caused by deletion and update , It also ensures the complete commit and rollback of the transaction , For similar billing system or financial system and other data accuracy requirements of the system ,InnoDB Is the most appropriate choice .
- MyISAM : If the application is based on read operation and insert operation , There are very few update and delete operations , And the integrity of the transaction 、 Concurrency requirements are not very high , So it's very appropriate to choose this storage engine .
- MEMORY: Save all data in RAM in , In the need for fast location records and other similar data environment , Can provide access to a few blocks .MEMORY The drawback is that there is a limit on the size of the table , Too large tables cannot be cached in memory , The second is to ensure that the data in the table can be recovered , After the abnormal termination of the database, the data in the table can be recovered .MEMORY Tables are usually used to update small tables that are less frequent , To quickly get access to results .
- MERGE: Used to equate a series of MyISAM Tables are logically combined , And refer to them as an object .MERGE The advantage of the table is that it can break through to single MyISAM Table size limit , And by distributing different tables on multiple disks , Can effectively improve MERGE Table access efficiency . It's important for storage such as data warehousing VLDB The environment is perfect .
边栏推荐
- Daily question 2013 Detect square
- Daily question 1984 Minimum difference in student scores
- Bit mask of bit operation
- One question per day 1447 Simplest fraction
- Simply sort out the types of sockets
- LeetCode 0108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树
- Leetcode-6109: number of people who know secrets
- Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135
- API related to TCP connection
- [rust notes] 16 input and output (Part 1)
猜你喜欢
MatrixDB v4.5.0 重磅发布,全新推出 MARS2 存储引擎!
leetcode-6110:网格图中递增路径的数目
Appium foundation - use the first demo of appium
Traditional databases are gradually "difficult to adapt", and cloud native databases stand out
Implement a fixed capacity stack
7. Processing the input of multidimensional features
Dynamic planning solution ideas and summary (30000 words)
开源存储这么香,为何我们还要坚持自研?
Introduction to LVS [unfinished (semi-finished products)]
redis发布订阅命令行实现
随机推荐
Transform optimization problems into decision-making problems
SPI details
[rust notes] 16 input and output (Part 2)
LeetCode 0107.二叉树的层序遍历II - 另一种方法
1039 Course List for Student
【Rust 笔记】13-迭代器(中)
Leetcode-556: the next larger element III
redis发布订阅命令行实现
Doing SQL performance optimization is really eye-catching
MIT-6874-Deep Learning in the Life Sciences Week 7
New title of module a of "PanYun Cup" secondary vocational network security skills competition
1.14 - 流水线
[rust notes] 14 set (Part 1)
[rust notes] 14 set (Part 2)
【Rust 笔记】16-输入与输出(上)
Sqlmap tutorial (II) practical skills I
Leetcode-3: Longest substring without repeated characters
MatrixDB v4.5.0 重磅发布,全新推出 MARS2 存储引擎!
“磐云杯”中职网络安全技能大赛A模块新题
SPI 详解