当前位置:网站首页>Detailed explanation of 9 common reasons for MySQL index failure
Detailed explanation of 9 common reasons for MySQL index failure
2022-07-29 00:04:00 【Goose goose goose】
Catalog
2. Primary key insertion order
3. Calculation 、 function 、 Type conversion ( Automatic or manual ) Cause index to fail
4. The column index to the right of the range condition fails
5. It's not equal to (!= perhaps <>) Cause index to fail
6.is null You can use index ,is not null Index not available
7.like Use wildcards % Invalid start index
8.OR As long as there are non indexed columns before and after , Will lead to index invalidation ?
9. Database and table character sets are used uniformly utf8mb4
Preface :
MySQL One of the most effective ways to improve performance in is to design a reasonable index for the data table . Indexing provides an efficient way to access data , And speed up the query , Therefore, index has a crucial impact on the speed of query .
- Using an index, you can quickly locate a record in a table , So as to improve the speed of database query , Improve database performance .
- If the index is not used in the query , The query statement will scan all records in the table . In case of large amount of data , In this way, the query speed will be very slow .
Most of the time ( Default ) use B+ Trees To build the index . Only the index of spatial column type makes R- Trees , also MEMORY surface And support hash Indexes . Actually , Use without index Ultimately, the optimizer has the final say .
What optimizer is based on ? be based on cost expenses (CostBaseOptimizer) , It's not based on rules ( Rule-BasedOptimizer), It's not based on semantics . How to spend small, how to . in addition , SQL Whether the statement uses index , With the database version 、 Data volume 、 Data selection is all about .
**1.** The best left prefix rule
expand :Alibaba《
Java Development Manual 》
Index file has B-Tree The leftmost prefix matching property of , If the value on the left is not determined , Then you can't use this index .
**2.** Primary key insertion order
If another primary key is inserted at this time, the value is 9 The record of , The insertion position is shown as follows :
But this data page is full , What if you plug in again ? We need to put the current Page splitting In two pages , Move some records in this page to the newly created page . What does page splitting and record shifting mean ? signify : Performance loss ! So if we want to try Avoid such unnecessary performance loss , It's best to let the inserted record The primary key values are incremented , In this way, there will be no such performance loss .
So we suggest : Let the primary key have AUTO_INCREMENT , Let the storage engine generate the primary key for the table itself ,
When inserting records, the storage engine will automatically fill in the self increasing primary key value for us . Such a primary key takes up less space , Write in sequence , Reduce page splits .
**3. Calculation 、 function 、 Type conversion ( Automatic or manual )** Cause index to fail
**4.** The column index to the right of the range condition fails
Example :
# Create a federated index , Note the order of the fields
create index idx_age_classid_name on student(age,classid,name);
# Implementation plan
EXPLAIN SELECT SQL_NO_CACHE * FROM student
WHERE student .age = 30 AND student .classId > 20 AND student .name = ‘abc’ ;
# Create another federated index , Compared with the index above, the field order has changed
create index idx_age_name_classid on student(age,name,classid);
# Execute the same plan as like as two peas.
EXPLAIN SELECT SQL_NO_CACHE * FROM student
WHERE student .age = 30 AND student .classId > 20 AND student .name = ‘abc’ ;
You can see that both execution plans use indexes , however :
- The first one is useless , Only the union index is used “idx_age_classid_name” Of age and classid.
- The second is to combine the index “idx_age_name_classid” Of age,name and classid It's all used .
5. It's not equal to (!= perhaps **<>)** Cause index to fail
6.is null**** You can use index ,is not null Index not available
**7.like Use wildcards %** Invalid start index
expand :Alibaba《Java Development Manual 》
【 mandatory 】 Page search must not be left blurred or full blurred , If you need to go to search engine to solve .
8.OR As long as there are non indexed columns before and after , Will lead to index invalidation
**9.** Database and table character sets are used uniformly utf8mb4
Unified use utf8mb4( 5.5.3 Version above supports ) Better compatibility , The unified character set can avoid the chaos caused by character set conversion . Different Character set Before comparison transformation Will cause index invalidation .
Thank you very much :
The above knowledge comes from “ Silicon Valley ”, Mr. Song Hongkang's 《MySQL Database tutorial ,mysql The installation to mysql senior , strong ! hard !》 Series courses .
So the book kings who can't understand this article can search song Hongkang's mysql Series courses .
边栏推荐
- Multi sensor fusion positioning (I) -- 3D laser odometer
- 【MySQL 8】Generated Invisible Primary Keys(GIPK)
- What is in word?:^ p
- E-commerce data model design
- Oracle create tablespaces and users
- 实时数仓:滴滴的实时数仓落地实践
- Best practices for migration of kingbasees v8.3 to v8.6 of Jincang database (3. Kingbasees migration capability support system)
- VMware VCSA 7.0 Install
- Connection pool - return connection details (Part 2)
- Leetcode 763. partition labels divide alphabetic intervals (medium)
猜你喜欢
Leetcode60. 排列序列
After SAP Oracle replicates a new instance, the remote connection of the database reports an error ora-01031
Explanation of history and chemical properties of Worthington ribonuclease B
YOLOV5学习笔记(一)——原理概述
Genomic DNA isolation Worthington ribonuclease A
研发效能的道法术器
基于 FPGA 实现数字时钟详细原理讲解及验证结果
Build SSM project with JSP as view parser
RHCE the next day
ISO 13400(DoIP)标准解读
随机推荐
EN 1935 building hardware. Single axis hinge - CE certification
RHCE first day
VMware VCSA 7.0 Install
Compatibility description between kingbasees and Oracle (4. SQL)
Do you know any formal part-time platforms?
实时数仓:滴滴的实时数仓落地实践
Data warehouse: Doris' application practice in meituan
JS advanced ES6 ~ es13 new features
[TA frost wolf \u may - "hundred people plan"] Figure 3.6 texture compression - inclusion slimming
VirtualLab基础实验教程-8.傅里叶变换(1)
Working principle of fastdfs (technical principle)
Connection pool - return connection details (Part 2)
【C】 Introduction and Simulation Implementation of ATOI and offsetof
Leetcode61. rotating linked list
EN 1873屋面用装配附件.塑料单个屋面灯—CE认证
SAP oracle 复制新实例后数据库远程连接报错 ora-01031
考过HCIP入职心仪公司,分享华为认证学习经历及心得
Kingbasees client programming interface guide ODBC (4. Create data source)
NAT如何配置地址转换
JS four formulas for judging data types