当前位置:网站首页>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
边栏推荐
- 2021-2022學年編譯原理考試重點[華僑大學]
- 05_ queue
- vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
- 10_ Redis_ geospatial_ command
- 做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统
- 03_线性表_链表
- CDN 在游戏领域的应用
- 21_ Redis_ Analysis of redis cache penetration and avalanche
- 语义分割学习笔记(一)
- Apprendre le Code de la méthode de conversion du calendrier lunaire grégorien en utilisant PHP
猜你喜欢
Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language
Table responsive layout tips
YOLOV5 代码复现以及搭载服务器运行
19_ Redis_ Manually configure the host after downtime
语义分割学习笔记(一)
可视化搭建页面工具的前世今生
. Solution to the problem of Chinese garbled code when net core reads files
XML配置文件
15_ Redis_ Redis. Conf detailed explanation
02_ Linear table_ Sequence table
随机推荐
19_Redis_宕机后手动配置主机
TiDB 软件和硬件环境建议配置
基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)
CDN 在游戏领域的应用
C thread transfer parameters
CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
08_ 串
06_ Stack and queue conversion
Data analysis thinking analysis methods and business knowledge - business indicators
C language exercises - (array)
Kibana basic operation
02_ Linear table_ Sequence table
可视化搭建页面工具的前世今生
02_线性表_顺序表
哈夫曼树:(1)输入各字符及其权值(2)构造哈夫曼树(3)进行哈夫曼编码(4)查找HC[i],得到各字符的哈夫曼编码
21_ Redis_ Analysis of redis cache penetration and avalanche
11_ Redis_ Hyperloglog_ command
vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
PHP method to get the index value of the array item with the largest key value in the array