Table of Contents
The difference between MyISAM and InnoDB in mysql: " >The difference between MyISAM and InnoDB in mysql:
##How to choose: " > ##How to choose:
Home Database Mysql Tutorial What is the difference between MyISAM and InnoDB in mysql

What is the difference between MyISAM and InnoDB in mysql

Nov 08, 2022 pm 07:42 PM
mysql

Differences: 1. InnoDB supports transactions, but MyISAM does not support it; 2. InnoDB supports foreign keys, but MyISAM does not support it; 3. InnoDB is a clustered index, while MyISAM is a non-clustered index; 4. Innodb does not support full-text indexes , while MyISAM supports full-text index; 5. InnoDB supports table and row-level locks, while MyISAM supports table-level locks; 6. InnoDB tables must have unique indexes, but Myisam does not need them; 7. The storage files are different.

What is the difference between MyISAM and InnoDB in mysql

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

InnoDB: MySQL’s default transactional engine is also the most important and widely used storage engine. It is designed to have a large number of short-term transactions. In most cases, short-term transactions are submitted normally and are rarely rolled back. InnoDB's performance and automatic crash recovery features make it popular for non-transactional storage needs. Unless there is a very special reason to use another storage engine, the InnoDB engine should be given priority.

MyISAM: In MySQL 5.1 and earlier versions, MyISAM is the default engine. MyISAM provides a large number of features, including full-text indexing, compression, spatial functions (GIS), etc., but MyISAM does not support transactions and row-level locks, and an undoubted flaw is that it cannot be safely recovered after a crash.

The difference between MyISAM and InnoDB in mysql:

1. InnoDB supports transactions, but MyISAM does not. For every SQL statement in InnoDB By default, languages ​​are encapsulated into transactions and automatically submitted, which will affect the speed, so it is best to put multiple SQL languages ​​between begin and commit to form a transaction;

2. InnoDB Supports foreign keys, while MyISAM does not. Converting an InnoDB table containing foreign keys to MYISAM will fail;

3. InnoDB is a clustered index, using B Tree as the index structure, and the data file is tied to the (primary key) index. Together (the table data file itself is an index structure organized by B Tree), there must be a primary key, and indexing through the primary key is very efficient. However, the auxiliary index requires two queries, first to query the primary key, and then to query the data through the primary key. Therefore, the primary key should not be too large, because if the primary key is too large, other indexes will also be large.

MyISAM is a non-clustered index and also uses B Tree as the index structure. The index and data files are separated, and the index saves the pointer of the data file. Primary key indexes and secondary indexes are independent.

That is to say: the leaf nodes of InnoDB's B-tree primary key index are data files, and the leaf nodes of the auxiliary index are the values ​​of the primary key; while the leaf nodes of MyISAM's B-tree primary key index and auxiliary index are both data files. address pointer.

What is the difference between MyISAM and InnoDB in mysql

What is the difference between MyISAM and InnoDB in mysql

4. InnoDB does not save the specific number of rows in the table, and the entire table is required when executing select count(*) from table scanning. MyISAM uses a variable to save the number of rows in the entire table. When executing the above statement, you only need to read the variable, which is very fast (note that no WHERE conditions can be added);

So why? What if InnoDB doesn't have this variable?

Because of the transaction characteristics of InnoDB, the number of rows in the same time table is different for different transactions, so the count statistics will calculate the number of rows that can be counted for the current transaction, not Store the total number of rows for quick query. InnoDB will try to traverse the smallest possible index unless the optimizer prompts to use another index. If the secondary index does not exist, InnoDB will also try to traverse other clustered indexes.
If the index is not completely in the buffer maintained by InnoDB (Buffer Pool), the count operation will be more time-consuming. You can create a table that records the total number of rows and let your program update the corresponding data during INSERT/DELETE. Like the problem mentioned above, this solution is not very useful if there are multiple transactions at this time. If the approximate row value is enough to meet your needs, you can try SHOW TABLE STATUS

5. Innodb does not support full-text indexing, but MyISAM supports full-text indexing. In terms of query efficiency in the field of full-text indexing MyISAM is faster and faster; PS: InnoDB after 5.7 supports full-text indexing

6. MyISAM tables can be compressed for query operations

7. InnoDB supports table and row (default) level locks, while MyISAM supports table-level locks

InnoDB’s row lock is implemented on the index, rather than locking on the physical row record. The subtext is that if the access does not hit the index and the row lock cannot be used, it will degenerate into a table lock.

For example:

t_user(uid, uname, age, sex) innodb;
 
    uid PK
    无其他索引
    update t_user set age=10 where uid=1;             命中索引,行锁。
 
    update t_user set age=10 where uid != 1;           未命中索引,表锁。
 
    update t_user set age=10 where name='chackca';    无索引,表锁。
Copy after login

8. The InnoDB table must have a unique index (such as a primary key) (if the user does not specify it, he or she will find/produce a hidden column Row_id to serve as the default primary key). And Myisam can not have it

9. Innodb storage files include frm and ibd, while Myisam is frm, MYD, MYI

Innodb: frm is Table definition file, ibd is the data file

Myisam: frm is the table definition file, myd is the data file, myi is the index file

##How to choose:

1. Do you want to support transactions? If so, please choose innodb. If not, you can consider MyISAM;

2. If most of the tables are only read queries, you can consider MyISAM. If there are both reads and writes, please use InnoDB.

3. After the system crashes, it will be more difficult for MyISAM to recover, is it acceptable?

4. Innodb has been released since MySQL 5.5 version Becoming the default engine of Mysql (previously MyISAM) shows that its advantages are obvious to all. If you don’t know what to use, then use InnoDB, at least it will not be bad.

Why does InnoDB recommend using auto-incrementing ID as the primary key?

Answer: Auto-incrementing ID can ensure that the B index is expanded from the right every time it is inserted, which can avoid B-tree and frequent merging and splitting (compared to using UUID). If you use string primary keys and random primary keys, data will be inserted randomly and the efficiency will be poor.

The 4 major features of innodb engine

## Insert buffer, double write , adaptive hash index (ahi), read ahead (read ahead)

[Related recommendations:

mysql video tutorial

]

The above is the detailed content of What is the difference between MyISAM and InnoDB in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

See all articles