当前位置:网站首页>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
边栏推荐
- Implementation of n queen in C language
- 编译原理课程实践——实现一个初等函数运算语言的解释器或编译器
- 03_ Linear table_ Linked list
- Why can't programmers who can only program become excellent developers?
- 15_ Redis_ Redis. Conf detailed explanation
- vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
- yolo格式数据集处理(xml转txt)
- Leetcode - Search 2D matrix
- HUSTPC2022
- 13_Redis_事务
猜你喜欢
随机推荐
Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
AtCoder Beginner Contest 254
AtCoder Beginner Contest 254
Niuke Practice 101
C语言实现N皇后问题
MFC CString to char*
面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?
Base64 coding can be understood this way
语义分割学习笔记(一)
Mfc a dialog calls B dialog function and passes parameters
. Net again! Happy 20th birthday
学习使用php实现公历农历转换的方法代码
XML配置文件
05_ queue
.NET Core 日志系统
Mavn builds nexus private server
解决el-radio-group 回显后不能编辑问题
搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
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]





![[C language] explain the initial and advanced levels of the pointer and points for attention (1)](/img/61/1619bd2e959bae1b769963f66bab4e.png)



