Mysql primary key association does not use index
I have a SQL problem.
When the user, organization, and organization are associated with the table, if the user status is used to filter the table, the index user_id cannot be used. If the condition is removed, the index user_id will be used.
Why is that?
MSYQL VERSION:5.7.32-log
Below is the specific SQL and table structure.
sql 1 :
SELECT DISTINCT USER
.user_id,
USER.NAME,
USER.nickname,
USER.position,
USER.first_line_id,
USER.second_line_id,
USER.org_id,
user.state
FROM
USER INNER JOIN user_org ON USER.user_id = user_org.user_id
INNER JOIN org ON user_org.org_id = org.id
WHERE
( org.end_time IS NULL OR org.end_time > NOW( ) )
AND USER.state = 1
AND ( full_id LIKE 'H_ROOT.00000001.00000002.50060182.50091585.50095679.50092012.10148706.50092333.10161139%' )
explain:user_id index not sufficient
sql2 :
SELECT DISTINCT USER
.user_id,
USER.NAME,
USER.nickname,
USER.position,
USER.first_line_id,
USER.second_line_id,
USER.org_id,
user.state
FROM
USER INNER JOIN user_org ON USER.user_id = user_org.user_id
INNER JOIN org ON user_org.org_id = org.id
WHERE
( org.end_time IS NULL OR org.end_time > NOW( ) )
-- AND USER.state = 1
AND ( full_id LIKE 'H_ROOT.00000001.00000002.50060182.50091585.50095679.50092012.10148706.50092333.10161139%' )
explain:user_id index sufficient
table count
USER:356007
ORG:142713
USER_ORG:353088
table schema
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `user_org`;
CREATE TABLE `user_org` (
`user_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`org_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`created_at` datetime(0) NULL DEFAULT NULL,
`updated_at` datetime(0) NULL DEFAULT NULL,
PRIMARY KEY (`user_id`, `org_id`) USING BTREE,
INDEX `org_id`(`org_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`user_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '工号',
`name` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '姓名',
`email` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '邮箱',
`email_private` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '个人邮箱',
`mobile` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '手机号',
`position` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '岗位',
`state` tinyint(4) NOT NULL DEFAULT 1 COMMENT '状态(1:启用;0:禁用)',
`org_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '部门编码',
PRIMARY KEY (`user_id`) USING BTREE,
INDEX `user_email_index`(`email`) USING BTREE,
INDEX `user_mobile_index`(`mobile`) USING BTREE,
INDEX `user_name_index`(`name`) USING BTREE,
INDEX `user_org_id_index`(`org_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '用户表' ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `org`;
CREATE TABLE `org` (
`id` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`name` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`parent_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`full_id` varchar(512) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`end_time` datetime(0) NULL DEFAULT NULL COMMENT '部门过期时间',
`created_at` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '创建时间',
`updated_at` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '更新时间',
`customer_code` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '',
`org_type` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '组织类别',
`state` tinyint(4) NULL DEFAULT NULL COMMENT ' 1 正常 2 停用\r\n冗余目前还是用endtime来识别有效性',
PRIMARY KEY (`id`) USING BTREE,
INDEX `org_full_id_index`(`full_id`(255)) USING BTREE,
INDEX `org_name_index`(`name`(255)) USING BTREE,
INDEX `org_parent_id_index`(`parent_id`) USING BTREE,
INDEX `end_time`(`end_time`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '组织表' ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;