当前位置:网站首页>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 .
边栏推荐
猜你喜欢

pycharm配置运行环境

Genomic DNA isolation Worthington ribonuclease A

Yolov5 learning notes (I) -- principle overview

Leetcode63. Different paths II

GhostNets on Heterogeneous Devices via Cheap Operations

With the help of rpa+lcap, the enterprise treasurer management can be upgraded digitally

EN 1873屋面用装配附件.塑料单个屋面灯—CE认证

SAP oracle 复制新实例后数据库远程连接报错 ora-01031

Apple's official website is being updated to maintain the apple store. Products such as the iPhone 13 / pro of the Bank of China will enjoy a maximum discount of 600 yuan

Classification and determination method of Worthington stemxyme
随机推荐
Leetcode63. Different paths II
电商数据模型设计
1-6 state与绑定事件
失败率高达80%,数字化转型如何正确完成战略规划?
EN 1873 assembly accessories for roofing - plastic single roof lamps - CE certification
SAP oracle 复制新实例后数据库远程连接报错 ora-01031
Leetcode60. 排列序列
Kingbasees client programming interface guide ODBC (4. Create data source)
Where is the DP interface of desktop computer (what if the host has no DP interface)
Jincang database kingbasees client programming interface guide ODBC (2. Overview)
The failure rate is as high as 80%. How to correctly complete the strategic planning of digital transformation?
laptop外接显示器
数据中台的那些“经验与陷阱”
Type 1-5 components
Leetcode64. Minimum path sum
考过HCIP入职心仪公司,分享华为认证学习经历及心得
CMake 基础学习
有效供应链管理的八大绩效分析指标(上)
VS2005透过SourceOffSite访问VSS2005的设置方法「建议收藏」
PowerCLi 批量添加esxi到vCenter