当前位置:网站首页>Ordering of MySQL composite index
Ordering of MySQL composite index
2022-06-24 08:40:00 【An unreliable programmer】
Yesterday, a colleague made a statement about the military regulations mysql Consult me about the index , I found that I didn't understand the rules of composite index . So let's make a note :
【 recommend 】 If there is order by Scene , Note the use of index order .order by The last field is part of the composite index , And put it at the end of the index combination order , Avoid file_sort The situation of , Affect query performance .
Example :where a=? and b=? order by c; Indexes :a_b_c
Counter example : Range lookup in index , Then index order cannot be utilized , Such as :WHERE a>10 ORDER BY b; Indexes a_b Cannot sort .
explain :order by The sorting principle of
1. Use the order of the index to obtain ordered data
2. Using memory / Disk file sorting results
1) Two way sorting : First, the corresponding sorting fields and row pointer information that can directly locate row data are extracted according to the corresponding conditions , And then in sort buffer In order .
2) One way sorting : Is to retrieve all fields of the qualified row at one time , And then in sort buffer In order .
The orderliness of composite index and the leftmost prefix principle
【 mandatory 】 Understand the leftmost prefix principle for composite indexes , Avoid duplicate index construction , If you set up (a,b,c), It's kind of set up (a), (a,b), (a,b,c)
Suppose there's an index (A,B)
mysql The rule for creating a composite index is that the leftmost part of the composite index is first , That's the first one A Sort the data of the field , Based on the sorting of the first field , And then to the second one B Field to sort . In fact, it is equivalent to the realization of similar order by A B Such a sort rule .
first A Fields are absolutely ordered , The second field is out of order . So usually , Use the second one directly B There is no index for conditional judgment of fields
So when can I use it ?
Certainly B The index data of fields can only be used when they are in order .
When will it be orderly ?
Only in A If the field is equivalent matching ,B It's order .
Various scenarios for combining index queries
Yes Index (A,B,C) —— Multiple fields of composite index are ordered , And it is a complete BTree Indexes .
The following conditions can be used for the combined index query :
A>5
A=5 AND B>6
A=5 AND B=6 AND C=7
A=5 AND B IN (2,3) AND C>5
The following conditions will not apply to combined index queries :
B>5 —— The query condition does not contain the first column field of the composite index
B=6 AND C=7 —— The query condition does not contain the first column field of the composite index
The following conditions will be able to use the upper part of the combined index query :
A>5 AND B=2 —— When a range query uses the first column , Query criteria can only use the first column
A=5 AND B>6 AND C=2 —— The range query uses the second column , Query criteria can only use the first two columns
Various scenarios of combined index sorting
There's a composite index Index(A,B).
The following conditions can be used to sort by combined index :
ORDER BY A—— First column sort
A=5 ORDER BY B—— After the first column is filtered, the second column is sorted
ORDER BY A DESC, B DESC—— Be careful , At this point, the two columns are sorted in the same order
A>5 ORDER BY A—— Data retrieval and sorting are in the first column
The following conditions cannot be used to sort by combined index :
ORDER BY B —— Sort in the second column of the index
A>5 ORDER BY B —— The range query is in the first column , Sort in second column
A IN(1,2) ORDER BY B —— For the same reason
ORDER BY A ASC, B DESC —— Be careful , At this point, the two columns are sorted in different order
Suggest
If you have any doubts about using the index, you can finish it sql in the future use explain Let's run it sql
Can be more conducive to understanding sql Implementation process of
边栏推荐
- Promise的使用场景
- MATLAB Camera Calibrator相机标定
- ZUCC_ Principles of compiling language and compilation_ Experiment 02 fsharp Ocaml language
- 2021-03-09 comp9021 class 7 Notes
- Synthesize video through ffmpeg according to m3u8 file of video on the network
- 2021-03-04 comp9021 class 6 notes
- Two methods of QT exporting PDF files
- Question 4 - datepicker date selector, disabling two date selectors (start and end dates)
- App Startup
- 分布式 | 如何与 DBLE 进行“秘密通话”
猜你喜欢

OpenCV to realize the basic transformation of image

ZUCC_ Principles of compiling language and compilation_ Experiment 03 getting started with compiler

ZUCC_编译语言原理与编译_实验06 07 语法分析 LL 分析

Redis的Cluster集群数据倾斜

MATLAB Camera Calibrator相机标定

ZUCC_编译语言原理与编译_实验05 正则表达式、有限自动机、词法分析

2021-03-04 comp9021 class 6 notes

Opencv实现图像的基本变换

LabVIEW finds prime numbers in an array of n elements

2021-03-11 comp9021 class 8 notes
随机推荐
Qmenu response in pyqt
JS merge multiple objects and remove duplicates
日本大阪大学万伟伟研究员介绍基于WRS系统机器人的快速集成方法和应用
Scénarios d'utilisation de la promesse
Tencent conference API - get rest API & webhook application docking information
lombok 使用
dataX使用指南
[real estate opening online house selection, WiFi coverage temporary network] 500 people are connected to WiFi at the same time
mysql组合索引的有序性
ZUCC_ Principles of compiling language and compilation_ Experiment 02 fsharp Ocaml language
11-- longest substring without repeated characters
Centos7安装jdk8以及mysql5.7以及Navicat连接虚拟机mysql的出错以及解决方法(附mysql下载出错解决办法)
"Adobe international certification" Photoshop software, about drawing tutorial?
PHP代码加密的几种方案
Base64编码详解及其变种(解决加号在URL变空格问题)
Smart power plant: how to make use of easycvr to build a safe, stable, green and environment-friendly intelligent inspection platform
Synthesize video through ffmpeg according to m3u8 file of video on the network
【关于运维和网工的差别,一文说透】
ZUCC_编译语言原理与编译_实验05 正则表达式、有限自动机、词法分析
【力扣10天SQL入门】Day2