MYSQL storage engine-the difference between MYISAM and INNODB

Transactions in mysql

1. Transactions in computer databases: In computer terms, it refers to a program execution unit (unit) that accesses and may update various data items
in the database. 2. In relational databases, a transaction can be a SQL statement, a group of SQL Statement or entire program

  • A transaction, as long as there is any error in the middle, all operations of this transaction must be undone
  • Myisam does not support transactions, while innodb supports transactions
Storage engine in mysql
mysql storage engines include: MyISAM, InnoDB, BDB, MEMORY, MERGE, EXAMPLE, NDBCluster, ARCHIVE, CSV, BLACKHOLE, FEDERATED, etc., among which InnoDB and BDB provide transaction-safe tables, and other storage engines are non-transaction-safe tables.

mysql version

  • The default storage engine before 5.1 was myisam,
  • The default storage engine after 5.1 is innodb.

1. The difference between myisam and innodb

MyISAM:
myisam only supports table-level locks. When the user operates the myisam table, select, update, delete, and insert statements will automatically lock the table. If the locked table meets the insert concurrency, insert at the end of the table. New data. You can also lock the table through the lock table command. This operation can mainly imitate transactions, but it consumes a lot of money and is generally only used in experimental demonstrations.
InnoDB:
Innodb supports transactions and row-level locks, which is the biggest feature of InnoDB.
ACID properties of the transaction: atomicity, consistent, isolation, durable.
Several problems caused by concurrent transactions: update loss, dirty reads, non-repeatable reads, phantom reads.
Insert picture description here
Check the default transaction isolation level of mysql "show global variables like'tx_isolation';"

mysql> show global variables like 'tx_isolation'; 
+--------------------+----------------------------+
| Variable_name | Value                        |
+--------------------+----------------------------+
| tx_isolation      | REPEATABLE-READ   |
+--------------------+----------------------------+
1 row in set (0.01 sec)
等同于 select @@global.tx_isolation
  • Set the current session isolation level set session transaction isolatin level repeatable read;

  • Set the current isolation level of the system set global transaction isolation level repeatable read;
    Innodb's row lock mode has the following types: shared locks, exclusive locks, intentional shared locks (table locks), intentional exclusive locks (table locks), gap locks.

Note: When the statement does not use the index, innodb cannot determine the operation row, this time the intention lock is used, that is, the table lock
2. Database file differences

Insert picture description hereInsert picture description here

3. Index differences

1. About automatic growth

  • The auto-growth column of the myisam engine must be an index. If it is a composite index, the auto-growth may not be the first column. It can be sorted and incremented according to the previous columns.

  • The automatic growth of the innodb engine must be an index. If it is a composite index, it must also be the first column of the composite index.
    2. About the primary key

  • Myisam allows tables without any indexes and primary keys to exist,

  • The index of myisam is the address of the saved row.

  • If the innodb engine does not set a primary key or a non-empty unique index, it will automatically generate a 6-byte primary key (not visible to the user)

  • The data of innodb is part of the main index, and the additional index saves the value of the main index
    3. About the count() function

  • myisam saves the total number of rows of the table, if select count(*) from table; will directly take out the value

  • Innodb does not save the total number of rows in the table. If you use select count(*) from table; it will traverse the entire table, which consumes a lot of money, but after adding the wehre condition, myisam and innodb handle the same way.
    4. Full-text index

  • myisam supports full text index of FULLTEXT type

  • Innodb does not support full-text indexing of FULLTEXT type, but innodb can use the sphinx plug-in to support full-text indexing, and the effect is better. (Sphinx is an open source software that provides API interfaces in multiple languages, which can optimize various queries of mysql)
    5. When delete from table
    uses this command, InnoDB will not recreate tables, but delete data one by one. If you want to clear a table with a large amount of data on innodb, it is best not to use this command. (It is recommended to use truncate table, but the user is required to have the permission to drop this table)
    6. Index save location

  • The indexes of myisam are saved as table name +.MYI files.

  • Innodb's index and data are stored in the table space together.

to sum up

1. MyISAM does not support transactions. InnoDB is a transaction type storage engine. When our tables need transaction support, then MyISAM must not be chosen.

2. MyISAM only supports table-level locks, BDB supports page-level locks and table-level locks by default as page-level locks, while InnoDB supports row-level locks and table-level locks by default as row-level locks
(table-level locks: directly lock the entire table, During the lock period, other processes cannot write to the table. If the write lock is set, then other processes are not allowed to read. MyISAM is a table-level locking storage engine, it will not have deadlock problems)

3. MyISAM engine does not support foreign keys, InnoDB supports foreign keys

4. The table of MyISAM engine will often be damaged under a large number of high concurrent reads and writes.

5. MyISAM has more advantages for count() queries

6. InnoDB is designed for maximum performance when processing huge amounts of data, and its CPU efficiency may be unmatched by any other disk-based relational database engine.

7. MyISAM supports full text indexing (FULLTEXT), but InnoDB does not support it

8. The query, update, and insert efficiency of the MyISAM engine table is higher than that of InnoDB

test

Test method 1: Submit 10 queries continuously, the total number of table records: 380,000, time unit s

Insert picture description here
a. If your data volume is millions and there is no transaction processing, then using MyISAM is the best choice
. b. The size of InnoDB table is larger, and using MyISAM can save a lot of hard disk space.

Test 2: Data reading performance test. Randomly read 1000 records each time, read repeatedly

Insert picture description here
It can be seen that the read performance of MyISAM is very scary, and the performance gap is 3 times.

Test 3: Two threads write concurrently, and two threads read concurrently.

Insert picture description here
To sum up: Innodb insert performance is more stable in applications with more writes and less reads, and it can be basically unchanged under concurrent conditions. If it is an application that requires faster reading speed, choose MyISAM.

Test 4: Data insertion performance test. Here I have tested the innodb_flush_log_at_trx_commit parameter on and off. Each test is run for 40s. The numbers in the table are the actual number of inserts.

Insert picture description here
Generally speaking, MyISAM performance is better. During the insertion test, I monitored the system resources and found that MyISAM occupies very low system resources, but Innodb occupies a high disk, which should be a lot more need for transaction control. Recorded log

Guess you like

Origin blog.csdn.net/weixin_45942735/article/details/104568969