前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MySQL优化案例(二)

MySQL优化案例(二)

作者头像
jeanron100
发布2019-05-17 15:36:07
6580
发布2019-05-17 15:36:07
举报

这是学习笔记的第 1979 篇文章

近期收到慢日志监控报警,在慢日志平台查看,主要瓶颈在于几条创建临时表的SQL语句,占用了大量的临时空间,需要优化。

SQL语句为:

create temporary table `tem_expireClassID`
 (
  select distinct class_id
  from dic_fsm_map_relation
  where game_id = 1
   and state = 0
   and class_id not in ( 
    SELECT distinct json_extract(fsm_info,'$.FSM.ClassID') 
     FROM dic_fsm_info 
     where state = 0
      and json_extract(fsm_info,'$.FSM.ETime') > unix_timestamp(now())
  )
  order by class_id;

两个表的数据量都在几千条,其实不算多,但是执行时间却差很多。

执行时间为150秒左右。

执行计划为:

+----+--------------------+----------------------+------------+------+---------------+------+---------+------+------+----------+------------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+--------------------+----------------------+------------+------+---------------+------+---------+------+------+----------+------------------------------+ | 1 | PRIMARY | dic_fsm_map_relation | NULL | ALL | plat_id | NULL | NULL | NULL | 2403 | 1.00 | Using where; Using temporary | | 2 | DEPENDENT SUBQUERY | dic_fsm_info | NULL | ALL | NULL | NULL | NULL | NULL | 1316 | 10.00 | Using where | +----+--------------------+----------------------+------------+------+---------------+------+---------+------+------+----------+------------------------------+ 2 rows in set, 1 warning (0.00 sec)

系统层优化:

系统临时表空间占用150G左右

[root@hb30-dba-mysql-tgp-124-34 data]# ll total 157854040 -rw-r----- 1 mysql mysql 362 Apr 26 2018 ib_buffer_pool -rw-r----- 1 mysql mysql 2818572288 May 13 14:41 ibdata1 -rw-r----- 1 mysql mysql 158792155136 May 13 14:40 ibtmp1 drwxr-x--- 2 mysql mysql 4096 Apr 18 2018 infra drwxr-x--- 2 mysql mysql 4096 Apr 18 2018 mysql

经过系统优化和业务协调需要做MySQL实例重启,已重置为初始大小,设置阈值为10G。

SQL层优化

SQL语句的优化分析发现,基于json类型的解析差异和字符类型存在较大的性能差异,建议对json的子查询创建临时表。

测试步骤如下:

create table dic_fsm_info3 (classid varchar(30),etime varchar(30));    --可以根据业务特点创建索引
mysql> insert into dic_fsm_info3 select  distinct json_extract(fsm_info,'$.FSM.ClassID') ,json_extract(fsm_info,'$.FSM.ETime') from tgp_db.dic_fsm_info where state=0;
Query OK, 334 rows affected (0.12 sec)
Records: 334  Duplicates: 0  Warnings: 0

重新执行语句,执行时长优化只0.2秒左右。

select distinct class_id from tgp_db.dic_fsm_map_relation
where game_id = 1
and state = 0
and class_id not in ( 
    SELECT distinct classid 
    FROM dic_fsm_info3 
    where etime > unix_timestamp(now())
)

|     2704 |
|     2705 |
|     2707 |
|     2715 |
+----------+
73 rows in set (0.23 sec)

JSON类型的解析效率可以通过profile的对比方式来分析:

mysql> show profile cpu for query 1;
+--------------------+----------+----------+------------+
| Status             | Duration | CPU_user | CPU_system |
+--------------------+----------+----------+------------+
| Sending data       | 0.047225 |     NULL |       NULL |
| executing          | 0.000002 |     NULL |       NULL |
| Sending data       | 0.047196 |     NULL |       NULL |
| executing          | 0.000004 |     NULL |       NULL |

而根据字符类型匹配,效率要高两个数量级。

+--------------------+----------+----------+------------+
| Status             | Duration | CPU_user | CPU_system |
+--------------------+----------+----------+------------+
| Sending data       | 0.000128 |     NULL |       NULL |
| executing          | 0.000001 |     NULL |       NULL |
| Sending data       | 0.000126 |     NULL |       NULL |
| executing          | 0.000001 |     NULL |       NULL |

后续对JSON类型的使用也需要注意以下。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-05-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 杨建荣的学习笔记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档