The difference between MyISAM and InnoDB engines in MySQL

Analysis & Answer

There are many differences, please tell the following points, the interview should be OK

1) Transaction support

MyISAM does not support transactions, while InnoDB does. InnoDB's AUTOCOMMIT is enabled by default, that is, each SQL statement will be encapsulated into a transaction by default and automatically submitted, which will affect the speed, so it is best to display multiple SQL statements between begin and commit to form a transaction to submit.

MyISAM is non-transaction-safe, while InnoDB is transaction-safe. Auto-commit is enabled by default. It is advisable to merge transactions and submit them together, reducing the overhead caused by multiple database submissions and greatly improving performance.

2) foreign keys

A foreign key (foreign key) is one or more columns used to establish and strengthen a link between two table data. A link between two tables is created by adding one or more columns to another table that hold the primary key values ​​in the table. This column becomes the foreign key to the second table.

  • MyISAM: not supported
  • InnoDB: support

3) Table lock difference

MyISAM: Only table-level locks are supported. When a user operates a MyISAM table, the select, update, delete, and insert statements will automatically lock the table. If the locked table meets the concurrency of insert, a new one can be inserted at the end of the table. The data.
InnoDB: Supporting transactions and row-level locks is the biggest feature of innodb. Row locks greatly improve the performance of multi-user concurrent operations. However, InnoDB's row lock is only valid for the primary key of WHERE, and the WHERE of non-primary keys will lock the entire table.

The granularity of MyISAM locks is at the table level, while InnoDB supports row-level locking. To put it simply, InnoDB supports data row locking, while MyISAM does not support row locking, only supports locking the entire table. That is, the read lock and write lock on the same table in MyISAM are mutually exclusive. When MyISAM reads and writes concurrently, if there are both read requests and write requests in the waiting queue, the priority of the write request is high by default, even if the read request arrives first, so MyISAM is not suitable for the situation where a large number of queries and modifications coexist, so the query process will be blocked for a long time. Because MyISAM is a lock table, a certain read operation is time-consuming and will starve other writing processes to death.

4) Full text index

  • MyISAM: support (FULLTEXT type) full-text index\
  • InnoDB: Full-text index (of FULLTEXT type) was not supported before, and the InnoDB storage engine began to support full-text index after version 5.6. After version 5.7, Chinese is supported by using the ngram plugin.

Full-text index refers to the establishment of an inverted index for each word (except stop words) in char, varchar, and text. MyISAM's full-text index is actually useless, because it does not support Chinese word segmentation, and the user must add spaces after word segmentation and write it into the data table, and words with less than 4 Chinese characters will be ignored like stop words.

In addition, MyIsam index and data are separated, and InnoDB is together. MyIsam is born with a non-clustered index, which has at most one unique property. InnoDB's data file itself is a primary key index file. Such an index is called a "clustered index".

5) CURD operation

MyISAM: If you perform a lot of SELECT, MyISAM is a better choice. MyISAM has a built-in counter that holds the total number of rows in the table.
InnoDB: If your data performs a large number of INSERT or UPDATE, for performance reasons, you should use InnoDB tables. DELETE is better than InnoDB in terms of performance, but when DELETE FROM table, InnoDB will not recreate the table, but delete it line by line. If you want to clear a table with a large amount of data on InnoDB, it is best to use the truncate table command.

Reflect & Expand

Application scenarios of both MyISAM and InnoDB

  1. MyISAM manages non-transactional tables. It provides high-speed storage and retrieval, as well as full-text search capabilities. If the application needs to perform a large number of SELECT queries, then MyISAM is a better choice.
  2. InnoDB is used for transaction processing applications and has many features, including ACID transaction support, stored procedures, views, row-level locking, and more. If the application needs to perform a large number of INSERT or UPDATE operations, InnoDB should be used, which can improve the performance of multi-user concurrent operations.

MySQL supports three full-text search modes

  1. Natural language mode: pass a specific string for retrieval by MATCH AGAINST
  2. Boolean mode: operators can be added to the retrieved string, "+" means must be included, "-" means must be excluded, ">" means increase correlation when the word appears, "<" means decrease correlation when the word appears , "*" indicates a wildcard, "~" allows the word to appear, etc.
  3. query expansion mode

How is InnoDB full-text index implemented?

Meow Interview Assistant: One-stop solution to interview questions, you can search the WeChat applet [Meow Interview Assistant]  or follow [Meow Brush Questions] -> Interview Assistant  free questions. If you have good interview knowledge or skills, look forward to your sharing!

Guess you like

Origin blog.csdn.net/jjclove/article/details/127391013