当前位置:网站首页>MySQL storage engine
MySQL storage engine
2022-07-06 10:10:00 【Eric-x】
Preface
If you want to be right SQL To optimize , Then understanding the storage engine is essential . Thinking about that, write an article about the engine , It's also a review of yourself
List of articles
1、 View storage engine
Before understanding the storage engine , We must know MySQL Which engines are provided to us , adopt :show engines see 
2、 Set the default storage engine of the system
We can check the default storage engine first
show variables like '%storage_engine%';
# or
SELECT @@default_storage_engine;

my MySQL Version of 8.x, You can see that the default engine is InnoDB
How to modify the default storage engine ?
If it is 8.0 And above , If the storage engine of the table is not explicitly specified in the statement of creating the table , That will default to InnoDB As a storage engine for tables .
If it is 5.x, If we want to change the default storage engine of the table , You can write the command line to start the server like this :
SET DEFAULT_STORAGE_ENGINE=MyISAM;
Or modify my.cnf file :
default-storage-engine=MyISAM
# Restart the service
systemctl restart mysqld.service
3、 Set the storage engine for the table
The storage engine is responsible for extracting and writing the data in the table , We can Different tables set different storage engines , In other words, different tables can have different physical storage structures , Different extraction and writing methods .
3.1、 Specify the storage engine when creating the table
If the statement that creates the table does not specify the storage engine of the table , Then the default storage engine will be used InnoDB . If we want to explicitly specify the storage engine of the table , It can be written like this :
CREATE TABLE Table name (
Create table statement ;
) ENGINE = Storage engine name ;
3.2、 Modify the storage engine of the table
If the table has been built , We can also use the following statement to modify the storage engine of the table :
ALTER TABLE Table name ENGINE = Storage engine name ;
For example, let's modify engine_demo_table Table storage engine :
ALTER TABLE engine_demo_table ENGINE = InnoDB;
4、 Engine introduction
good , Here we are. , Because it is completely familiar to everyone SQL Statement first understand the engine , I believe you have a little feeling about the engine , If you haven't felt , No problem , Next, I will only focus on :
4.1、InnoDB engine : Transaction storage engine with foreign key support
MySQL from 3.23.34a Start with InnoDB Storage engine . Greater than or equal to 5.5 after , By default InnoDB engine .
InnoDB yes MySQL Of Default transactional engine , It's designed to handle a lot of short-term (short-lived) Business . It can ensure the complete submission of transactions (Commit) And rollback (Rollback).
In addition to adding and querying , It needs to be updated 、 Delete operation , that , Preference should be given to InnoDB Storage engine .
Unless there's a very special reason to use another storage engine , Otherwise, priority should be given to InnoDB engine .
Data file structure :
(1) Table name .frm Storage table structure
(2) Table name .ibd Store data and indexcontrast MyISAM Storage engine for , InnoDB The processing efficiency of writing is poor , And it will take up more disk space to save data and indexes .
MyISAM Cache index only , Don't cache real data ;InnoDB Not only index but also real data , High memory requirements , And memory size has a decisive impact on performance .
4.2、MyISAM engine : The main non transactional storage engine
- MyISAM There are a lot of features , Include full text index 、 Compress 、 Space function (GIS) etc. , but MyISAM Unsupported transaction 、 Row-level locks 、 Foreign keys , There is no doubt that the defect is Can't recover safely after a crash .( This is also caused by not supporting transactions )
- MySQL5.5 The previous default storage engine
- Advantage is Access to the Fast , There is no requirement for transaction integrity or SELECT、INSERT Mainly applications
- There is additional constant storage for data statistics . so count(*) The query efficiency is very high
- Data file structure :
(1) Table name .frm Storage table structure
(2) Table name .MYD Store the data
(3) Table name .MYI Storage index - Application scenarios : Read only applications or read-only businesses
4.3、Archive engine : For data archiving

