惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
V
V2EX
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
Martin Fowler
Martin Fowler
小众软件
小众软件
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
The Cloudflare Blog
T
Tailwind CSS Blog
H
Help Net Security
腾讯CDC
爱范儿
爱范儿
人人都是产品经理
人人都是产品经理
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The GitHub Blog
The GitHub Blog
Microsoft Security Blog
Microsoft Security Blog
Stack Overflow Blog
Stack Overflow Blog
D
DataBreaches.Net
C
Check Point Blog
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

祈雨的笔记

安全多方计算MPC spark原理解析 kueue执行源码分析 spark on k8s执行源码分析 spark-operator源码解析 系统压测遇到的缓存击穿问题 我的世界PC与安卓联机 蚂蚁金服流量投放平台的AIG改造 G1大对象致Old区占用率高 日志打印导致接口响应率下跌分析 Groovy加载类导致OOM分析 ERROR日志打印导致CPU满载 记OceanBase死锁超时 应用发版期间服务响应超时 Ark Serverless初探 系统优化复盘一二三 The user specified as a definer does not exist Kong网关初探 API网关选型调研 CPU火焰图常用工具 配置中心选型调研 root操作Nginx导致用户组错误 基于Proxifier使用代理 FastJSON字段智能匹配踩坑 Nacos初探 记一次Nginx服务器CPU满荷载故障 基于券系统分库分表的思考 limit不参与SQL成本计算致索引失效 Linux常用性能监控命令 golang低版本http2偶现400
MySQL表字符集不同导致关联查询索引失效
祈雨的笔记 · 2020-03-17 · via 祈雨的笔记

概述

mysql在多表之间做关联查询时,需要注意各个表的字符集是否一致。如果在字符集不一致的场景下做关联查询,会出现关联字段即使有索引,但却索引失效的问题。

复现

建表语句,创建两个字符集不同的表,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
CREATE TABLE `school` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`school_code` varchar(255) DEFAULT NULL,
`school_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `code_index` (`school_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`school_code` varchar(255) DEFAULT NULL,
`student_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `code_index` (`school_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

初始化数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
delimiter  //
CREATE PROCEDURE init_school()
BEGIN
DECLARE schid INT;
SET schid = 0;
START TRANSACTION;
WHILE schid < 100 DO
insert into school(school_code,school_name) values (concat('code',schid),concat('name',schid));
SET schid = schid + 1;
END WHILE;
COMMIT;
END //
delimiter ;

delimiter //
CREATE PROCEDURE init_student()
BEGIN
DECLARE schid INT;
DECLARE stuid INT;
SET schid = 0;
WHILE schid < 100 DO
SET stuid = 0;
START TRANSACTION;
WHILE stuid < 50 DO
insert into student(school_code,student_name) values (concat('code',schid),concat('stu_name',stuid));
SET stuid = stuid + 1;
END WHILE;
SET schid = schid + 1;
COMMIT;
END WHILE;
END //
delimiter ;

call init_school();
call init_student();

在两个表都在school_code字段上建立索引的情况下,以该字段关联查询预期应该使用索引,但实际效果如下,通过explain命令发现mysql的执行计划并没有使用索引。

1
2
3
4
5
6
7
8
explain select * from school s1,student s2 where s1.school_code = s2.school_code and s2.school_code = 'code10';

+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+
| 1 | SIMPLE | s2 | NULL | ref | code_index | code_index | 1023 | const | 50 | 100 | NULL |
| 1 | SIMPLE | s1 | NULL | ALL | NULL | NULL | NULL | NULL | 100 | 100 | Using where; Using join buffer (Block Nested Loop) |
+

解决方案

将两个表的编码改为一致,均为utf8mb4后,在执行该关联查询语句后,发现效果与预期相同,mysql的执行计划使用了索引。

1
2
3
4
5
6
7
8
explain select * from school s1,student s2 where s1.school_code = s2.school_code and s2.school_code = 'code10';

+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+
| 1 | SIMPLE | s1 | NULL | ref | code_index | code_index | 1023 | const | 1 | 100 | NULL |
| 1 | SIMPLE | s2 | NULL | ref | code_index | code_index | 1023 | const | 50 | 100 | Using index condition |
+