全在说梦话 全在说梦话
关注数: 13 粉丝数: 23 发帖数: 742 关注贴吧数: 20
添加索引后 导致查询结果不一样 感觉像是 and 条件不起作用了 1.初始化表SQL: 创建表 'ai_material_type' ( 'id' int 不空 AUTO_INCREMENT, 'type' varchar(255) 字符集 UTF8MB4 整理 utf8mb4_unicode_ci 空默认值 NULL,主键 ('id') 使用 BTREE) 引擎 = InnoDB AUTO_INCREMENT = 5 个字符集 = UTF8MB4 整理 = utf8mb4_unicode_ci ROW_FORMAT = 动态; 插入“ai_material_type”值(1,“A”); 插入“ai_material_type”值(2,“B”); 插入“ai_material_type”值(3,“C”); 插入“ai_material_type”值(4,“D”); 创建表 'ai_material_customer' ( 'id' int NOT NULL AUTO_INCREMENT, 'customer_code' VARCHAR(255) 字符集 UTF8MB4 整理 utf8mb4_unicode_ci 空默认值 NULL, 'material_id' INT NULL 默认 NULL, 主键 ('id') 使用 BTREE) 引擎 = InnoDB AUTO_INCREMENT = 16 个字符集 = UTF8MB4 排序 = utf8mb4_unicode_ci ROW_FORMAT = 动态; 插入“ai_material_customer”值(1,“100002”,1); 插入“ai_material_customer”值(2,“100003”,2); 插入“ai_material_customer”值(3,“100004”,3); 插入“ai_material_customer”值(4,“100002”,4); 插入“ai_material_customer”值(5,“100003”,5); 插入“ai_material_customer”值(6,“100004”,6); 插入“ai_material_customer”值(7,“100002”,7); 插入“ai_material_customer”值(8,“100003”,7); 插入“ai_material_customer”值(9,“100003”,8); 插入“ai_material_customer”值(10,“100002”,8); 插入“ai_material_customer”值(11,“100004”,8); 插入“ai_material_customer”值(12,“100003”,9); 插入“ai_material_customer”值(13,“100004”,9); 插入“ai_material_customer”值(14,“100005”,9); 插入“ai_material_customer”值(15,“0”,10); 创建表 'ai_material' ( 'id' int NOT NULL AUTO_INCREMENT, 'type_id' INT NULL 默认 NULL, 主键 ('id') 使用 BTREE, 索引 'ai_material_type_index'('type_id' ASC) 使用 BTREE) 引擎 = InnoDB AUTO_INCREMENT = 11 个字符集 = utf8MB4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = 动态; 插入“ai_material”值(1,1); 插入“ai_material”值(2,1); 插入“ai_material”值(3,1); 插入“ai_material”值(4,2); 插入“ai_material”值(5,2); 插入“ai_material”值(6,2); 插入“ai_material”值(7,3); 插入到“ai_material”值(8,3); 插入“ai_material”值(9,3); 插入“ai_material”值(10,3); 2. 执行下面SQL 查看结果 选择 t.类型, 计数( m.id ) c 从 ai_material_type 吨 左连接 ai_material m 在 t.id = m.type_id 和 m.id ( 从ai_material_customer中选择material_id customer_code IN ( '100002', '0' ) ) 按 t.id 分组 3. 创建索引 在 ai_material 上使用 BTREE 创建索引ai_material_type_index (type_id); 4. 再次查询第2步骤 的结果
1 下一页