4.4、Memory engine : Table in memory
summary :
Memory The logical medium used is Memory , Fast response , however When mysqld When the daemon crashes Data will be lost . in addition , The stored data is required to be in a format with constant data length , such as ,Blob and Text Data of type is not available ( Of variable length ).
Main features :
- Memory meanwhile Support hash (HASH) Indexes and B+ Tree index .
- Memory The watch is at least better than MyISAM Table One order of magnitude fast .(MylSAM Than InnoDB Be quick , however Memory Better than MylSAM fast , Because it is read directly from memory )
- MEMORY The size of the table is limited Of . The size of the table mainly depends on two parameters , Namely max_rows and max_heap_table_size . among ,max_rows You can specify... When creating a table ;max_heap_table_size The default size of is 16MB, It can be expanded as needed .
- Data files are stored separately from index files
- shortcoming : Its data is easy to lose , Short life cycle . Based on this flaw , choice MEMORY You need to be very careful when storing the engine .
Memory Usage scenarios of storage engine :
- The target data is relatively small , And very Frequent visits , Store data in memory , If the data is too large, it will cause out of memory . You can use the parameter max_heap_table_size control Memory The size of the table , Limit Memory The maximum size of the table .
- If The data is provisional , and Must be available immediately obtain , Then you can put it in memory .
- Stored in Memory In the table If the data suddenly If you lose it, it doesn't matter much .
4.5、 Other engines
MySQL The storage engine and :
- Blackhole engine : Discard write operation , The read operation will return empty content
- CSV engine : When storing data , Separate data items with commas
- Federated engine : Access remote tables
- Merge engine : Manage multiple MyISAM A collection of tables made up of tables
- NDB engine :MySQL Cluster specific storage engine
These are not commonly used , But also list , Interested partners can learn about it alone .
MyISAM and InnoDB Comparison of
MySQL5.5 The previous default storage engine was MyISAM,5.5 Then it changed to InnoDB.
MySQL In the same database , Different storage engines can be selected for different tables . And in the real world , The two most used engines are MylSAM and InnoDB 了 . Here's a point to note , Is not to say that InnoDB As MySQL5.5 After that, the default engine must be better than MylSAM The engine is good , There is no saying that InnoDB To replace MyISAM Of , The two are more inclined to a complementary relationship .
| Contrast item | MylSAM | InnoDB |
|---|---|---|
| Foreign keys | I won't support it | Support |
| Business | I won't support it | Support |
| Row table lock | Table locks , Even operating on one record locks the entire table , Not suitable for highly concurrent operations | Row lock , Lock only one line during operation , It doesn't affect anything else , Suitable for high concurrency operations |
| cache | Cache index only , Don't cache real data | Not only index but also real data , High memory requirements , And memory size has a decisive impact on performance |
| Use the self-contained system table | Y | N |
| concerns | performance : Save resources 、 Less consumption 、 Suitable for simple business | Business 、 Write concurrently 、 More resources |
| Default installation | Y | Y |
| By default | N | Y |
summary
If there is anything wrong , I hope you can point out that , thank .
边栏推荐
- MySQL real battle optimization expert 08 production experience: how to observe the machine performance 360 degrees without dead angle in the process of database pressure test?
- Teach you how to write the first MCU program hand in hand
- C miscellaneous shallow copy and deep copy
- MySQL combat optimization expert 09 production experience: how to deploy a monitoring system for a database in a production environment?
- CANoe不能自动识别串口号?那就封装个DLL让它必须行
- [one click] it only takes 30s to build a blog with one click - QT graphical tool
- PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
- If someone asks you about the consistency of database cache, send this article directly to him
- If a university wants to choose to study automation, what books can it read in advance?
- 软件测试工程师发展规划路线
猜你喜欢

MySQL real battle optimization expert 11 starts with the addition, deletion and modification of data. Review the status of buffer pool in the database
![[after reading the series] how to realize app automation without programming (automatically start Kwai APP)](/img/e1/bad9cfa70d3c533cfaddeee40b96f1.jpg)
[after reading the series] how to realize app automation without programming (automatically start Kwai APP)

The 32 year old programmer left and was admitted by pinduoduo and foreign enterprises. After drying out his annual salary, he sighed: it's hard to choose

嵌入式開發中的防禦性C語言編程

Combined search /dfs solution - leetcode daily question - number of 1020 enclaves

Southwest University: Hu hang - Analysis on learning behavior and learning effect

MySQL实战优化高手04 借着更新语句在InnoDB存储引擎中的执行流程,聊聊binlog是什么?

CANoe下载地址以及CAN Demo 16的下载与激活,并附录所有CANoe软件版本

CAPL 脚本打印函数 write ,writeEx ,writeLineEx ,writeToLog ,writeToLogEx ,writeDbgLevel 你真的分的清楚什么情况下用哪个吗?

112 pages of mathematical knowledge sorting! Machine learning - a review of fundamentals of mathematics pptx
随机推荐
17 医疗挂号系统_【微信支付】
[one click] it only takes 30s to build a blog with one click - QT graphical tool
Southwest University: Hu hang - Analysis on learning behavior and learning effect
MySQL實戰優化高手08 生產經驗:在數據庫的壓測過程中,如何360度無死角觀察機器性能?
[NLP] bert4vec: a sentence vector generation tool based on pre training
MySQL实战优化高手10 生产经验:如何为数据库的监控系统部署可视化报表系统?
在CANoe中通过Panel面板控制Test Module 运行(初级)
C杂讲 文件 续讲
软件测试工程师必备之软技能:结构化思维
Sichuan cloud education and double teacher model
宝塔的安装和flask项目部署
Control the operation of the test module through the panel in canoe (primary)
Canoe cannot automatically identify serial port number? Then encapsulate a DLL so that it must work
South China Technology stack cnn+bilstm+attention
CAPL script printing functions write, writeex, writelineex, writetolog, writetologex, writedbglevel do you really know which one to use under what circumstances?
MySQL的存储引擎
Embedded development is much more difficult than MCU? Talk about SCM and embedded development and design experience
AI的路线和资源
MySQL实战优化高手03 用一次数据更新流程,初步了解InnoDB存储引擎的架构设计
Function description of shell command parser