I know that the interviewer will ask me about the four common database engines of Mysql.

Preface

The database storage engine is the underlying software organization of the database, and the database management system (DBMS) uses the data engine to create, query, update, and delete data. Different storage engines provide different storage mechanisms, indexing techniques, locking levels and other functions. Using different storage engines can also obtain specific functions. Many different database management systems now support a variety of different data engines. The core of MySQL is the storage engine.

Storage engine view

MySQL provides developers with the function of querying the storage engine. I am using MySQL 5.1 here. You can use:

SHOW ENGINES

Command to view the engine used by MySQL, the output of the command is (Navicat Premium I use):

Insert picture description here

I see that MySQL provides users with so many storage engines, including the engine for processing transaction-safe tables and the engine for non-transaction-safe tables.

If you want to see which engine the database uses by default, you can use the command:

SHOW VARIABLES LIKE 'storage_engine';

To check, the query result is:

Insert picture description here

In MySQL, there is no need to use the same storage engine in the entire server. For specific requirements, you can use a different storage engine for each table. The value of the Support column indicates whether a certain engine can be used: YES means it can be used, NO means it cannot be used, and DEFAULT means that the engine is the current default storage engine. Let's take a look at some of the commonly used engines.

InnoDB storage engine

InnoDB is the preferred engine for transactional databases. It supports transaction security tables (ACID), row locking and foreign keys. As you can see in the figure above, InnoDB is the default MySQL engine. The main features of InnoDB are:

  • 1. InnoDB provides MySQL with a transaction security (ACID compatible) storage engine with commit, rollback, and crash recovery capabilities. InnoDB is locked at the row level and also provides an Oracle-like non-locking read in the SELECT statement. These features increase multi-user deployment and performance. In SQL queries, you can freely mix InnoDB tables and other MySQL table types, even in the same query

  • 2. InnoDB is designed for maximum performance in processing huge amounts of data. Its CPU efficiency may be unmatched by any other disk-based relational database engine lock

  • 3. The InnoDB storage engine is fully integrated with the MySQL server. The InnoDB storage engine maintains its own buffer pool for caching data and indexes in main memory. InnoDB puts its tables and indexes in a logical table space, and the table space can contain several files (or raw disk files). This is different from MyISAM tables, for example, each table in the MyISAM table is stored in a separate file. InnoDB tables can be of any size, even on operating systems where the file size is limited to 2GB

  • 4. InnoDB supports foreign key integrity constraints. When storing data in a table, the storage of each table is stored in the order of the primary key. If the primary key is not specified when the table is defined, InnoDB will generate a 6-byte ROWID for each row And use it as the primary key

  • 5. InnoDB is used on many large database sites that require high performance

InnoDB does not create a directory. When using InnoDB, MySQL will create a 10MB automatically expanded data file named ibdata1 in the MySQL data directory, and two 5MB log files named ib_logfile0 and ib_logfile1

MyISAM storage engine

MyISAM is based on the ISAM storage engine and extends it. It is one of the most commonly used storage engines in the Web, data warehousing and other application environments. MyISAM has high insertion and query speed, but it does not support things. The main features of MyISAM are:

  • 1. Large files (up to 63-bit file length) are supported on file systems and operating systems that support large files

  • 2. When deleting and updating and inserting operations are mixed, dynamically sized rows produce less fragmentation. This is done automatically by merging adjacent deleted blocks, and if the next block is deleted, expanding to the next block

  • 3. The maximum number of indexes per MyISAM table is 64, which can be changed by recompiling. The maximum number of columns per index is 16

  • 4. The maximum key length is 1000 bytes, which can also be changed by compiling. If the key length exceeds 250 bytes, a key exceeding 1024 bytes will be used

  • 5. BLOB and TEXT columns can be indexed

  • 6, NULL is allowed in the index column, this value occupies 0~1 bytes of each key

  • 7. All numeric key values ​​are stored in high byte first to allow a higher index compression

  • 8. Each MyISAM type table has an AUTO_INCREMENT internal column, which is updated during INSERT and UPDATE operations, and the AUTO_INCREMENT column will be refreshed at the same time. So, the AUTO_INCREMENT column update of MyISAM type table is faster than the AUTO_INCREMENT of InnoDB type

  • 9. You can put data files and index files in different directories

  • 10. Each character column can have a different character set

  • 11. Tables with VARCHAR can have fixed or dynamic record length

  • 12. VARCHAR and CHAR columns can be up to 64KB

