当前位置:网站首页>MySQL -- Index Optimization -- order by
MySQL -- Index Optimization -- order by
2022-07-02 15:22:00 【It blade out of sheath】
brief introduction
explain
In this paper, MySQL Of ORDER BY Optimization scheme of index .
ORDER BY Two implementation methods of
- Automatic implementation with ordered index .( Use the orderliness of the ordered index instead of sorting ( Fast ))
- That is to say explain The results of Using index
- Select the results and then sort .( Slow speed )
- That is to say explain The results of Using filesort
Whether to go to the index or not
Query and sort are indexed
- SELECT Fields and ORDER BY The fields are exactly the same ( Or form a joint index ).
- example :SELECT col1 FROM tb1 ORDER BY col1;
- WHERE Fields and ORDER BY The fields are exactly the same ( Or form a joint index ).
- example :SELECT col1 FROM tb1 WHERE col2=2 ORDER BY col2;
Query without index
- SELECT Field does not contain ORDER BY Field .
- example :SELECT col1 FROM tb1 ORDER BY col2
- SELECT Field contains ORDER BY Field + Other fields .
- example :SELECT col1,col3 FROM tb1 ORDER BY col2
- WHERE Field does not contain ORDER BY Field .
- example :SELECT col1 FROM tb1 WHERE col2=2 ORDER BY col3
example
DROP TABLE IF EXISTS test;
CREATE TABLE test(
id int primary key auto_increment,
c1 varchar(10),
c2 varchar(10),
c3 varchar(10),
c4 varchar(10),
c5 varchar(10)
) ENGINE=INNODB default CHARSET=utf8;
INSERT INTO test(c1,c2,c3,c4,c5) VALUES('a1','a2','a3','a4','a5');
INSERT INTO test(c1,c2,c3,c4,c5) VALUES('b1','b2','b3','b4','b5');
INSERT INTO test(c1,c2,c3,c4,c5) VALUES('c1','c2','c3','c4','c5');
INSERT INTO test(c1,c2,c3,c4,c5) VALUES('d1','d2','d3','d4','d5');
INSERT INTO test(c1,c2,c3,c4,c5) VALUES('e1','e2','e3','e4','e5');
CREATE INDEX idx_c1234 ON test(c1,c2,c3,c4);
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
result :

Equivalent query
Case study 1:SELECT *,WHERE Follow the leftmost prefix ,WHERE And ORDER BY Follow the leftmost prefix
EXPLAIN SELECT * FROM test WHERE c1 = 'b1' AND c2 = 'b2' ORDER BY c3;
- 1.
result : Query index ; Sort and index

Case study 2:SELECT *,WHERE Do not follow the leftmost prefix ,ORDER BY Do not follow the leftmost prefix ,WHERE And ORDER BY Do not follow the leftmost prefix
EXPLAIN SELECT * FROM test WHERE c2 = 'b2' AND c3 = 'b3' ORDER BY c4;
- 1.
result : Queries don't index ; Sort without index

Case study 3:SELECT *,WHERE Do not follow the leftmost prefix ,ORDER BY Follow the leftmost prefix ,WHERE And ORDER BY Do not follow the leftmost prefix
EXPLAIN SELECT * FROM test WHERE c2 = 'b2' AND c3 = 'b3' ORDER BY c1;
- 1.
result : Queries don't index ; Sort without index ( Same as above )

Case study 4:SELECT And ORDER BY identical ,WHERE Do not follow the leftmost prefix ,ORDER BY Follow the leftmost prefix ,WHERE And ORDER BY Do not follow the leftmost prefix
EXPLAIN SELECT c1 FROM test WHERE c2 = 'b2' AND c3 = 'b3' ORDER BY c1;
- 1.
result : Queries don't index ; Sort and index ( Overlay index )

Range queries
Case study 1:SELECT *, And WHERE And ORDER BY Dissimilarity
EXPLAIN SELECT * FROM test WHERE c2 > 'b1' ORDER BY c1;
- 1.
result : Queries don't index ; Sort without index

Case study 2:SELECT No *,WHERE And ORDER BY Dissimilarity ,ORDER BY Left most prefix
EXPLAIN SELECT c3 FROM test WHERE c2 > 'b1' ORDER BY c1;
- 1.
result : Queries don't index , Sort and index . This situation is equivalent to using indexes only for sorting
( Be careful : take c3 Switch to c1,c2 etc. , The results are the same )

Case study 3:SELECT *,WHERE And ORDER BY Same and follow the leftmost
EXPLAIN SELECT * FROM test WHERE c1 > 'b1' ORDER BY c1;
- 1.
result : Query index ; There is no operation for sorting , Directly use the query results

Case study 4:SELECT *,WHERE And ORDER BY Different and follow the leftmost
EXPLAIN SELECT * FROM test WHERE c1 > 'b1' ORDER BY c2;
- 1.
result : Query index ; Sort without index

Other websites
Mysql stay order by The use mechanism of time index - Programmer base
边栏推荐
- Printf function and scanf function in C language
- Learn the method code example of converting timestamp to uppercase date using PHP
- Topology architecture of the minimum deployment of tidb cluster
- php获取数组中键值最大数组项的索引值的方法
- 18_Redis_Redis主从复制&&集群搭建
- 传感器数据怎么写入电脑数据库
- 基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
- JVM architecture, classloader, parental delegation mechanism
- 12_Redis_Bitmap_命令
- Practical debugging skills
猜你喜欢

03_线性表_链表

基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测

Tidb data migration tool overview

学习使用php将时间戳转换为大写日期的方法代码示例

Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language

LeetCode 2320. Count the number of ways to place the house

.NET Core 日志系统

04_ 栈

03_線性錶_鏈錶

How to avoid 7 common problems in mobile and network availability testing
随机推荐
你不知道的Set集合
C语言实现N皇后问题
.NET Core 日志系统
17_Redis_Redis发布订阅
10_Redis_geospatial_命令
搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
03_线性表_链表
GeoServer offline map service construction and layer Publishing
Introduction to C language -- array
List集合&UML图
C # delay, start the timer in the thread, and obtain the system time
[noi Simulation Competition] scraping (dynamic planning)
13_Redis_事务
Real estate market trend outlook in 2022
How to conduct TPC-C test on tidb
Points clés de l'examen de principe de compilation pour l'année scolaire 2021 - 2022 [Université chinoise d'outre - mer]
MFC console printing, pop-up dialog box
Jenkins Pipeline 应用与实践
Tidb hybrid deployment topology
How to test tidb with sysbench