![149ac69a0bd3b865f1078b4d3c6fee8a.jpeg](https://img-blog.csdnimg.cn/img_convert/149ac69a0bd3b865f1078b4d3c6fee8a.jpeg)
今天来看看MySQL 中如何多Group By 语句进行优化的。
先创建tb_user 表如下
![2cbcbb703366a900c322bf75556a31c6.jpeg](https://img-blog.csdnimg.cn/img_convert/2cbcbb703366a900c322bf75556a31c6.jpeg)
通过show index from tb_user; 命令查看表,没有存在任何的索引。
![d79184d36d04fcd5f4a30f57f3c7e07f.jpeg](https://img-blog.csdnimg.cn/img_convert/d79184d36d04fcd5f4a30f57f3c7e07f.jpeg)
执行如下代码,查看SQL 执行情况
explain select profession, count(*) from tb_user group by profession ;
![8b8cc1ac4193a93bd12cc2b3a9db76b4.jpeg](https://img-blog.csdnimg.cn/img_convert/8b8cc1ac4193a93bd12cc2b3a9db76b4.jpeg)
发现返回结果中 type 为“ALL” ,Extra 返回“Using temporary” 说明没有使用索引。
于是,创建基于profession,age和status 的索引如下
create index index_user_pro_age_sta on tb_user(professi