

MySQL表字符集不同导致关联查询索引失效
source link: https://wakzz.cn/2020/03/18/mysql/MySQL%E8%A1%A8%E5%AD%97%E7%AC%A6%E9%9B%86%E4%B8%8D%E5%90%8C%E5%AF%BC%E8%87%B4%E5%85%B3%E8%81%94%E6%9F%A5%E8%AF%A2%E7%B4%A2%E5%BC%95%E5%A4%B1%E6%95%88/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

MySQL表字符集不同导致关联查询索引失效
mysql在多表之间做关联查询时,需要注意各个表的字符集是否一致。如果在字符集不一致的场景下做关联查询,会出现关联字段即使有索引,但却索引失效的问题。
建表语句,创建两个字符集不同的表,如下:
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;
初始化数据
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的执行计划并没有使用索引。
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的执行计划使用了索引。
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 |
+----+-------------+-------+------------+------+---------------+------------+---------+-------+------+----------+-----------------------+
Recommend
-
30
问题概述 写代码的时候,有一段sql,表示该sql存在隐式转换,不走索引。 经过测试排查后,发现是类型varchar的字段, 我使...
-
21
最近一直忙着处理原来老项目遗留的一些SQL优化问题,由于当初表的设计以及字段设计的问题,随着业务的增长,出现了大量的慢SQL,导致MySQL的CPU资源飙升,基于此,给大家简单分享下这些比较使用的易于学习和使用的经验。这次的话简单说下如何防止你的索引失效。再...
-
21
随着业务的增长,出现了大量的慢SQL,导致MySQL的CPU资源飙升,基于此,给大家简单分享下这些比较使用的易于学习和使用的经验。
-
18
MySQL charset不同导致无法使用索引的坑 今天排查了一个MySQL Charset不同导致无法使用索引的坑。 当然最开始我是不知道的,同事碰到一个性能问题,我也在群里,因此就捞过界了一把,一起看了一下问题。但是从SQL来说应该是 充分利用了SQL才...
-
20
模拟数据 注:在使用下面数据的时候建议将每一条都调整到一行进行插入,每条记录的_id是不一样的。{ "_id" : ObjectId("606ed113137fa535ac53d3ee"), "key1" : null, "key2" : 1033072826, "key3...
-
4
群里一个小伙伴在问为什么MySQL字符串不加单引号会导致索引失效,这个问题估计很多人都知道答案。没错,是因为MySQL内部进行了隐式转换。本期文章就聊聊什么是隐式转换,为什么会发生隐式转换。
-
4
1. MySQL索引概述 1.1 索引的概念 什么是索引,索引就是排好序的快速查找数据结构。 1.2 索引的特点 索引的优点 1.提高数据检索的效率, 降低数据库的IO成本。 2.通过索引列对数据进行排...
-
6
我之前写的一篇文章《
-
10
mysql字符集utf8mb4失效踩坑 祈雨的博客 2019-08-17
-
7
本文分享自华为云社区《GaussDB(DWS)之查询时索引失效原因》,...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK