Which query situations in mysql do not use the index?
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;-- 会使用索引
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');
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测试%' --不走索引
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 --不走索引
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 -- 走索引
6. where id !=2 or where id <> 2, no indexing!
SELECT * FROM t_user WHERE username <> 'mysql测试'
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 -- 不走索引
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 ('600538') -- 走索引 SELECT * FROM `stock_data` WHERE `stock_code` IN ('600538','688663','688280') -- 走索引 SELECT * FROM `stock_data` WHERE `stock_code` IN (大量数据) -- 不走索引 SELECT * FROM `stock_data` WHERE `stock_code` IN (600538) -- 不走索引
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';走索引 select * from stu where name = 111;不走索引
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%' 走索引 EXPLAIN SELECT * FROM teltab WHERE telnum LIKE ‘%110' 不走索引
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!

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.

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

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.

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

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.

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.
