MySQL进阶之SQL优化(group by优化,limit优化,count优化,update优化)

SQL优化

group by优化

首先删除所有索引,执行命令

mysql> explain select profession , count(*) from tb_user group by profession ;

+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-----------------+
| id | select_type | table   | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra           |
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-----------------+
|  1 | SIMPLE      | tb_user | NULL       | ALL  | NULL          | NULL | NULL    | NULL |   18 |   100.00 | Using temporary |
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-----------------+
1 row in set, 1 warning (0.01 sec)

然后,我们在针对于 profession , age, status 创建一个联合索引

mysql> create index idx_user_pro_age_sta on tb_user(profession , age , status);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> explain select profession , count(*) from tb_user group by profession ;
+----+-------------+---------+------------+-------+----------------------+----------------------+---------+------+------+----------+-------------+
| id | select_type | table   | partitions | type  | possible_keys        | key                  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+---------+------------+-------+----------------------+----------------------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | tb_user | NULL       | index | idx_user_pro_age_sta | idx_user_pro_age_sta | 54      | NULL |   18 |   100.00 | Using index |
+----+-------------+---------+------------+-------+----------------------+----------------------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.02 sec)

 我们发现,如果仅仅根据age分组,就会出现 Using temporary ;而如果是 根据 profession,age两个字段同时分组,则不会出现 Using temporary。原因是因为对于分组操作, 在联合索引中,也是符合最左前缀法则的。

mysql> explain select profession , count(*) from tb_user group by profession ,age;
+----+-------------+---------+------------+-------+----------------------+----------------------+---------+------+------+----------+-------------+
| id | select_type | table   | partitions | type  | possible_keys        | key                  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+---------+------------+-------+----------------------+----------------------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | tb_user | NULL       | index | idx_user_pro_age_sta | idx_user_pro_age_sta | 54      | NULL |   18 |   100.00 | Using index |
+----+-------------+---------+------------+-------+----------------------+----------------------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

mysql> explain select age , count(*) from tb_user group by age;
+----+-------------+---------+------------+-------+----------------------+----------------------+---------+------+------+----------+------------------------------+
| id | select_type | table   | partitions | type  | possible_keys        | key                  | key_len | ref  | rows | filtered | Extra                        |
+----+-------------+---------+------------+-------+----------------------+----------------------+---------+------+------+----------+------------------------------+
|  1 | SIMPLE      | tb_user | NULL       | index | idx_user_pro_age_sta | idx_user_pro_age_sta | 54      | NULL |   18 |   100.00 | Using index; Using temporary |
+----+-------------+---------+------------+-------+----------------------+----------------------+---------+------+------+----------+------------------------------+
1 row in set, 1 warning (0.00 sec)

 

所以,在分组操作中,我们需要通过以下两点进行优化,以提升性能:

A. 在分组操作时,可以通过索引来提高效率。

B. 分组操作时,索引的使用也是满足最左前缀法则的。

limit优化

在数据量比较大时,如果进行limit分页查询,在查询时,越往后,分页查询效率越低。

当在进行分页查询时,如果执行 limit 2000000,10 ,此时需要MySQL排序前2000010 记 录,仅仅返回 2000000 - 2000010 的记录,其他记录丢弃,查询排序的代价非常大 。

优化思路: 一般分页查询时,通过创建 覆盖索引 能够比较好地提高性能,可以通过覆盖索引加子查 询形式进行优化。

mysql> select id from tb_user order by id limit 0,10;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  5 |
|  6 |
|  7 |
|  8 |
|  9 |
| 10 |
+----+
10 rows in set (0.00 sec)
#将上面的命令当做一张表来查询
mysql> select t.* from tb_user t,(select id from tb_user order by id limit 0,10) a where t.id = a.id;

count优化

count() 是一个聚合函数,对于返回的结果集,一行行地判断,如果 count 函数的参数不是 NULL,累计值就加 1,否则不加,最后返回累计值。

用法:count(*)、count(主键)、count(字段)、count(数字)

mysql> select count(id) from tb_user;
+-----------+
| count(id) |
+-----------+
|        18 |
+-----------+
1 row in set (0.00 sec)

mysql> select count(name) from tb_user;
+-------------+
| count(name) |
+-------------+
|          18 |
+-------------+
1 row in set (0.00 sec)

mysql> select count(1) from tb_user;
+----------+
| count(1) |
+----------+
|       18 |
+----------+
1 row in set (0.00 sec)
count用法 含义
count(主 键) InnoDB 引擎会遍历整张表,把每一行的 主键id 值都取出来,返回给服务层。 服务层拿到主键后,直接按行进行累加(主键不可能为null)
count(字段) 没有not null 约束 : InnoDB 引擎会遍历整张表把每一行的字段值都取出 来,返回给服务层,服务层判断是否为null,不为null,计数累加。 有not null 约束:InnoDB 引擎会遍历整张表把每一行的字段值都取出来,返 回给服务层,直接按行进行累加。
count(数字) InnoDB 引擎遍历整张表,但不取值。服务层对于返回的每一行,放一个数字“1” 进去,直接按行进行累加。
count(*) InnoDB引擎并不会把全部字段取出来,而是专门做了优化,不取值,服务层直接 按行进行累加。

按照效率排序的话,count(字段) < count(主键 id) < count(1) ≈ count(),所以尽 量使用 count()。

update优化

update course set name = 'javaEE' where id = 1 ;

当我们在执行删除的SQL语句时,会锁定id为1这一行的数据,然后事务提交之后,行锁释放。

但是当我们在执行如下SQL时。

update course set name = 'SpringBoot' where name = 'PHP' ;

当我们开启多个事务,在执行上述的SQL时,我们发现行锁升级为了表锁。 导致该update语句的性能 大大降低。

InnoDB的行锁是针对索引加的锁,不是针对记录加的锁 ,并且该索引不能失效,否则会从行锁 升级为表锁 。

 

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-01-30 15:58:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-30 15:58:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-30 15:58:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-30 15:58:03       20 阅读

热门阅读

  1. Docker

    Docker

    2024-01-30 15:58:03      37 阅读
  2. Qt Bezier闭合曲线插值(2D)

    2024-01-30 15:58:03       39 阅读
  3. FreeRTOS简介

    2024-01-30 15:58:03       42 阅读
  4. HTML5 服务器发送事件(Server-Sent Events)

    2024-01-30 15:58:03       42 阅读
  5. JNDI以及利用JNDI进行漏洞攻击

    2024-01-30 15:58:03       37 阅读
  6. 【Vue】为什么Vue3使用Proxy代替defineProperty?

    2024-01-30 15:58:03       34 阅读
  7. Hotspot源码解析-第28章-终结篇章

    2024-01-30 15:58:03       35 阅读
  8. 在Ubuntu环境下搭建小型化Git服务器

    2024-01-30 15:58:03       33 阅读
  9. 隐马尔可夫模型系列——(三)模型推断

    2024-01-30 15:58:03       33 阅读
  10. sftp上传下载文件

    2024-01-30 15:58:03       38 阅读