Table of Contents
1. MyISAM index implementation:
Home Database Mysql Tutorial The difference between the index implementation methods of MyISAM and InnoDB storage engines

The difference between the index implementation methods of MyISAM and InnoDB storage engines

Sep 11, 2017 am 11:26 AM
innodb myisam index

1. MyISAM index implementation:

1) Primary key index:

MyISAM engine uses B+Tree as the index structure, and the data field of the leaf node stores The address of the data record. The following figure is the schematic diagram of MyISAM primary key index:


##                                                There are three columns in total. Suppose we use Col1 As the primary key, the figure myisam1 shows the primary index (Primary key) of a MyISAM table. It can be seen that MyISAM's index file only saves the address of the data record.

2) Secondary index (Secondary key)

In MyISAM, there is no structural difference between the primary index and the secondary index (Secondary key). The primary index requires the key to be unique, while the key of the secondary index can be repeated.

If we create an auxiliary index on Col2, the structure of this index is as shown below:


is also the same B+Tree, the data field saves the address of the data record. Therefore, the index retrieval algorithm in

MyISAM is to first search the index according to the B+Tree search algorithm. If the specified Key exists, the value of its data field is taken out, and then the value of the data field is used as the address to read the corresponding data record. .

MyISAM's indexing method is also called "non-clustered". The reason why it is called so is to distinguish it from InnoDB's clustered index.

2. InnoDB index implementation

Although InnoDB also uses B+Tree as the index structure, the specific implementation method is completely different from MyISAM.

1) Primary key index:

         

MyISAM index file and data file are separated, and the index file only saves the address of the data record. In InnoDB, the table data file itself is an index structure organized by B+Tree, and the leaf node data field of this tree saves complete data records. The key of this index is the primary key of the data table, so the InnoDB table data file itself is the primary index.

##                                                                                                                                                                    

(Figure inndb primary key index) is a schematic diagram of the InnoDB primary index (also a data file). You can see that the leaf nodes contain complete data records. This kind of index is called clustered index. Because InnoDB's data files themselves are aggregated by primary key, InnoDB requires that the table must have a primary key (MyISAM may not have one). If it is not explicitly specified, the MySQL system will automatically select a column that can uniquely identify the data record as the primary key. If it does not exist, For this type of column, MySQL automatically generates an implicit field as the primary key for the InnoDB table. The length of this field is 6 bytes and the type is long.

2). InnoDB’s auxiliary index

All auxiliary indexes in InnoDB refer to the primary key as the data field. For example, the following figure shows an auxiliary index defined on Col3:


## The InnoDB table is built based on a clustered index. Therefore, InnoDB's index can provide a very fast primary key lookup performance. However, its auxiliary index (Secondary Index, that is, non-primary key index) will also contain the primary key column, so if the primary key is defined relatively large, other indexes will also be large. If you want to define many indexes on the table, try to define the primary key as small as possible. InnoDB does not compress indexes.

The ASCII code of the text character is used as the comparison criterion.

This implementation of clustered index makes searching by primary key very efficient, but auxiliary index search requires retrieving the index twice: first, retrieve the auxiliary index to obtain the primary key, and then use the primary key to retrieve the records in the primary index.

The index implementation methods of different storage engines are very helpful for the correct use and optimization of indexes. For example, after knowing the index implementation of InnoDB, it is easy to understand why it is not recommended to use overly long fields as primary keys. , because all secondary indexes refer to the primary index, a primary index that is too long will make the secondary index too large. For another example, using non-monotonic fields as primary keys is not a good idea in InnoDB because the InnoDB data file itself is a B+Tree. Non-monotonic primary keys will cause the data file to maintain the characteristics of the B+Tree when inserting new records. Frequent split adjustments are very inefficient, and using auto-increment fields as primary keys is a good choice.


The difference between InnoDB index and MyISAM index :

First, the difference between the main index, the InnoDB data file itself is the index file . The index and data of MyISAM are separated.

The second is the difference between auxiliary indexes: InnoDB's auxiliary index data field stores the value of the corresponding record's primary key instead of the address. There is not much difference between MyISAM's secondary index and the primary index.

MySql index algorithm principle analysis (easy to understand, only talking about B-tree)

The above is the detailed content of The difference between the index implementation methods of MyISAM and InnoDB storage engines. 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)

What are the Oracle index types? What are the Oracle index types? Nov 16, 2023 am 09:59 AM

