当前位置: 首页 > news >正文

mysql not in 查询引发的bug问题记录

mysql not in 查询引发的bug问题记录

  • 数据准备
    • 版本信息
    • 建表语句
    • 测试数据
  • 问题说明
    • 使用 not in 查询
  • 问题分析以及正确写法
    • 执行逻辑
    • 错误问题分析
    • 性能分析
    • 正确写法

数据准备

版本信息

mysql 8.0.13

mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.13    |
+-----------+
1 row in set (0.00 sec)

建表语句

create table t_null_test(id bigint primary key auto_increment,city varchar(100) comment '居住城市',user_id bigint comment '用户ID'
) charset=utf8mb4 comment = 'not in null测试表';create table t_user(
id bigint primary key auto_increment,
name varchar(50) comment '姓名'
) charset=utf8mb4;

查看表信息
show create table t_null_test\G

mysql> show create table t_null_test\G;
*************************** 1. row ***************************Table: t_null_test
Create Table: CREATE TABLE `t_null_test` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`city` varchar(100) DEFAULT NULL COMMENT '居住城市',`user_id` bigint(20) DEFAULT NULL COMMENT '用户ID',PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='not in null测试表'
1 row in set (0.00 sec)ERROR:
No query specifiedmysql> show create table t_user\G;
*************************** 1. row ***************************Table: t_user
Create Table: CREATE TABLE `t_user` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`name` varchar(50) DEFAULT NULL COMMENT '姓名',PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)ERROR:
No query specified

测试数据

insert into t_null_test(city, user_id) values('佛山', 'cd'),('广州', 'scd'), ('深圳', 'ss'), ('东莞', null);
mysql> insert into t_null_test(city, user_id) values('佛山', 1),('广州', 2), ('深圳', 3), ('东莞', null);
Query OK, 4 rows affected (0.16 sec)
Records: 4  Duplicates: 0  Warnings: 0mysql> select * from t_null_test;
+----+------+---------+
| id | city | user_id |
+----+------+---------+
|  1 | 佛山 |       1 |
|  2 | 广州 |       2 |
|  3 | 深圳 |       3 |
|  4 | 东莞 |    NULL |
+----+------+---------+
4 rows in set (0.00 sec)mysql> insert into t_user(name) values('scd'),('cd'),('ss'),('sss');
Query OK, 4 rows affected (0.18 sec)
Records: 4  Duplicates: 0  Warnings: 0mysql> select * from t_user;
+----+------+
| id | name |
+----+------+
|  1 | scd  |
|  2 | cd   |
|  3 | ss   |
|  4 | sss  |
+----+------+
4 rows in set (0.00 sec)

问题说明

使用 not in 查询

mysql> select * from t_user where id not in (select user_id from t_null_test);
Empty set (0.07 sec)

这条有问题的sql 主要的功能是从t_user表中筛选出那些id值不在t_null_test表的user_id列中的记录,查询出的结果为空,实际上是有一个id=4不存在 t_null_test表的

问题分析以及正确写法

执行逻辑

  1. 先执行子查询select user_id from t_null_test获取所有user_id值
  2. 主查询检查t_user表中每条记录的id是否不在子查询结果集中
  3. 最终返回满足条件的完整用户记录

错误问题分析

NOT IN在处理包含NULL值的子查询时可能返回意外结果,因为NULL值的比较会返回UNKNOWN而非TRUE/FALSE

性能分析

当子查询结果集较大时性能较差,因为它需要对子查询结果进行哈希匹配

正确写法

  1. 过滤 null 数据
mysql> select * from t_user where id not in (select user_id from t_null_test where user_id is not null);
+----+------+
| id | name |
+----+------+
|  4 | sss  |
+----+------+
1 row in set (0.00 sec)
  1. 使用 not exists (推荐)
mysql> select * from t_user u where not exists (select 1 from t_null_test t where u.id=t.user_id);
+----+------+
| id | name |
+----+------+
|  4 | sss  |
+----+------+
1 row in set (0.00 sec)
  1. 使用left join 过滤 null
mysql> explain select * from t_user u left join t_null_test t on u.id=t.user_id where t.user_id is null;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra                                              |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+
|  1 | SIMPLE      | u     | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    4 |   100.00 | NULL                                               |
|  1 | SIMPLE      | t     | NULL       | ALL  | idx_user_id   | NULL | NULL    | NULL |    4 |    25.00 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+
2 rows in set, 1 warning (0.00 sec)