Use the MyISAM engine to create a database, and 3 files will be generated. The file name starts with the table name, and the extension is the file type: frm file storage table definition, the extension of the data file is .MYD (MYData), the extension of the index file is .MYI (MYIndex)

MEMORY storage engine

The MEMORY storage engine stores the data in the table in memory, and provides fast access without querying and referencing other table data. The main features of MEMORY are:

  • 1. Each table of the MEMORY table can have up to 32 indexes, each index has 16 columns, and a maximum key length of 500 bytes

  • 2. MEMORY storage engine executes HASH and BTREE in miniature

  • 3. There can be non-unique key values ​​in a MEMORY table

  • 4. The MEMORY table uses a fixed record length format

  • 5. MEMORY does not support BLOB or TEXT columns

  • 6. MEMORY supports AUTO_INCREMENT columns and indexes on columns that can contain NULL values

  • 7. The MEMORY table is shared among all clients (just like any other non-TEMPORARY table)

  • 8. The memory of the MEMORY table is stored in the memory. The memory is shared between the MEMORY table and the internal table created when the server is idle during query processing.

  • 9. When the contents of the MEMORY table are no longer needed, to release the memory used by the MEMORY table, you should execute DELETE FROM or TRUNCATE TABLE, or delete the entire table (using DROP TABLE)

ARCHIVE storage engine

Archive means archiving. After archiving, many advanced functions are no longer supported, and only support the most basic insertion and query functions. Prior to MySQL 5.5, Archive did not support indexes, but it began to support indexes in MySQL 5.5 or later. Archive has a good compression mechanism. It uses the zlib compression library and compresses in real time when the record is requested, so it is often used as a warehouse.

Scenario: Archive is very suitable as a storage engine for the log table due to its high compression and fast insertion, but the premise is that the table is not frequently queried.

Choice of storage engine

Different storage engines have their own characteristics to meet different needs, as shown in the following table:

Insert picture description here

If you want to provide transaction security (ACID compatible) capabilities such as commit, rollback, and crash recovery capabilities, and require concurrency control, InnoDB is a good choice

If the data table is mainly used to insert and query records, the MyISAM engine can provide higher processing efficiency

If the data is only temporarily stored, the amount of data is not large, and high data security is not required, you can choose the Memory engine that saves the data in memory, and MySQL uses this engine as a temporary table to store the intermediate results of the query

If there are only INSERT and SELECT operations, you can choose Archive. Archive supports highly concurrent insert operations, but it is not transaction safe. Archive is very suitable for storing archived data, such as recording log information can use Archive

Which engine to use requires a flexible choice. Multiple tables in a database can use different engines to meet various performance and actual needs. Using a suitable storage engine will improve the performance of the entire database.

Pay attention, don't get lost

Alright, everyone, the above is the entire content of this article. The people who can see here are all talents . As I said before, there are a lot of technical points in PHP, because there are too many, it is really impossible to write, and you will not read too much after writing it, so I will organize it into PDF and documents here, if necessary Can

Click to enter the secret code: PHP+「Platform」

Insert picture description here

Insert picture description here


For more learning content, please visit the [Comparative Standard Factory] excellent PHP architect tutorial catalog, as long as you can read it to ensure that the salary will rise a step (continuous update)

The above content hopes to help everyone . Many PHPers always encounter some problems and bottlenecks when they are advanced. There is no sense of direction when writing too much business code. I don’t know where to start to improve. I have compiled some information about this, including But not limited to: distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripts, Docker, microservices, Nginx, etc. Many knowledge points, advanced advanced dry goods, can be shared with everyone for free, and those who need can join my PHP technology exchange group

Guess you like

Origin blog.csdn.net/weixin_49163826/article/details/108811416