当前位置:网站首页>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
边栏推荐
- ZUCC_编译语言原理与编译_实验03 编译器入门
- Two methods of QT exporting PDF files
- 【无标题】
- lombok 使用
- Review SGI STL secondary space configurator (internal storage pool) | notes for personal use
- 2022 mobile crane driver special operation certificate examination question bank and online simulation examination
- JS to get the last element of the array
- Glusterfs replacement failure brick
- 2021-06-25: a batch of strings consisting only of lowercase letters (a~z) are put
- Question 4 - datepicker date selector, disabling two date selectors (start and end dates)
猜你喜欢

5 minutes, excellent customer service chat handling skills

分布式 | 如何与 DBLE 进行“秘密通话”

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

Opencv实现图像的基本变换

ZUCC_ Principles of compiling language and compilation_ Experiment 04 language and grammar

2021-03-11 comp9021 class 8 notes

The article takes you to understand the security of Windows operating system and protect your computer from infringement

List of Li Bai's 20 most classic poems

Centos7安装jdk8以及mysql5.7以及Navicat连接虚拟机mysql的出错以及解决方法(附mysql下载出错解决办法)

JUC个人简单笔记
随机推荐
lombok 使用
RuntimeError: Missing dependencies:XXX
12-- merge two ordered linked lists
JUC个人简单笔记
单目双视三维坐标确定
ZUCC_编译语言原理与编译_实验02 FSharp OCaml语言
图片工具
rsync做文件备份
13 -- remove invalid parentheses
Common misconceptions in Tencent conference API - signature error_ code 200003
日本大阪大学万伟伟研究员介绍基于WRS系统机器人的快速集成方法和应用
How to mount a USB hard disk with NTFS file format under RHEL5 system
2022 mobile crane driver special operation certificate examination question bank and online simulation examination
Shell basic operator -- arithmetic operator
Qt源码分析--QObject(2)
11-- longest substring without repeated characters
jwt(json web token)
Battle history between redis and me under billion level traffic
2021-06-25: a batch of strings consisting only of lowercase letters (a~z) are put
中国芯片独角兽公司