Oracle index types include: 1. B-Tree index; 2. Bitmap index; 3. Function index; 4. Hash index; 5. Reverse key index; 6. Local index; 7. Global index; 8. Domain index ; 9. Bitmap connection index; 10. Composite index. Detailed introduction: 1. B-Tree index is a self-balancing tree data structure that can efficiently support concurrent operations. In Oracle database, B-Tree index is the most commonly used index type; 2. Bit Graph index is an index type based on bitmap algorithm and so on.

MySQL storage engine selection comparison: InnoDB, MyISAM and Memory performance index evaluation MySQL storage engine selection comparison: InnoDB, MyISAM and Memory performance index evaluation Jul 26, 2023 am 11:25 AM

MySQL storage engine selection comparison: InnoDB, MyISAM and Memory performance index evaluation Introduction: In the MySQL database, the choice of storage engine plays a vital role in system performance and data integrity. MySQL provides a variety of storage engines, the most commonly used engines include InnoDB, MyISAM and Memory. This article will evaluate the performance indicators of these three storage engines and compare them through code examples. 1. InnoDB engine InnoDB is My

PHP returns the string from the start position to the end position of a string in another string PHP returns the string from the start position to the end position of a string in another string Mar 21, 2024 am 10:31 AM

This article will explain in detail how PHP returns the string from the start position to the end position of a string in another string. The editor thinks it is quite practical, so I share it with you as a reference. I hope you will finish reading this article. You can gain something from this article. Use the substr() function in PHP to extract substrings from a string. The substr() function can extract characters within a specified range from a string. The syntax is as follows: substr(string,start,length) where: string: the original string from which the substring is to be extracted. start: The index of the starting position of the substring (starting from 0). length (optional): The length of the substring. If not specified, then

How to solve the problem that the index exceeds the array limit How to solve the problem that the index exceeds the array limit Nov 15, 2023 pm 05:22 PM

The solutions are: 1. Check whether the index value is correct: first confirm whether your index value exceeds the length range of the array. The index of the array starts from 0, so the maximum index value should be the array length minus 1; 2. Check the loop boundary conditions: If you use the index for array access in a loop, make sure the loop boundary conditions are correct; 3. Initialize the array: Before using an array, make sure that the array has been initialized correctly; 4. Use exception handling: You can use the exception handling mechanism in the program to catch errors where the index exceeds the bounds of the array, and handle it accordingly.

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

How to improve the efficiency of data grouping and data aggregation in PHP and MySQL through indexes? How to improve the efficiency of data grouping and data aggregation in PHP and MySQL through indexes? Oct 15, 2023 am 11:39 AM

How to improve the efficiency of data grouping and data aggregation in PHP and MySQL through indexes? Introduction: PHP and MySQL are currently the most widely used programming languages ​​and database management systems, and are often used to build web applications and process large amounts of data. Data grouping and data aggregation are common operations when processing large amounts of data, but if indexes are not designed and used appropriately, these operations can become very inefficient. This article will introduce how to use indexes to improve the efficiency of data grouping and data aggregation in PHP and MySQL, and improve

How to use indexes in MySQL to improve query performance? How to use indexes in MySQL to improve query performance? Jul 30, 2023 pm 10:43 PM

How to use indexes in MySQL to improve query performance? Introduction: MySQL is a commonly used relational database. As the amount of data increases, query performance becomes an important consideration. In MySQL, indexes are one of the key factors in improving query performance. This article will introduce what an index is, why using indexes can improve query performance, and give some sample code for using indexes in MySQL. 1. What is an index? An index is a structure that sorts the values ​​of one or more columns in a database table. It can quickly

Advanced applications of Python slicing and indexing: reveal hidden functions and explore the infinite possibilities of programming Advanced applications of Python slicing and indexing: reveal hidden functions and explore the infinite possibilities of programming Feb 19, 2024 pm 08:40 PM

The basic syntax of slicing in Python is to use the [start:end:step] syntax for slicing operations, where start represents the starting position of the slice, end represents the end position of the slice, and step represents the slicing step. If start is omitted, it means slicing from the beginning of the list or string; if end is omitted, it means slicing to the end of the list or string; if step is omitted, it means the step size is 1. For example: my_list=[1,2,3,4,5]#Cut from the 2nd element to the 4th element (excluding the 4th element) sub_list=my_list[1:4]#[2,3,4 ]#Start from the first element until the end of the list sub_li

See all articles