What is mysql index and how to use it? Organized in great detail
When learning mysql, you often encounter indexes. What is an index? I was only vaguely able to use it before, but it was a bit difficult for me to explain it, so I took advantage of my free time to read some books, just in case someone asked me in the future, I sorted it out and wrote it down, which gave me some impressions. It's good after all, and it's quite embarrassing to say no. After all, I've been exposed to these for a few years. Let's not talk nonsense. Let's talk about the key points of apache php mysql:
begin!
1. What is an index?
1. Index introduction
The index is actually a data structure stored on the disk in the form of a file. Index retrieval Disk I/O operations are required. Unlike main memory, disk I/O involves mechanical movement costs, so the time consumption of disk I/O is huge.
2.IO Introduction
IO refers to input and output in computers. Since programs and runtime data reside in memory, they are executed by the ultra-fast computing core of the CPU, which involves data exchange. Places, usually disks, networks, etc., require IO interfaces. Life example: All key things that need to be remembered need to be written in a notebook. Take them out and read them when needed. Every time you go to the notebook to read the records, it is IO. If people with good memory will remember this matter, they can do it directly. Read it out, this is the cache (it cannot be saved all the time in the computer).
2. Index algorithm
1. The database is basically implemented using the B Tree algorithm
2. The database index uses the number of disk I/O To evaluate the quality of the index structure
3.B-Tree
(1) The definition of B-Tree shows that a maximum of h-1 nodes need to be accessed for one retrieval (the root node is resident in memory). The designers of the database system cleverly took advantage of the disk read-ahead principle and set the size of a node to equal one page, so that each node only needs one I/O to be fully loaded
(2) Actual implementation B-Tree also needs to use the following skills: each time a new node is created, it directly applies for a page of space. This ensures that a node is physically stored in a page. In addition, the computer storage allocation is aligned by page, and this is achieved. A node only needs one I/O
(3) Using B-Tree storage structure, the number of I/Os during search will generally not exceed 3 times, so using B-Tree as an index structure is very efficient. , but the nodes in B-tree can contain a large amount of keyword information and branches depending on the actual situation
4.B Tree
(1) The search complexity of B-Tree is O(h)=O (logdN), so the greater the out-degree d of the tree, the smaller the depth h, and the fewer the number of I/Os. B Tree can exactly increase the width of out-degree d, because each node is one page size, so the upper limit of out-degree depends on the size of the key and data in the node
(2) Since the internal nodes of B Tree Data is removed, so it can have a larger out-degree and thus have better performance
3. Clustered index and non-clustered index
1. Clustered index
(1) The physical storage order of clustered index data is consistent with the index order, that is: as long as the indexes are adjacent, the corresponding data must also be stored adjacently on the disk. Clustered indexes are much more efficient than non-clustered index query
(3) Each table can only have one clustered index, because records in a table can only be stored in one physical order
(4) Innodb's default index
2. Non-clustered index
(1) Non-clustered index, similar to the appendix of a book, in which chapter the professional term appears, these The technical terms are in order, but the position where they appear is not. However, a table can have more than one non-clustered index
(2) The implementation principle is to use leaf nodes to store the primary key of the reference row (it can be said to be a clustered index)
(3) Clustered index It is an index of non-clustered index, that is, the indexing method of primary and secondary indexes. The advantage of this primary and secondary index is that when data row movement or page split occurs, the auxiliary index tree does not need to be updated, because the auxiliary index tree stores The primary key keyword of the primary index, rather than the specific physical address of the data
(4) Therefore, the non-clustered index needs to access the index twice
4. Index type
1.UNIQUE (unique index): the same value cannot appear, and NULL values are allowed
2.INDEX (ordinary index): the same index content is allowed
3.PROMARY KEY (primary key index): The same value is not allowed
4.FULLTEXT INDEX (full-text index): It can target a certain word in the value, but the efficiency is very poor
5. Combined index: Essentially, multiple fields are built into one index, and the combination of column values must be unique
5. Indexing skills
1. The index is not Columns that will contain NULL
(1) As long as the column contains NULL values, they will not be included in the index. As long as there is a column in the composite index that contains NULL values, then this column is eligible for the index. It is invalid
2. Use short index
(1) to index the string. If possible, you should specify a prefix length. For example, if you have a column of char(255), don't index the entire column if most values are unique within the first 10 or 20 characters. Short indexes can not only improve query speed but also save disk space and I/O operations
3. Index column sorting
(1) MySQL query only uses one index, so if the index has been used in the where clause, the columns in order by will not use the index. Therefore, do not use sorting operations when the default sorting of the database can meet the requirements. Try not to include sorting of multiple columns. If necessary, it is best to build composite indexes for these columns
4.like statement operations
(1) Generally, the use of like operations is discouraged. If it must be used, pay attention to the correct way of use. Like '�a%' will not use the index, but like 'aaa%' can use the index
5. Do not perform operations on columns
6. Do not use NOT IN, <> ;,! = operation, but <,<=, =,>,>=,BETWEEN,IN can use indexes
7. Indexes should be established on fields where select operations are often performed
(1) This is because if these columns are rarely used, the presence or absence of indexes will not significantly change the query speed. On the contrary, due to the addition of indexes, it reduces the maintenance speed of the system and increases the space requirements
8. The index should be established on the fields with relatively unique values
9. For those defined as Columns of text, image, and bit data types should not be indexed. Because the amount of data in these columns is either quite large or has very few values
10. The columns appearing in where and join need to be indexed
11.There is an inequality sign in the query condition of where ( where column != …), mysql will not be able to use the index
12. If a function is used in the query condition of the where clause (such as: where DAY(column)=…), mysql will not be able to use the index
13. In the join operation (when data needs to be extracted from multiple data tables), mysql can only use the index when the data type of the primary key and the foreign key is the same, otherwise the index will not be used if it is established in time
14.explain can help developers analyze SQL problems. Explain shows how mysql uses indexes to process select statements and connection tables. It can help choose better indexes and write more optimized query statements
6. Indexes and locks
1. If the lock uses an index, it is a row lock. If the index is not used, it is a table lock, so the data to be operated must use a lock.
(1) If there is no index, data selection or positioning will be done through a full table scan, which will form a table lock. If there is an index, the specified row will be directly located, that is A row lock is formed. Note here that if the index is not used when updating the data, the entire table will be scanned
end
Most of this content is I usually accumulate some unclear information from the Internet and books, so please forgive me!
Related articles:
How to use mysql index name and when to use it
What is an index? There are currently several main index types in Mysql
Related videos:
A brief introduction to indexes - a video tutorial to take you through MySQL in six days
The above is the detailed content of What is mysql index and how to use it? Organized in great detail. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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 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.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

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.

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 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.

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.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.
