当前位置:网站首页>MySQL 原理与优化,Group By 优化 技巧
MySQL 原理与优化,Group By 优化 技巧
2022-08-09 11:58:00 【51CTO】
今天来看看MySQL 中如何多Group By 语句进行优化的。
先创建tb_user 表如下
通过show index from tb_user; 命令查看表,没有存在任何的索引。
执行如下代码,查看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);
这里创建索引从左到右的顺序是 profession ,age, status。
此时再次执行SQL执行计划如下:
explain select profession, count(*) from tb_user group by profession ;
发现使用了索引“index_user_pro_age_sta”。说明在执行 group by操作的时候,使用联合索引是有效的。
接着在看使用如下代码:
explain select age, count(*) from tb_user group by age;
SQL 语句使用age 进行group by,查看explain的结果如下:
在Extra 字段中发现使用了“Using temporary”,说明没有走索引,是因为没有满足索引的最左前缀法则。
联合索引 index_user_pro_age_sta的顺序从左到右分别是 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 ,符合联合索引的创建顺序,因此索引生效。
我们再来试试再加入过滤条件的情况,加入profession = 软件工程,此时group by 里面只显示 age,那么此时是否会走索引, 答案是 using index。因为满足了最左前缀法则。
explain select age, count(*) from tb_user where profession = '软件工程' group by age;
总结一下:
SQL在分组操作的时候,可以通过索引来提高效率。
做分组操作的时候,索引的使用需要满足最左前缀法则。
边栏推荐
- 字符串 | 反转字符串 | 双指针法 | leecode刷题笔记
- 两分钟录音就可秒变语言通!火山语音音色复刻技术如何修炼而成?
- Fapi_StatusType Fapi_issueProgrammingCommand使用注意事项
- ClickHouse物化视图(八)
- IDEA close/open reference prompt Usages
- Programmer's Exclusive Romance - Use 3D Engine to Realize Fireworks in 5 Minutes
- F280049库函数API编程、直接寄存器控制编程和混合编程方法
- 箭头函数和普通函数的常见区别
- Shell之常用小工具(sort、uniq、tr、cut)
- WeChat payment development process
猜你喜欢
Double pointer - the role of char **, int **
罗振宇折戟创业板/ B站回应HR称用户是Loser/ 腾讯罗技年内合推云游戏掌机...今日更多新鲜事在此...
The latest interview summary in 20022 brought by Ali senior engineer is too fragrant
F280049库函数API编程、直接寄存器控制编程和混合编程方法
结构体变量的首地址获取注意事项
程序员的专属浪漫——用3D Engine 5分钟实现烟花绽放效果
2022 Niu Ke Duo School (6) M. Z-Game on grid
在北京参加UI设计培训到底怎么样?
proto3-2 syntax
[Interview high-frequency questions] Linked list high-frequency questions that can be gradually optimized
随机推荐
JD.com architects tidy up: what are the core technical knowledge points of jvm and performance tuning
阻塞、非阻塞、多路复用、同步、异步、BIO、NIO、AIO 一锅端
Summary of learning stages (knapsack problem)
网页控制台控制编辑框
WPF 实现带蒙版的 MessageBox 消息提示框
WeChat side: what is consistent hashing, usage scenarios, and what problems does it solve?
WeChat payment development process
【VQA survey】视觉问答中的语言学问题
Shell之常用小工具(sort、uniq、tr、cut)
2022 全球 AI 模型周报
web course design
win10 outlook邮件设置
mysql + redis + flask + flask-sqlalchemy + flask-session 配置及项目打包移植部署
《数字经济全景白皮书》银行业智能营销应用专题分析 发布
Here comes the question: Can I successfully apply for 8G memory on a machine with 4GB physical memory?
Blocking, non-blocking, multiplexing, synchronous, asynchronous, BIO, NIO, AIO all in one pot
Two ways to enter the Oracle database
Blazor Server (9) from scratch -- modify Layout
Experiment record: the process of building a network
electron 应用开发优秀实践