前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >尴尬的bug:一条查询语句让MySQL崩溃

尴尬的bug:一条查询语句让MySQL崩溃

作者头像
jeanron100
发布2019-07-05 11:09:45
2.2K0
发布2019-07-05 11:09:45
举报

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

前几天睡觉前接到前同事的一个信息,说有个奇怪的SQL问题,想让我帮忙看看,给点建议,我以为是一种非常复杂的SQL,他的反馈能让MySQL崩溃。

我简单看了下,感觉不大可能啊,于是在自己的环境做了测试。

相关的SQL会关联两张表,我们就称为t10和t10_sub吧。

建表语句如下:

CREATE TABLE `t10` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`pid` varchar(10) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `t10_sub` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`pid` varchar(10) DEFAULT NULL,

`cid` varchar(10) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

接下来我们直接进入正题,首先先来做一个查询,因为表里没有数据,所以查询的过程还是快的,显然是返回一个空集。

mysql> (select sub_tmp.cid, (select count(*) from t10 where pid = sub_tmp.pid ) as new_t10 from t10_sub as sub_tmp order by new_t10 + 1) union (select sub_tmp.id,sub_tmp.id from t10_sub as sub_tmp);

Empty set (0.00 sec)

而奇怪的是如果查看执行计划,则整条语句会导致当前会话崩溃。

mysql> explain (select sub_tmp.cid, (select count(*) from t10 where pid = sub_tmp.pid ) as new_t10 from t10_sub as sub_tmp order by new_t10 + 1) union (select sub_tmp.id,sub_tmp.id from t10_sub as sub_tmp);

ERROR 2013 (HY000): Lost connection to MySQL server during query

当然这个操作是可以复现,通过这个

写入1条数据

mysql> insert into t10(pid) values('test');

mysql> insert into t10_sub(pid,cid) values('test','test100');

数据也没什么特别之处,我们来继续执行查询。

mysql> (select sub_tmp.cid, (select count(*) from t10 where pid = sub_tmp.pid ) as new_t10 from t10_sub as sub_tmp order by new_t10 + 1) union (select sub_tmp.id,sub_tmp.id from t10_sub as sub_tmp);

ERROR 2013 (HY000): Lost connection to MySQL server during query

可以看到这次结果比较统一,执行计划解析和查询操作都会导致会话崩溃。

那目前有什么好的解决办法吗?

经过测试,目前有两种,一种是去除union的子句,一种是对于order by的部分做下调整,原来是order by new_t10+1,现在修改为order by new_t10.

mysql> explain (select sub_tmp.cid, (select count(*) from t10 where pid = sub_tmp.pid ) as new_t10 from t10_sub as sub_tmp order by new_t10 ) union (select sub_tmp.id,sub_tmp.id from t10_sub as sub_tmp);

+----+--------------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------+

| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |

+----+--------------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------+

| 1 | PRIMARY | sub_tmp | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | NULL |

| 2 | DEPENDENT SUBQUERY | t10 | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | Using where |

| 3 | UNION | sub_tmp | NULL | index | NULL | PRIMARY | 4 | NULL | 1 | 100.00 | Using index |

| NULL | UNION RESULT | <union1,3> | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | Using temporary |

+----+--------------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------+

4 rows in set, 2 warnings (0.00 sec)

当然在这里也是抛砖引玉,想看看大家有什么好的想法和调试办法。

毫无疑问,这是一个bug.

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

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

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

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

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