当前位置:网站首页>What is the storage engine and the three common database storage engines for MySQL
What is the storage engine and the three common database storage engines for MySQL
2022-06-25 04:28:00 【Life is sweet and good luck is good】
Reprint : What is a storage engine (Save Engines)?
The storage engine is how to store data 、 How to index and update stored data 、 Query data and other technologies . Because in a relational database, data is stored in the form of tables , So a storage engine can also be called a table type ( That is, the type of storage and operation of this table ).
stay Oracle and SQL Server There is only one storage engine in the database , All data storage management mechanisms are the same . and MySql The database provides a variety of storage engines . Users can choose different storage engines for data tables according to different requirements , Users can also write their own storage engine according to their own needs .
1、MySql What are the storage engines in ?
- MyISAM: This kind of engine is mysql The first . This kind of engine can be divided into static state MyISAM、 dynamic MyISAM And compression MyISAM Three , Whatever it is MyISAM surface , At present, it doesn't support transactions , The function of row level lock and foreign key constraint .
- MyISAM Merge engine : This type is MyISAM A variant of the type . Merging tables is to combine several of the same MyISAM Tables merged into a virtual table . It is often used in log and data warehouse .
- InnoDB:InnoDB A table type can be seen as a pair of MyISAM Further update products , It provides transactions 、 The function of row level lock mechanism and foreign key constraint , Also present MySQL The default storage engine .
- Memory(heap): This type of data table only exists in memory . It uses hash index , So data access is very fast . Because it's in memory , So this type is often used in temporary tables .
- archive: This type only supports select and insert sentence , And it doesn't support indexing . It is often used in logging and aggregation analysis .
Of course MySql More than the above table types are supported . Let's introduce how to view and set the data table type .
2、 Storage engine operations
Look at the storage engines that the database can support , use show engines; The command can display the storage engines supported by the current database :
-- Inquire about MySQL Supported storage engines
SHOW ENGINES\G;
-- Query results ( Omitted results )
***************************[ 2. row ]***************************
Engine | MEMORY
Support | YES
Comment | Hash based, stored in memory, useful for temporary tables
Transactions | NO
XA | NO
Savepoints | NO
***************************[ 5. row ]***************************
Engine | MyISAM
Support | YES
Comment | MyISAM storage engine
Transactions | NO
XA | NO
Savepoints | NO
........
MySQL from 5.0 At first, the default storage engine is InnoDB, This can be done through the system variable default_storage_engine
Query the default storage engine of the system .
mysql [email protected]:sakila> show variables like 'default_storage_engine';
+------------------------+--------+
| Variable_name | Value |
+------------------------+--------+
| default_storage_engine | InnoDB |
+------------------------+--------+
The storage engine is table based , So you can specify the storage engine at the time of creation .
-- Specify the storage engine when creating the table , The default is InnoDB
CREATE TABLE test_table(
id int primary key auto_increment,
name varchar(128) NOT NULL
)ENGINE = MyISAM;
Reprint :MySQL - Three common database storage engines
Let's take a look at some of the common engines .
1.InnoDB Storage engine
InnoDB Is the preferred engine for transactional databases , Support transaction security (ACID), Other storage engines are non transactional security tables , Support row locking and foreign keys ,MySQL5.5 In the future, it will be used by default InnoDB Storage engine .
InnoDB The main features
by MySQL Provided with commit 、 Transaction security of rollback and crash recovery capability (ACID compatible ) Storage engine .InnoDB Locked at line level and also at SELECT Statement provides a similar Oracle Non locked reading . These capabilities add to multi-user deployment and performance . stay SQL Querying , Be free to InnoDB Types of watches and other MySQL The table types are mixed , Even in the same query can be mixed .
InnoDB The auto growing columns of the table can be inserted manually , But if it's empty or 0, The actual insertion is automatically increased to the value . Can pass "ALTER TABLE...AUTO_INCREMENT=n;" Statement to force the start value of the autogrow value , The default is 1, But the force to the default value is stored in memory , This value will be lost when the database is restarted . have access to LAST_INSERT_ID() Query the value used by the current thread to insert the record last . If you insert more than one record at a time , Then the auto growth value used by the first record is returned .
about InnoDB surface , Autogrow columns must be indexes . If it's a composite index , It must also be the first column of the composite index , But for MyISAM surface , Autogrow columns can be other columns of a composite index , After inserting the record like this , The auto growing columns are sorted and incremented according to the combined index .
MySQL The only storage engine that supports foreign keys is InnoDB, When creating a foreign key , The parent table must have a corresponding index , When creating a foreign key, the sub table will also automatically create the corresponding index . When creating an index , 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. among restrict and no action identical , It refers to the restriction when the sub tables are related , The parent table cannot be updated ;casecade 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 fields corresponding to the sub table are set null. When a table is referenced by a foreign key created by another table , Then the index or primary key corresponding to the table is forbidden to be deleted .
have access to set foreign_key_checks=0; Temporarily turn off foreign key constraints ,setforeign_key_checks=1; Open constraints .InnoDB The storage engine maintains its own buffer pool for caching data and indexes in main memory .InnoDB Put its tables and indexes in a logical table space , A table space can contain several files ( Or the original disk file ). This is related to MyISAM The watch is different , For example MyISAM Each table in the table is stored in a separate file .InnoDB A watch can be any size , Even when the file size is limited to 2GB On the operating system of .
InnoDB Support for foreign key integrity constraints , When storing data in a table , Each table is stored in primary key order , If the primary key is not specified when the table is defined ,InnoDB A... Will be generated for each line 6 Bytes of ROWID, And use this as the primary key .
Use InnoDB Storage engine MySQL A file named... Will be created in the data directory ibdata1 Of 10MB Automatic extended data file size , And two named ib_logfile0 and ib_logfile1 Of 5MB The size of the log file
2.MyISAM Storage engine
MyISAM be based on ISAM Storage engine , And expand it . It's in Web、 One of the most commonly used storage engines in data warehousing and other application environments .MyISAM Has a higher insert 、 Query speed , Transaction is not supported , Foreign key not supported .
MyISAM The main features :
Supported by large file systems and operating systems .
When you mix delete with update and insert , Dynamic sized rows produce less fragmentation . This is done by merging adjacent deleted blocks , If the next block is deleted , Expand to the next piece of autocomplete .
Every MyISAM The maximum number of indexes in the table is 64, This can be changed by recompiling . The maximum number of columns per index is 16.
The maximum bond length is 1000 byte , This can also be changed by compiling , For keys longer than 250 Byte case , One is more than 1024 Byte keys will be used .
BLOB and TEXT Columns can be indexed .
NULL Allowed in indexed columns , This value takes up... Of each key 0~1 Bytes .
All numeric key values are stored in high byte priority to allow a higher index compression .
Every MyISAM All types of tables have a AUTOINCREMENT Internal columns of , When INSERT and UPDATE This column is updated during the operation , meanwhile AUTOINCREMENT The columns will be refreshed . So ,MyISAM Of the type table AUTOINCREMENT Column update ratio InnoDB Type of AUTOINCREMENT faster .
Data files and index files can be placed in different directories , Average distribution IO, Get faster . To specify the path of the data file and index file , You need to create a table through DATA DIRECTORY and INDEX DIRECTORY The statement specifies , File paths need to use absolute paths .
Every MyISAM Every watch has a sign , Server or myisamchk The program is checking MyISAM This flag is set when the data table is created .MyISAM The table also has a flag to indicate whether the data table has been closed normally since it was last used . If the server thinks it's down or crashed , This flag can be used to determine whether the data table needs to be checked and repaired . If you want to make this check automatic , You can use it when you start the server --myisam-recover The phenomenon . This allows the server to open one at a time MyISAM Data table is the mark of automatically checking data table and repairing it if necessary .MyISAM Tables of type can be corrupted , have access to CHECK TABLE Statement to check MyISAM Table health , And use REPAIR TABLE Statement to fix a corruption to MyISAM surface .
Each character column can have a different character set .
Yes VARCHAR The table can be fixed or dynamic record length .
VARCHAR and CHAR Columns can be up to 64KB.
Use MyISAM The engine creates the database , Will produce 3 File . The name of the file starts with the name of the table , File type at extension :frm File storage table definition 、 The extension of the data file is .MYD(MYData)、 When indexing file extensions .MYI(MYIndex).MyISAM Table support 3 Different storage formats : static state ( Fixed length ) surface , Dynamic table , Compression meter .
Static tables are the default storage format . Fields in static tables are all non variable length fields , So every record is a fixed length , The advantage of this storage method is that the storage is very fast , Easy to cache , It's easy to recover in case of failure ; The disadvantage is that it usually takes up more space than a dynamic table . The static table will complement the space according to the width defined by the column when storing data , But you don't get these spaces when you visit , These spaces have been removed before being returned to the application . And also notice that : In some cases, you may need to return the space after the field , When this format is used, the space after it will be automatically processed .
Dynamic tables contain variable length fields , Records are not fixed length , The advantage of this storage is that it takes up less space , However, frequent updating and deletion of records will cause fragmentation , It needs to be carried out on a regular basis OPTIMIZE TABLE Sentence or myisamchk -r Command to improve performance , And it's relatively difficult to recover in case of failure .
The compressed table consists of myisamchk Tool creation , Take up a very small space , Because each record is compressed individually , So there's only a very small cost of access .
3.MEMORY Storage engine
MEMORY The storage engine stores the data in the table in memory , Provides quick access to queries and references to other table data .
MEMORY The main features :
MEMORY A watch can have as many as 32 An index , Every index 16 Column , as well as 500 The maximum key length of bytes .
Can be in a MEMORY There is not only key value in the table .
MEMORY Support AUTO_INCREMENT Columns and pairs can contain NULL Index of the column of the value .
MEMORY Tables are shared between all clients ( Like anything else TEMPORARY surface ).
MEMORY Table memory is stored in memory , Memory is MEMORY Tables and servers are idle during query processing , Internal table share created .
By default ,MEMORY Tables use hash indexes , Use this index to “ Equal comparison ” Very fast , But yes. “ Range comparison ” It's much slower . therefore , Hash index values are suitable for "=" and "<=>" In the operator of , Not suitable for use in "<" or ">" In the operator , It's also not suitable for order by In words and sentences . If you really want to use "<" or ">" or betwen The operator , have access to btree Index to speed up .
Stored in MEMORY The data rows in the data table are in a fixed length format , So speed up processing , This means that you can't use BLOB and TEXT Such a variable length data type .VARCHAR It's a variable length type , But because it's in MySQL The interior is fixed in length CHAR type , So you can use it .
create table tab_memoryengine=memory select id,name,age,addr from man order by id;
Use USING HASH/BTREE To specify a specific index .
create index mem_hash using hashon tab_memory(city_id);
Start up MySQL Use when serving --init-file Options , hold insert into...select or load data infile Such statements are put into this file , You can load tables from persistent data sources when the service starts .
Every MEMORY The amount of data placed in the table , suffer max_heap_table_size Constraints on system variables , The initial value of this system variable is 16M, At the same time, creating MEMORY You can use MAX_ROWS Clause to specify the maximum number of rows in the table .
Every MEMORY The table actually corresponds to a disk file , The format is .frm.MEMORY Type of table access is very fast , Because the data is in memory , And by default HASH Indexes , But once the server is shut down , The data in the table will be lost , But the table will continue to exist . The server needs enough memory to keep the server in use at the same time MEMORY surface , When there is no need for MEMORY The contents of the table , To release MEMORY Memory used by table , Should carry out DELETE FROM or TRUNCATE TABLE, Or delete the entire table ( Use DROP TABLE).
4. Choice of storage engine
In practice , Choosing a suitable storage engine is a complicated problem . Each storage engine has its own advantages and disadvantages , We can't generally say who is better than who .
Storage engine comparison
InnoDB: Support transaction processing , Support foreign keys , Support for crash recovery and concurrency control . If you need to have a high requirement for transaction integrity ( Such as banks ), Concurrent control is required ( Like selling tickets ), Then choose InnoDB There are great advantages . If frequent updates are needed 、 Delete the database of operation , You can also choose InnoDB, Because it supports transaction commit (commit) And rollback (rollback).
MyISAM: Insert data fast , Low space and memory usage . If the table is mainly used to insert new records and read records , So choose MyISAM It can achieve high processing efficiency . If the integrity of the application 、 Concurrency requirements are relatively low , You can also use .
MEMORY: All the data is in memory , Data processing speed is fast , But the security is not high . If you need fast reading and writing speed , Low security requirements for data , You can choose MEMOEY. It has requirements for the size of the table , Can't build too large a table . therefore , This kind of database is only used in relatively small database tables .
The same database can also use a variety of storage engine tables . If a table requires high transaction processing , You can choose InnoDB. In this database, you can select tables with high query requirements MyISAM Storage . If the database needs a temporary table for queries , You can choose MEMORY Storage engine .
To modify the default engine , You can modify... In the configuration file default-storage-engine. Can pass :show variables like 'default_storage_engine'; View the current database to the default engine . command :show engines and show variables like 'have%' You can list the engines supported by the current database . among Value Is shown as disabled Indicates that the database supports this engine , It is disabled when the database is started . stay MySQL5.1 in the future ,INFORMATION_SCHEMA There is a... In the database ENGINES Table of , The information it provides is similar to show engines; The sentence is exactly the same , You can use the following statement to query which storage engines support transaction processing :select engine from information_chema.engines where transactions ='yes';
Can pass engine Keyword specifies the engine used when creating or modifying a database .
When creating a table, use engine=... or type=... To specify the engine to use .show table status from DBname To view the engine of the specified table .
边栏推荐
- 微信小程序父子组件之间传值
- 【openwrt】推荐一个国内开发的openwrt的版本,iStoreOS简介,非常好用,主要是做了一些优化。解决了汉化的问题。
- BSC smart contract dividend mainstream currency | including marketing wallet | deflation | reflow | dividend free token | available for direct deployment
- IntStream API介绍
- Laravel document sorting 7. View
- LeetCode 劍指Offer II 091 粉刷房子[動態規劃] HERODING的LeetCode之路
- 马斯克发布人形机器人,AI对马斯克为什么意义重大?
- Nodejs 通过Heidisql连接mysql出现ER_BAD_DB_ERROR: Unknown database 'my_db_books'
- Coinlist how to operate the middle lot number security tutorial
- List rendering in wechat applet
猜你喜欢
How many images can opencv open?
Cesium loading display thermal diagram
acmStreamOpen返回值问题
Anaconda安装+TensorFlow安装+Keras安装+numpy安装(包含镜像和版本信息兼容问题)
1、项目第二阶段——用户注册和登陆
Turn 2D photos into 3D models to see NVIDIA's new AI "magic"!
【openwrt】推荐一个国内开发的openwrt的版本,iStoreOS简介,非常好用,主要是做了一些优化。解决了汉化的问题。
OBS Browser+浏览器的基本使用
WMS仓储管理系统的使用价值,你知道多少
Read lsd-slam: large scale direct monolithic slam
随机推荐
Flutter FittedBox组件
How much do you know about the use value of WMS warehouse management system
Finereport displays and hides column data according to conditions
Cesium loading display thermal diagram
sql_ mode=only_ full_ group_ By's pit
Hello CTP (IV) - CTP transaction API
ThinkPHP is integrated with esaywechat. What's wrong with wechat payment callback without callback?
How many images can opencv open?
地方/園區產業規劃之 “ 如何進行產業定比特 ”
Although the Internet in the traditional sense has long ceased to exist, this does not mean that the Internet has long disappeared
mysql的tinyint字段类型判断的疑惑
Hello CTP (III) - CTP quotation API
无法安装redis接口
Can Navicat directly operate the Android database SQLite
升级cmake
Basic use of OBS browser+ browser
"Grammar sugar" -- my new programming knowledge
【esp32学习之路6——flash加密】
Hot and cold, sweet and sour, want to achieve success? Dengkang oral, the parent company of lengsuanling, intends to be listed on the main board of Shenzhen Stock Exchange
Retrofit 源码分析