Table of Contents
Introduction
Index structure (tree)
Why use a tree instead of a hash table
BTree Index
B Tree Index
Clustered index and non-clustered index
Index classification
Performance analysis
Index creation scenario
Home Database Mysql Tutorial Let's talk about MySQL index structure

Let's talk about MySQL index structure

Aug 31, 2022 pm 05:49 PM
mysql

Recommended learning: mysql video tutorial

Introduction

In addition to data, the database system also maintains Data structures that satisfy specific search algorithms. These data structures reference (point to) data in some way so that advanced search algorithms can be implemented on these data structures. This data structure is an index.

Generally speaking, the index itself is also very large and cannot be stored entirely in memory, so the index is often stored on disk in the form of an index file.

Advantages:

1. Similar to building a bibliographic index in a university library, it improves the efficiency of data retrieval and reduces the IO cost of the database.

2. Sort data through index columns to reduce the cost of data sorting and reduce CPU consumption.

Disadvantages:

1. Although the index greatly improves the query speed, it will also reduce the speed of updating the table, such as INSERT, UPDATE and DELETE on the table. Because when updating the table, MySQL not only needs to save the data, but also save the index file. Every time a field that adds an index column is updated, the index information after the key value changes caused by the update will be adjusted.

2. In fact, the index is also a table. This table saves the primary key and index fields and points to the records of the entity table, so the index column also takes up space.

Index example: ( Use tree structure as index)

The left side is the data table, with a total of two columns and seven records. The leftmost one is the physical address of the data record.

In order to speed up the search of Col2, you can maintain a binary search tree as shown on the right. Each node contains the index key value and a pointer to the physical address of the corresponding data record. Pointer, so that you can use binary search to obtain the corresponding data within a certain complexity, thereby quickly retrieving records that meet the conditions.

Index structure (tree)

How to speed up the query speed of database tables through indexes? For the convenience of explanation, we limit the database table to only include the following two query requirements:

1. select* from user where id=1234;

2. select *from user where id>1234 and id<2345; (by interval)

Why use a tree instead of a hash table

Ha The performance of Greek table query by value is very good, and the time complexity is O(1), but it cannot support fast search of data according to intervals, so it cannot meet the requirements. In the same way, although the query performance of the balanced binary search tree is very high, the time complexity is O(logn), and the in-order traversal of the tree can output an ordered data sequence, it cannot meet the need to quickly find data according to intervals. .

In order to support fast search of data according to intervals, we transform the binary search tree and string the leaf nodes of the binary search tree with a linked list. If you want to find data in a certain interval, you only need to use the interval The starting value is searched in the tree. After locating a node in the ordered linked list, it starts from this node and traverses along the ordered linked list until the node data value in the ordered linked list is greater than the interval end value. until.

And because the time complexity of many operations on the tree is proportional to the height of the tree, reducing the height of the tree can reduce disk IO operations. Therefore, we build the index into an m-ary tree (m>2). Please see the following article for details.

BTree Index

Before introducing the B-tree, let’s first understand the B-tree.

1. Initialization introduction

A b-tree, the light blue block is called a disk block, you can see each disk block Contains several data items (shown in dark blue) and pointers (shown in yellow). For example, disk block 1 contains data items 17 and 35, and contains pointers P1, P2, and P3. P1 represents disk blocks less than 17, P2 represents disk blocks between 17 and 35, and P3 represents disk blocks greater than 35.

Note:

Real data only exists in leaf nodes, namely 3, 5, 9, 10, 13, 15, 28, 29, 36, 60, 75, 79, 90, 99. (And it is a data interval composed of multiple pieces of data: 3~5,...,90~99)

Non-leaf nodes do not store real data, but only store data items that guide the search direction, such as 17, 35 does not actually exist in the data sheet.

2. Search process

If you want to find data item 29, you will first load disk block 1 from the disk to the memory. At this time, an IO occurs. Use a binary search in the memory to determine that 29 is between 17 and 35, and lock P2 of disk block 1. Pointer, memory time can be ignored because it is very short (compared to disk IO). Disk block 3 is loaded from the disk to the memory through the disk address of the P2 pointer of disk block 1. The second IO occurs, 29 between 26 and 30. During the period, the P2 pointer of disk block 3 is locked, and disk block 8 is loaded into the memory through the pointer. The third IO occurs. At the same time, a binary search is performed in the memory to find 29, and the query is ended. A total of three IOs.

B Tree Index

B tree is similar to B tree, and B tree is an improved version of B tree. That is: the tree constructed by the m-fork search tree and the ordered linked list is the B-tree, which is the tree index to be stored

As shown in the figure: B-tree and the main features of the B-tree The differences are as follows:

1. The leaf nodes of the B-tree are connected in series using a linked list. To find data in a certain interval, you only need to use the starting value of the interval to search in the tree. After locating a node in the ordered linked list, start from this node and traverse backward along the ordered linked list until Until the node data value in the ordered linked list is greater than the interval end value.