为啥推荐 not exists, 查看sql 执行计划分析

mysql> explain select * from t_user u where not exists (select 1 from t_null_test t where u.id=t.user_id);
+----+--------------------+-------+------------+------+---------------+-------------+---------+-----------+------+----------+-------------+
| id | select_type        | table | partitions | type | possible_keys | key         | key_len | ref       | rows | filtered | Extra       |
+----+--------------------+-------+------------+------+---------------+-------------+---------+-----------+------+----------+-------------+
|  1 | PRIMARY            | u     | NULL       | ALL  | NULL          | NULL        | NULL    | NULL      |    4 |   100.00 | Using where |
|  2 | DEPENDENT SUBQUERY | t     | NULL       | ref  | idx_user_id   | idx_user_id | 9       | test.u.id |    1 |   100.00 | Using index |
+----+--------------------+-------+------------+------+---------------+-------------+---------+-----------+------+----------+-------------+
2 rows in set, 2 warnings (0.00 sec)mysql> explain select * from t_user where id not in (select user_id from t_null_test where user_id is not null);
+----+--------------------+-------------+------------+----------------+---------------+-------------+---------+------+------+----------+--------------------------+
| id | select_type        | table       | partitions | type           | possible_keys | key         | key_len | ref  | rows | filtered | Extra                    |
+----+--------------------+-------------+------------+----------------+---------------+-------------+---------+------+------+----------+--------------------------+
|  1 | PRIMARY            | t_user      | NULL       | ALL            | NULL          | NULL        | NULL    | NULL |    4 |   100.00 | Using where              |
|  2 | DEPENDENT SUBQUERY | t_null_test | NULL       | index_subquery | idx_user_id   | idx_user_id | 9       | func |    2 |   100.00 | Using where; Using index |
+----+--------------------+-------------+------------+----------------+---------------+-------------+---------+------+------+----------+--------------------------+
2 rows in set, 1 warning (0.02 sec)mysql> explain select * from t_user u left join t_null_test t on u.id=t.user_id where t.user_id is null;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra                                              |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+
|  1 | SIMPLE      | u     | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    4 |   100.00 | NULL                                               |
|  1 | SIMPLE      | t     | NULL       | ALL  | idx_user_id   | NULL | NULL    | NULL |    4 |    25.00 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+
2 rows in set, 1 warning (0.00 sec)

扫描行数计算 rows * filtered%

  1. not exists = 1 * 100% = 1
  2. not in = 2 * 100% = 2
  3. left join … not null = 4 * 25% = 1
http://www.dtcms.com/a/286057.html

相关文章:

  • Microsoft 365 Adoption Score功能深度解析:驱动企业数字化转型的利器
  • 网安-DNSlog
  • 第三章自定义检视面板_创建自定义编辑器类_检测与应用修改(本章进度(2/9))
  • 可靠消息最终一致性分布式事务解决方案
  • 补贴退坡、平价上网,数字隔离器如何守护更高功率的光伏逆变器?
  • 门控线性单元GLU (Gated Linear Unit)
  • ApplicationContext 事件发布与监听机制详解
  • 反射机制的登录系统
  • PHP 8.0 升级到 PHP 8.1
  • 创建型模式
  • 基于 HT 的 3D 可视化智慧矿山开发实现
  • 从一开始的网络攻防(四):XSS
  • hadoop(服务器伪分布式搭建)
  • FastAdmin后台登录地址变更原理与手动修改方法-后台入口机制原理解析-优雅草卓伊凡
  • Hadoop安全机制深度剖析:Kerberos认证与HDFS ACL细粒度权限控制
  • 《Web安全之深度学习实战》读书笔记总结
  • AI赋能轮胎安全:基于YOLO11的智能裂纹检测系统
  • 基于springboot+vue+mysql的智慧社区设计与实现(源码+论文+开题报告)
  • Docker Swarm 集群使用记录
  • Matlab打开慢、加载慢的解决办法
  • 免费的一些工具收集
  • 【Oracle】centos7离线静默安装oracle11g(p13390677_112040)
  • Hive 向量化执行引擎 Vectorized Execution 常见 NPE 报错分析及解决
  • 全球天气预报5天(经纬度版)免费API接口教程
  • Python绘制数据(二)
  • JAVA面试宝典 -《微服务治理:从链路追踪到熔断》
  • 某邮生活旋转验证码识别
  • 算法竞赛备赛——【图论】求最短路径——小结
  • 前端之CSS
  • MyBatis之关联查询