mysql多表关联,未走索引
接手前人代码,今天架构发了条sql给我,有个子查询走了全表,让我处理一下,如图
SELECT lr.id, lr.content, lr.create_time AS createTime, lr.status, (SELECT feedback_content FROM member_complaints_feedback WHERE member_complaints_id = lr.id) AS feedbackContent, (SELECT image_address FROM member_complaints_img WHERE member_complaints_id = lr.id) AS imageAddress FROM member_complaints lr WHERE lr.uid = '1E759FF1-CAC9-4527-AC78-C9173E5903D9' ORDER BY lr.create_time desc
我心想:这么简单的sql,给 member_complaints_img 表加个索引不就轻轻松松解决?
然后我发现:
我就纳闷了,既然有索引,为什么没走呢?
member_complaints_img 表和 member_complaints_feedback 表的索引一样呀,为什么 member_complaints_feedback 表走了索引呢?
然后我把这两张表的结构导出:
仔细一看,member_complaints_id 列字符编码不同,看一下数据库的编码:
建 member_complaints_img 表的前辈真是人才!甜蜜的!两张表的字符编码不同!你猜我排查了多久才想到字符编码!
修改字符编码后:
ALTER TABLE member_complaints_img MODIFY member_complaints_id varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '用户投诉表id';
解决!