引言
在数据库日常运维中,“查询结果不符合预期” 是高频问题,但多数情况可归因于 sql 语法、数据异常或索引设计。而本次遇到的案例,却源于 mysql 的底层 bug—— 明明数据存在,单一条件查询正常,叠加一个过滤条件后竟返回空集,着实令人费解。本文将完整还原问题场景、排查过程,以及最终的解决方案。
1. 问题背景
数据库版本:mysql8.0.40
假设我们创建了一个名为 product_info 的表,用于存储产品的相关信息。该表包含三个字段:product_id(产品编号)、category_id(类别编号)和 brand_id(品牌编号)。其中,product_id 被设置为主键,并且采用降序排列。
create table product_info( product_id varchar(32) collate utf8mb4_general_ci not null comment '产品编号', category_id varchar(32) collate utf8mb4_general_ci default null comment '类别编号', brand_id varchar(32) collate utf8mb4_general_ci default null comment '品牌编号', primary key(`product_id` desc), key `idx_brand_id`(`brand_id`), key idx_category_id(category_id))default charset=utf8mb4 collate=utf8mb4_general_ci;
以下是创建表的 sql 语句:随后,我们向表中插入了一些数据:
insert into product_info values('p001','c01','b02'),('p002','c02','b01'),('p003','c02','b01'),('p004','c01','b02'),('p005','c03','b01'),('p006','c03','b01');数据插入完成后,我们进行了两次查询操作。第一次查询是筛选出 category_id 为 c02 的记录:
select * from product_info where category_id='c02';
这次查询正常返回了两条记录,结果如下:
+------------+-------------+----------+| product_id | category_id | brand_id |+------------+-------------+----------+| p003 | c02 | b01 || p002 | c02 | b01 |+------------+-------------+----------+
然而,当我们进行第二次查询,增加了 brand_id 为 b01 的条件时:
mysql> select * from product_info where category_id='c02' and brand_id='b01';empty set (0.00 sec)
本应返回上述两条记录,但实际结果却为空集,这显然与预期不符。

2. 问题分析及排查
2.1 字符集和校对规则方面
表和字段都采用了 utf8mb4_general_ci 字符集和校对规则。通常情况下,对于数字和字母组成的字符串比较,这种校对规则不会出现问题。但我们不能排除隐式类型转换或者存在不可见字符的可能性。为了验证这一点,我们可以使用 hex 函数查看 brand_id 的实际存储值:
select product_id, category_id, brand_id, hex(brand_id) from product_info where category_id='c02';

如果 brand_id 的值确实是 b01,那么 hex 函数的结果应该是 423031。若结果中出现其他字符,比如尾随空格,可能会导致比较时出现不匹配的情况。但是此案例明显不是。
2.2 索引相关问题
索引选择问题
当执行组合条件查询时,优化器可能会选择不合适的索引。对于 select * from product_info where category_id='c02' and brand_id='b01' 这个查询,优化器可能只选择了 idx_category_id 或 idx_brand_id 其中一个索引,从而无法有效地结合两个条件进行查询。
mysql> select * from product_info force index (idx_category_id) where category_id='c02' and brand_id='b01';+------------+-------------+----------+| product_id | category_id | brand_id |+------------+-------------+----------+| p003 | c02 | b01 || p002 | c02 | b01 |+------------+-------------+----------+2 rows in set (0.00 sec)

mysql> select * from product_info force index (idx_brand_id) where category_id='c02' and brand_id='b01';+------------+-------------+----------+| product_id | category_id | brand_id |+------------+-------------+----------+| p003 | c02 | b01 || p002 | c02 | b01 |+------------+-------------+----------+

可见强制走其中一个索引都能正常
索引合并问题
以上可以看出优化器选择使用索引合并(如 index merge intersect),将 idx_category_id 和 idx_brand_id 的结果合并,但由于主键降序排列等因素,可能会导致两个索引的结果无法正确交集,进而出现查询结果为空的情况。因此我们关闭index_merge_intersection或者index_merge测试一下:
mysql> set optimizer_switch='index_merge_intersection=off';query ok, 0 rows affected (0.00 sec) mysql> select * from product_info force index (idx_brand_id) where category_id='c02' and brand_id='b01';+------------+-------------+----------+| product_id | category_id | brand_id |+------------+-------------+----------+| p003 | c02 | b01 || p002 | c02 | b01 |+------------+-------------+----------+2 rows in set (0.00 sec)

关闭后确实可以了。另外关闭
2.3 主键降序排列的影响
二级索引结构
主键采用降序排列可能会对二级索引的存储结构和扫描方向产生影响。在查询时,可能会因为这种影响导致索引无法正常工作,从而无法正确检索到符合条件的记录。
我们建一张product_info2表,再导入原样的数据,再查询一遍
mysql> create table product_info2( -> product_id varchar(32) collate utf8mb4_general_ci not null comment '产品编号', -> category_id varchar(32) collate utf8mb4_general_ci default null comment '类别编号', -> brand_id varchar(32) collate utf8mb4_general_ci default null comment '品牌编号', -> primary key(`product_id` ), -> key `idx_brand_id`(`brand_id`), -> key idx_category_id(category_id) -> ) -> default charset=utf8mb4 collate=utf8mb4_general_ci;query ok, 0 rows affected (0.01 sec) mysql> insert into product_info2 select * from product_info;query ok, 6 rows affected (0.01 sec)records: 6 duplicates: 0 warnings: 0 mysql> set optimizer_switch='index_merge_intersection=off';query ok, 0 rows affected (0.00 sec) mysql> set optimizer_switch='index_merge_intersection=on';query ok, 0 rows affected (0.00 sec) mysql> select * from product_info where category_id='c02' and brand_id='b01';empty set (0.00 sec) mysql> select * from product_info2 where category_id='c02' and brand_id='b01';+------------+-------------+----------+| product_id | category_id | brand_id |+------------+-------------+----------+| p002 | c02 | b01 || p003 | c02 | b01 |+------------+-------------+----------+2 rows in set (0.00 sec)

通过对比可以发现,修改为非降序索引后确实也正常了。
2.4 mysql 版本兼容性
不同的 mysql 版本对降序索引的支持和处理方式可能存在差异。某些旧版本可能存在与降序索引相关的 bug,导致在使用降序主键和二级索引进行查询时出现问题。出现问题的版本是mysql8.0.40,我们用mysql8.0.41再看一下,发现新版本已经解决

3. 小结
本次问题的本质是 mysql 8.0.40 版本中,降序主键与索引合并交集模式的底层逻辑冲突—— 二级索引的存储结构受降序主键影响,导致索引合并时无法正确计算结果交集,最终查询 “丢失” 数据。通过逐层排查,我们定位了核心诱因,并提供了紧急规避与长期优化方案,即:
- 尽量不要使用降序主键,如需使用降序特性,建议创建二级索引解决
- 如非必要不要开启index_merge或index_merge_intersection,以免导致性能问题或检索错误问题,如果需要,可以考虑先建组合索引解决
- 以上案例和数据自身也有关系,只是部分数据会出现此情况,大家如需复现可以用我案例中的数据进行测试
因此,在平时数据库运维中,看似 “匪夷所思” 的异常,往往与版本 bug、索引策略或表结构设计相关。遇到类似问题时,可按 “验证数据→排查索引→测试版本兼容性” 的思路定位,同时优先选择经过实践验证的表结构与索引设计方案,降低踩坑概率。
以上就是mysql隐蔽bug:组合条件查询无故返回空集的排查与规避方案的详细内容,更多关于mysql bug组合条件查询无故返回空集的资料请关注代码网其它相关文章!
发表评论