Introduction to mysql index
What is an index?
An index is a data structure that efficiently obtains data.
Type of index
FULLTEXT, (HASH, BTREE[two main types used by mysql]), RTREE.
1. FULLTEXT
is the full-text index, currently only supported by the MyISAM engine. It can be used in CREATE TABLE, ALTER TABLE, and CREATE INDEX, but currently only full-text indexes can be created on CHAR, VARCHAR, and TEXT columns.
Full-text index was not born together with MyISAM. It appeared to solve the problem of low efficiency of fuzzy query for text such as WHERE name LIKE "%word%".
(Free learning video tutorial recommendation: mysql video tutorial)
2. HASH
Due to the uniqueness of HASH (almost 100% unique) and similar key values The right form is suitable for use as an index.
HASH index can be located once and does not need to be searched layer by layer like a tree index, so it is extremely efficient. However, this efficiency is conditional, that is, it is only efficient under the "=" and "in" conditions, and is still not efficient for range queries, sorting, and combined indexes.
3. BTREE
The BTREE index is a method that stores the index value into a tree-shaped data structure (binary tree) according to a certain algorithm. Each query is from the entrance of the tree. Starting from root, traverse the nodes in sequence to obtain the leaf. This is the default and most commonly used index type in MySQL.
4. RTREE
#RTREE is rarely used in MySQL and only supports the geometry data type. The only storage engines that support this type are MyISAM, BDb, InnoDb, NDb, and Archive.
Compared with BTREE, the advantage of RTREE lies in range search.
Index type
Normal index: only accelerates query
Unique index: accelerates query with unique column value (can have null)
Primary key index: Accelerate query column value is unique (cannot have null) There is only one in the table
Combined index: Multiple column values form an index, specially used for combined search, its efficiency is greater than index merging
Full-text index: segment the text content and search
Index use
1. Create an index
1 --创建普通索引CREATE INDEX index_name ON table_name(col_name); 2 --创建唯一索引CREATE UNIQUE INDEX index_name ON table_name(col_name); 3 --创建普通组合索引CREATE INDEX index_name ON table_name(col_name_1,col_name_2); 4 --创建唯一组合索引CREATE UNIQUE INDEX index_name ON table_name(col_name_1,col_name_2);
2. By modifying the table Structure creation index
ALTER TABLE table_name ADD INDEX index_name(col_name);
3. Directly specify the index when creating the table
CREATE TABLE table_name ( ID INT NOT NULL,col_name VARCHAR (16) NOT NULL,INDEX index_name (col_name) );
4. Delete the index
--直接删除索引DROP INDEX index_name ON table_name; --修改表结构删除索引ALTER TABLE table_name DROP INDEX index_name;
5. Other commands
- 查看表结构 desc table_name; - 查看生成表的SQL show create table table_name; - 查看索引 show index from table_name; - 查看执行时间 set profiling = 1; SQL... show profiles;
Causes of index failure
1. Full value matching, which is equivalent to the index not being used.
2. Failure to meet the optimal prefix rule may also cause index failure.
3. Doing anything on the index (calculation, function, (automatic or manual) type conversion) will cause the index to fail and lead to a full table scan.
4. Mysql cannot use the index when using not equal to (<>, !=), resulting in a full table scan.
5. Index cannot be used even if is null or is not null.
6. Using wildcard switch like ('�c') will cause index failure and full table scan.
7. If the string index is not enclosed in single quotes, the index will be invalid.
8. Use or less. Using or to connect will cause index failure.
9. Use select * query and try to use covering index.
mysql index specification
1. [Mandatory] Fields with unique characteristics in business, even if they are a combination of multiple fields, must be built into a unique index (Note: Don't think that the unique index affects the insert speed. This speed loss can be ignored,
, but the improvement in search speed is obvious; in addition, even if very complete verification control is done at the application layer, as long as there is no unique index, according to According to Murphy's Law, dirty data must be generated.)
2. [Mandatory] Joining of more than three tables is prohibited. The data types of the fields that need to be joined must be absolutely consistent; when performing multi-table related queries, it is ensured that the related fields need to have indexes.
(Note: Even if you join a double table, you must pay attention to the table index and SQL performance.)
3. [Mandatory] When creating an index on a varchar field, you must specify the index length. There is no need to index the entire field, just determine the index length based on the actual text distinction.
(Note: Index length and discrimination are a pair of contradictions. Generally, for string type data, the index with a length of 20 will have a discrimination of more than 90%.
You can use the distinction of count(distinct left(column name, index length))/count(*) to determine.)
4. [Mandatory] Left fuzzy or full fuzzy is strictly prohibited in page search. If necessary, please Let the search engine solve it.
(Note: The index file has the leftmost prefix matching feature of B-Tree. If the value on the left is not determined, then this index cannot be used.)
Recommended related article tutorials: mysql tutorial
The above is the detailed content of Introduction to mysql index. 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.

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.

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.

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

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.
