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

AI Hentai Generator
Generate AI Hentai for free.

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



MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

Recovering deleted rows directly from the database is usually impossible unless there is a backup or transaction rollback mechanism. Key point: Transaction rollback: Execute ROLLBACK before the transaction is committed to recover data. Backup: Regular backup of the database can be used to quickly restore data. Database snapshot: You can create a read-only copy of the database and restore the data after the data is deleted accidentally. Use DELETE statement with caution: Check the conditions carefully to avoid accidentally deleting data. Use the WHERE clause: explicitly specify the data to be deleted. Use the test environment: Test before performing a DELETE operation.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.