2. Any node in the B-tree does not store real data, but is only used for indexing. B-tree obtains data directly through leaf nodes; and each leaf node of B-tree stores the key value and address information of the data row. When a certain leaf node is queried, the real data information is found through the address of the leaf node.

Clustered index and non-clustered index

Clustered index is not a separate index type, but a data storage method. The term 'clustered' refers to the storage of data rows and adjacent key-value clusters together.

Benefits of clustered index:

According to the clustered index arrangement order, when the query displays a certain range of data, because the data are closely connected, the database does not have to extract from multiple data blocks data, so it saves a lot of io operations.

Limitations of clustered indexes:

1. For the mysql database, currently only the innodb data engine supports clustered indexes, while Myisam does not support clustered indexes.

2. Since there can only be one physical storage sorting method for data, each Mysql table can only have one clustered index. Usually it is the primary key of the table.

3. In order to make full use of the clustering characteristics of the clustered index, the primary key column of the innodb table should try to use ordered sequential IDs, and it is not recommended to use unordered IDs, such as uuid.

As shown below, the index on the left is a clustered index, because the arrangement of data rows on the disk is consistent with the index sorting.

Index classification

Single value index

That is, an index only contains a single column, and a table can have multiple single-column indexes

随表一起建索引:
CREATE TABLE customer (
id INT(10) UNSIGNED  AUTO_INCREMENT ,
customer_no VARCHAR(200),
customer_name VARCHAR(200),
PRIMARY KEY(id),
KEY (customer_name)
);
 
单独建单值索引:
CREATE  INDEX idx_customer_name ON customer(customer_name); 
 
删除索引:
DROP INDEX idx_customer_name  on customer;
Copy after login

Unique index

The value of the index column must be unique, but null values ​​are allowed

随表一起建索引:
CREATE TABLE customer (
id INT(10) UNSIGNED  AUTO_INCREMENT ,
customer_no VARCHAR(200),
customer_name VARCHAR(200),
  PRIMARY KEY(id),
  KEY (customer_name),
  UNIQUE (customer_no)
);
  
单独建唯一索引:
CREATE UNIQUE INDEX idx_customer_no ON customer(customer_no); 
 
删除索引:
DROP INDEX idx_customer_no on customer ;
Copy after login

Primary key index

After setting the primary key, the database will automatically create an index , innodb is a clustered index

随表一起建索引:
CREATE TABLE customer (
id INT(10) UNSIGNED  AUTO_INCREMENT ,
customer_no VARCHAR(200),
customer_name VARCHAR(200),
  PRIMARY KEY(id) 
);
   
CREATE TABLE customer2 (
id INT(10) UNSIGNED   ,
customer_no VARCHAR(200),
customer_name VARCHAR(200),
  PRIMARY KEY(id) 
);
 
 单独建主键索引:
ALTER TABLE customer 
 add PRIMARY KEY customer(customer_no);  
 
删除建主键索引:
ALTER TABLE customer 
 drop PRIMARY KEY ;  
 
修改建主键索引:
必须先删除掉(drop)原索引,再新建(add)索引
Copy after login

Compound index

That is, an index contains multiple columns

随表一起建索引:
CREATE TABLE customer (
id INT(10) UNSIGNED  AUTO_INCREMENT ,
customer_no VARCHAR(200),
customer_name VARCHAR(200),
  PRIMARY KEY(id),
  KEY (customer_name),
  UNIQUE (customer_name),
  KEY (customer_no,customer_name)
);
 
单独建索引:
CREATE  INDEX idx_no_name ON customer(customer_no,customer_name); 
 
删除索引:
DROP INDEX idx_no_name  on customer ;
Copy after login

Performance analysis

Index creation scenario

In which cases it is necessary to create an index

1. The primary key automatically creates a unique index

2. Fields that are frequently used as query conditions should create indexes

3 , Fields associated with other tables in the query, foreign key relationships to establish indexes

4. Single key/combined index selection problem, the combined index is more cost-effective

5. Sorted fields in the query , if the sorting field is accessed through the index, the sorting speed will be greatly improved

6. Statistics or grouping fields in the query

Under what circumstances should not create an index

1. There are too few table records

2. Reasons for tables or fields that are frequently added, deleted or modified: It improves the query speed, but at the same time reduces the speed of updating the table, such as INSERT, UPDATE and DELETE on the table. Because when updating the table, MySQL not only needs to save the data, but also save the index file

3. No index will be created for fields that are not used in the Where condition

4. If the filtering is not good, no index will be created. Suitable for index building

Recommended learning:mysql video tutorial

The above is the detailed content of Let's talk about MySQL index structure. 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)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1263
29
C# Tutorial
1236
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL for Beginners: Getting Started with Database Management MySQL for Beginners: Getting Started with Database Management Apr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Apr 18, 2025 am 08:42 AM

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

See all articles