MySQL
A very important feature that distinguishes the database from other databases is its plug-in table storage engine, which is based on tables rather than databases. Since each storage engine has its own characteristics, we can choose the most suitable storage engine for each table.
As DBA
, we should have a deep understanding of storage engines. Today we introduce the two most common storage engines and their differences: InnoDB
and MyISAM
.
InnoDB
Storage engine
InnoDB
The storage engine supports transactions, and its main design goal is Applications for OLTP (On Line Transaction Processing)
. Features include row lock design, support for foreign keys, and support for non-locking reads. Starting from version 5.5.8
, InnoDB
has become the default storage engine of MySQL
.
InnoDB
The storage engine uses a clustered index to store data, so each table is stored in the order of the primary key. If the primary key is not specified, InnoDB
will automatically generate a 6
byte ROWID
for each row as the primary key.
MyISAM
Storage engine
MyISAM
The storage engine does not support transaction and table lock designs. Supports full-text indexing, mainly for OLAP (On Line Analytical Processing)
applications, suitable for scenarios with frequent queries such as data warehouses. Before the 5.5.8
version, MyISAM
was the default storage engine for MySQL
. This engine represents the need for querying and analyzing massive amounts of data. It emphasizes performance, so the query execution speed is faster than InnoDB
. The difference between
InnoDB
and MyISAM
Transaction
For the atomicity of database operations, we need transactions. Ensure that a set of operations either all succeed or fail, such as the function of transferring money. We usually place multiple SQL
statements between begin
and commit
to form a transaction.
InnoDB
is supported, MyISAM
is not supported.
Primary key
Due to the clustered index of InnoDB
, if the primary key is not specified, the primary key will be automatically generated. MyISAM
Supports the existence of tables without primary keys.
Foreign keys
In order to solve the dependencies of complex logic, we need foreign keys. For example, the entry of college entrance examination scores must belong to a certain student, so we need a foreign key to the college entrance examination score database with the admission ticket number.
InnoDB
is supported, MyISAM
is not supported.
Index
In order to optimize the speed of queries, sort and match searches, we need indexes. For example, everyone's names are stored sequentially from the first letters of a-z
. When we search for zhangsan
or the 44
position, we can quickly locate the name we want. Find the desired location.
InnoDB
is a clustered index. The data is bound to the clustered index of the primary key. Indexing through the primary key is very efficient. If you search through the auxiliary index of other columns, you need to find the clustered index first, and then query all the data, which requires two queries.
MyISAM
is a non-clustered index, the data files are separated, and the index saves the pointer of the data.
From InnoDB 1.2.x
version, MySQL5.6
version, both support full-text indexing.
auto_increment
Auto-increment
For auto-increment fields, InnoDB
requires that the column must be an index and must It is the first column of the index, otherwise an error will be reported:
mysql> create table test( -> a int auto_increment, -> b int, -> key(b,a) -> ) engine=InnoDB; ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
Just replace the order of (b,a)
with (a,b)
.
And MyISAM
can combine this field with other fields in any order to form a joint index.
Number of table rows
A very common requirement is to see how many pieces of data there are in the table. At this time we need select count(*) from table_name
.
InnoDB
Does not save the number of table rows and requires a full table scan. MyISAM
Save it in a variable and read the value directly, which is faster. At that time, when querying with where
, the two were the same.
Storage
The files of the database need to be stored on the disk, and then read into the memory when the application needs it. Generally includes data files and index files.
InnoDB
is divided into:
-
.frm
Table structure file -
.ibdata1
Shared table space -
.ibd
Exclusive table space -
.redo
Log file
##MyISAMDivided into three files:
- .frm
Storage table definition
- .MYD
Storage table data
- .MYI
Storage table index
Execution speed
If your operation is a large number of query operations, such as SELECT
, the performance will be better using MyISAM
.
If most of the operations are deletions and changes, use InnoDB
. The indexes of
InnoDB
and MyISAM
are both B
tree indexes. The primary key of the data can be queried through the index. If you are not familiar with B
Tree can view MySQL InnoDB index principles and algorithms. The performance difference between the two mainly lies in the different processing methods after querying the primary key of the data.
InnoDB
will cache index and data files. Generally, 16KB
is used as a minimum unit (data page size) to interact with the disk, InnoDB
After querying the index data, what you actually get is the ID
of the primary key. It needs to find all the data of the row in the data page in the memory. However, if the data is not loaded hot data, you still need to perform The search and replacement of data pages may involve multiple I/O
operations and data searches in memory, resulting in high time consumption.
AndMyISAM
The storage engine only caches index files and does not cache data files. The cache of its data files directly uses the cache of the operating system, which is very unique. At this time, the same space can load more indexes, so when the cache space is limited, MyISAM
's index data page replacement times will be less. According to what we know earlier, the files of MyISAM
are divided into MYI
and MYD
. When we find the primary key ID## through
MYI #, in fact, the
offset offset of the
MYD data file is obtained, and searching for data is much faster than
InnoDB addressing mapping.
MyISAM is a table lock, and
InnoDB supports row locks, when a large number of write operations are involved, the concurrency performance of
InnoDB Much better than
MyISAM. At the same time,
InnoDB also uses
MVVC multi-version control to improve concurrent read and write performance.
deleteDelete data
delete from table,
MyISAM will directly rebuild the table ,
InnoDB will be deleted row by row, but it can be replaced by
truncate table. Reference: Two ways and differences in clearing table data in mysql.
Lock
##MyISAMOnly supports table locks, and the entire table is locked for each operation. InnoDB
Supports row locks, locking a minimum number of rows of data for each operation. Compared with row locks, table locks consume fewer resources and will not cause deadlock, but at the same time, the concurrency performance is poor. Row lock consumes more resources, is slower, and may cause deadlock, but because the locking granularity is small and the data is small, the concurrency performance is good. If a statement in
cannot determine the range to be scanned, the entire table will also be locked. When a row lock deadlock occurs, the number of rows affected by each transaction will be calculated, and then the transaction with a smaller number of rows will be rolled back.
MyISAM
There is no fast and safe recovery after a crash. InnoDB
has a complete recovery mechanism.
MyISAM
Only caches index data and queries data through the index. InnoDB
Not only caches index data, but also caches data information. The data is read into the cache pool by page and updated according to the LRU (Latest Rare Use)
algorithm.
How to choose a storage engineThe statements to create a table are all the same, only the last
type specifies the storage engine.
MyISAM1. A large number of queries total
2. Frequent queries and insufficient insertion Frequent
3. No transaction operation
InnoDB##1. High availability is required, or transactions are required
MySQL tutorial
The above is the detailed content of Differences between InnoDB and MyISAM storage engines in MySQL. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于架构原理的相关内容,MySQL Server架构自顶向下大致可以分网络连接层、服务层、存储引擎层和系统文件层,下面一起来看一下,希望对大家有帮助。

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

方法:1、利用right函数,语法为“update 表名 set 指定字段 = right(指定字段, length(指定字段)-1)...”;2、利用substring函数,语法为“select substring(指定字段,2)..”。

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
