Table of Contents
mysql In which query situations do not use the index
The situation of not using the index:
Home Database Mysql Tutorial Which query situations in mysql do not use the index?

Which query situations in mysql do not use the index?

May 29, 2023 pm 09:19 PM
mysql

mysql In which query situations do not use the index

1. The index column participates in the calculation and does not use the index

SELECT `username` FROM `t_user` WHERE age=20;-- 会使用索引
SELECT `username` FROM `t_user` WHERE age+10=30;-- 不会使用索引!!因为所有索引列参与了计算
SELECT `username` FROM `t_user` WHERE age=30-10;-- 会使用索引
Copy after login

2. The index column uses a function and may not use the index

-- 不会使用索引,因为使用了函数运算,原理与上面相同
SELECT username FROM t_user WHERE concat(username,'1') = 'admin1'; 
-- 会使用索引
SELECT username FROM t_user WHERE username = concat('admin','1');
Copy after login

3. Use the like statement for the index column, and may not use the index

SELECT * FROM USER WHERE username LIKE 'mysql测试%'   --走索引
SELECT * FROM USER WHERE username LIKE '%mysql测试'   --不走索引
SELECT * FROM USER WHERE username LIKE '%mysql测试%'  --不走索引
Copy after login

4. Implicit conversion of data types, direct comparison between string columns and numbers, without using the index

-- stock_code字符串类型带索引
SELECT * FROM `stock_data` WHERE stock_code = '600538'  --走索引
SELECT * FROM `stock_data` WHERE stock_code = 600538  --不走索引
Copy after login

5. Try to avoid OR operation, as long as one field does not have an index, the index will not be used when changing the statement, and the index will not be used!

-- stock_code带索引,open不带索引
SELECT * FROM `stock_data` WHERE `stock_code` = '600538' OR `open` = 6.62  -- 不走索引
-- stock_code带索引,up_down_pre带索引
SELECT * FROM `stock_data` WHERE `stock_code` = '600538' OR `up_down_pre` = 5.1  -- 走索引
Copy after login

6. where id !=2 or where id <> 2, no indexing!

SELECT * FROM t_user WHERE username <> &#39;mysql测试&#39;
Copy after login

7. If it is null or is not null, you cannot use the index. Do not use the index!

SELECT * FROM t_user WHERE username IS NULL -- 不走索引
SELECT * FROM t_user WHERE username IS NOT NULL -- 不走索引
Copy after login

8. The index column uses the in statement, and the index may not be used

-- stock_code数据类型为varchar
SELECT * FROM `stock_data` WHERE `stock_code` IN (&#39;600538&#39;)  -- 走索引
SELECT * FROM `stock_data` WHERE `stock_code` IN (&#39;600538&#39;,&#39;688663&#39;,&#39;688280&#39;)  -- 走索引
SELECT * FROM `stock_data` WHERE `stock_code` IN (大量数据)  -- 不走索引
SELECT * FROM `stock_data` WHERE `stock_code` IN (600538)  -- 不走索引
Copy after login

The situation of not using the index:

1. There is no query condition, or the query condition No indexes are created in the business database, especially tables with relatively large amounts of data.

Suggestions:

1 Change to indexed columns as query conditions

2 Or index frequently queried columns

2. The query result set is most of the data in the original table. It should be more than 25% of the

query result set. If it exceeds 25% of the total number of rows, the optimizer feels that there is no need to use the index. .

Suggestion:

1 If the business allows, you can use limit control.

2 Based on business judgment, is there a better way? If there is no better rewriting solution

3 Try not to store this data in mysql. Put it in redis.

3. The index itself is invalid and the statistical data is unreal

The index has the ability to self-maintain. When the table content changes frequently, the index may appear. Invalid.

Change plan:

Back up table data, delete and rebuild related tables.

4. Query conditions use functions on index columns, or perform operations on index columns. Operations include (, -, *, /,!, etc.)

Change Method:

Reduce the use of calculation operations such as addition, subtraction, multiplication and division in mysql.

5. Implicit conversion causes index failure. This should be taken seriously. It is also a mistake often made in development.

The field created by the index is varchar() ;

select * from stu where name = ‘111&#39;;走索引
select * from stu where name = 111;不走索引
Copy after login

Change method:

Consult with R&D, and the statement query complies with the specifications.

6.<>, not in without indexing (auxiliary index)

Change method:

Try not to use the above method to query , or select the index column as a filter condition.

Individual >,<,in may or may not go. It depends on the result set. Try to add limit

or or in according to the business. Try to change it to union

7.like “%” The percent sign is not placed at the front

EXPLAIN SELECT * FROM teltab WHERE telnum LIKE ‘31%&#39; 走索引
EXPLAIN SELECT * FROM teltab WHERE telnum LIKE ‘%110&#39; 不走索引
Copy after login

Change method:

For search requirements of %linux% class, you can use elasticsearch mongodb is a database product specializing in search services

The above is the detailed content of Which query situations in mysql do not use the index?. 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)

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.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

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

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.

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.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

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

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

See all articles