当前位置:网站首页>MySQL principle and optimization of Group By optimization techniques
MySQL principle and optimization of Group By optimization techniques
2022-08-09 13:03:00 【51CTO】
今天来看看MySQL how muchGroup By 语句进行优化的.
先创建tb_user 表如下
通过show index from tb_user; 命令查看表,No index exists.
执行如下代码,查看SQL 执行情况
explain select profession, count(*) from tb_user group by profession ;
发现返回结果中 type 为“ALL” ,Extra 返回“Using temporary” 说明没有使用索引.
于是,创建基于profession,age和status 的索引如下
create index index_user_pro_age_sta on tb_user(profession ,age, status);
Here the order of creating indexes from left to right is profession ,age, status.
此时再次执行SQL执行计划如下:
explain select profession, count(*) from tb_user group by profession ;
found that the index was used“index_user_pro_age_sta”.说明在执行 group by操作的时候,Using a federated index is valid.
Then look at using the following code:
explain select age, count(*) from tb_user group by age;
SQL 语句使用age 进行group by,查看explain的结果如下:
在Extra Field found used“Using temporary”,说明没有走索引,It is because the index is not satisfied最左前缀法则.
联合索引 index_user_pro_age_staThe order is from left to right respectively profession ,age, status.
上面的SQL 语句Group by 后面接着的是age ,因此出现“Using temporary”.
这里对SQL 进行修改.如下:
explain select profession,age, count(*) from tb_user group by profession, age;
由于group by 后面跟着profession, age ,Consistent with the order in which the federated index was created,So the index works.
Let's try again and add the filter condition,加入profession = 软件工程,此时group by 里面只显示 age,Then whether the index will be taken at this time, 答案是 using index.Because the leftmost prefix rule is satisfied.
explain select age, count(*) from tb_user where profession = '软件工程' group by age;
总结一下:
SQLwhen operating in groups,可以通过索引来提高效率.
When doing group operations,The use of indexes needs to satisfy the leftmost prefix rule.
边栏推荐
猜你喜欢
在北京参加UI设计培训到底怎么样?
听声辨物,这是AI视觉该干的???|ECCV 2022
API调用,API传参,面向对接开发,你真的会写接口文档吗?
Information system project managers must memorize the core test sites (63) The main process of project portfolio management & DIPP analysis
HAproxy: load balancing
web course design
Win10 compiles the x264 library (there are also generated lib files)
ABP 6.0.0-rc.1的新特性
已解决IndentationError: unindent does not match any oute r indentation Level
数字化转型之支撑保障单元
随机推荐
go基础之web获取参数
香港服务器如何进行加密?
用皮肤“听”音乐,网友戴上这款装备听音乐会:仿佛住在钢琴里
数字化转型之支撑保障单元
微信一面:一致性哈希是什么,使用场景,解决了什么问题?
proto3-2 syntax
win10编译x264库(也有生成好的lib文件)
【无标题】
Here comes the question: Can I successfully apply for 8G memory on a machine with 4GB physical memory?
获取url地址中问号后参数(即使是iframe也可以)
曼城推出可检测情绪的智能围巾,把球迷给整迷惑了
"Digital Economy Panorama White Paper" Special Analysis of Banking Industry Intelligent Marketing Application Released
阻塞、非阻塞、多路复用、同步、异步、BIO、NIO、AIO 一锅端
推荐一个免费50时长的AI算力平台
京东架构师呕心整理:jvm与性能调优有哪些核心技术知识点
The latest interview summary in 20022 brought by Ali senior engineer is too fragrant
ACM longest non-descent subsequence problem
PM2 configuration file
FFmpeg库在win10上配置使用(不配置libx264)
ABAP 面试题:如何使用 ABAP 编程语言的 System CALL 接口,直接执行 ABAP 服务器所在操作系统的 shell 命令?