Mysql related knowledge application 2: Mysql database engine (MyISAM, Innodb)

Browse the catalog

  • Overview
  • What are Mysql database engines
  • MyISAM, Innodb's characteristics and reasons for selection

I. Overview

The database engine is a core service for storing, processing, and protecting data . Use the database engine to control access permissions and quickly process transactions , thus meeting the requirements of most applications in the enterprise that need to process large amounts of data. Use the database engine to create a relational database for online transaction processing or online analytical processing of data. This includes creating tables for storing data and database objects (such as indexes, views, and stored procedures ) for viewing, managing, and protecting data security .

Database Engine Task

In the database engine documentation, the order of the topics follows the main order used to implement the tasks of the system using the database engine for data storage.

  • Design and create a database to store the relationship or XML documents required by the system
  • Implement the system to access and change the data stored in the database. Including applications that implement websites or use data, and also include the process of generating and using SQL Server tools and utilities to use data.
  • Deploy the implemented system for units or customers
  • Provide daily management support to optimize database performance

Two: What are Mysql database engines

Reference URL

The database engine you can use depends on how mysql was compiled when it was installed. To add a new engine, you must recompile MYSQL. By default , MYSQL supports three engines: ISAM, MYISAM, and HEAP. The other two types, INNODB and BERKLEY (BDB), are also often available.

  • ISAM

ISAM is a well-defined and time-tested data table management method . It was designed to take into account that the number of database queries is much greater than the number of updates . Therefore, ISAM performs read operations very quickly and does not occupy a large amount of memory and storage resources. The two major deficiencies of ISAM are that it does not support transaction processing or fault tolerance : if your hard disk crashes, the data files cannot be recovered. If you are using ISAM in mission-critical applications, you must always back up all your real-time data . Through its replication feature, MYSQL can support such backup applications.

  • MYISAM

MYISAM is MYSQL's ISAM extension format and the default database engine. In addition to providing index and field management functions that are not available in ISAM , MYISAM also uses a table locking mechanism to optimize multiple concurrent read and write operations. The cost is that you need to run the OPTIMIZE TABLE command frequently to restore the space wasted by the update mechanism. MYISAM also has some useful extensions, such as the MYISAMCHK tool for repairing database files and the MYISAMPACK tool for recovering wasted space.
  MYISAM emphasizes the fast read operation , which may be the main reason why MYSQL is so popular in WEB development: in WEB development, a large number of data operations are read operations. Therefore, most web hosting providers and Internet platform providers only allow the use of MYISAM format . As for why MyISAM reads data quickly, it will be combined with MyISAM data form for analysis in the future

  • HEAP

HEAP allows temporary tables that only reside in memory . Residing in memory makes HEAP faster than ISAM and MYISAM, but the data it manages is unstable , and if it is not saved before shutdown, all data will be lost. When the data row is deleted, HEAP will not waste a lot of space. HEAP tables are very useful when you need to use SELECT expressions to select and manipulate data . Remember to delete the form after using it .

  • INNODB和BERKLEYDB

The INNODB and BERKLEYDB (BDB) database engines are both direct products of the technology that makes MYSQL flexible. This technology is the MYSQL ++ API. When using MYSQL, almost every challenge you face comes from the ISAM and MYISAM database engines that do not support transaction processing or foreign keys . Although much slower than the ISAM and MYISAM engines, INNODB and BDB include support for transaction processing and foreign keys, both of which are not available in the first two engines. As mentioned earlier, if your design requires one or both of these features, then you will be forced to use one of the last two engines.


Three: MyISAM, Innodb's characteristics and choice reasons
  • 1) View and modify the database engine:

1. View the engines supported by the current database and the default database engine:

show engines;

2. Change the database engine

2.1, Change method 1: Modify the configuration file my.ini

Save my-small.ini as my.ini , add default-storage-engine = InnoDB after [mysqld] , restart the service, and change the default engine of the database to InnoDB

2.2, Change method 2: Specify when building the table

Specify when creating the table:

create table mytbl(   
    id int primary key,   
    name varchar(50)   
)type=MyISAM;

2.3. Change method 3: change after building the table

alter table mytbl2 type = InnoDB;

3. View the modification results

//方式1:
show table status from mytest; 
//方式2:
show create table table_name
  • 2) Database engine features:
  • Innodb engine features

A: The Innodb engine provides support for database ACID transactions , and implements four isolation levels of the SQL standard. For database transactions and their isolation levels, see the article Database Transactions and Their Isolation Levels.
B: The engine also provides row-level locks and foreign key constraints . Its design goal is to handle large-capacity database systems . It is actually a complete database system based on the MySQL background. Innodb will create a buffer pool in memory when MySQL runs. Used to buffer data and indexes.
C: FULLTEXT type index is not supported , and it does not save the number of rows in the table, the entire table needs to be scanned when SELECT COUNT (*) FROM TABLE.
D: Usage: When you need to use database transactions , this engine is of course the first choice. Because the granularity of the lock is smaller, the write operation will not lock the full table , so when the concurrency is high, using the Innodb engine will improve efficiency. However, the use of row-level locking is not absolute. If MySQL cannot determine the range to be scanned when executing a SQL statement, the InnoDB table will also lock the entire table.

ACID

A transaction atomicity (Atomicity) : refers to a transaction that is all executed or not executed. That is to say, a transaction cannot be stopped after only half of the execution. For example, if you withdraw money from the cash machine, this transaction can be divided into two steps : 1 draw card, 2 pay. It is impossible to draw the card, but the money did not come out. The two steps must be completed at the same time. Or it will not be completed .
C Consistency of transaction (Consistency) : refers to the operation of the transaction does not change the database Data consistency . For example, if integrity restricts a + b = 10, and a transaction changes a, then b should also change accordingly.
I Independence (Isolation) : The independence of the transaction is also called isolation, is Refers to the state that two or more transactions will not be interleaved . This may cause data inconsistency.
D Durability : The durability of a transaction means that after the transaction is successfully executed, the changes made by the firm to the database are It is persistently stored in the database and will not be rolled back for no reason.

  • MyISAM engine features

A: MyIASM is the default engine of MySQL, but it does not provide support for database transactions, nor does it support row-level locks and foreign keys , so when INSERT (insert) or UPDATE (update) data, write operations need to lock the entire table, The efficiency will be lower.
B: The number of rows of the table is stored in MyIASM , so when SELECT COUNT (*) FROM TABLE, you only need to directly read the saved values ​​without performing a full table scan.
C: Engine selection: If the read operation of the table is far more than the write operation and does not require the support of database transactions , then MyIASM is also a good choice.

  • 3) Summary of usage:

Large size data sets tend to choose the InnoDB engine because it supports transaction processing and failure recovery .

The size of the database determines the length of time for failure recovery. InnoDB can use the transaction log for data recovery, which will be faster. The primary key query will also be quite fast under the InnoDB engine, but it should be noted that if the primary key is too long, it will also cause performance problems (the reason will be mentioned in the next article, the data structure of the Mysql index ). A large number of INSERT statements (write multiple rows in each INSERT statement, bulk insert) will be faster under MyISAM , but the UPDATE statement will be faster under InnoDB , especially when the amount of concurrency is large.


Next: Mysql database index data structure (BTree, B + Tree)

Published 27 original articles · praised 0 · visits 9934

Guess you like

Origin blog.csdn.net/weixin_38246518/article/details/89503822