Skip to content

Commit c020d36

Browse files
committed
MDEV-7474: Semi-Join's DuplicateWeedout strategy skipped ...
JOIN::cur_dups_producing_tables was not maintained correctly in the cases of greedy optimization (search_depth < n_tables). Moved it to POSITION structure where it will be maintained automatically. Removed POSITION::prefix_dups_producing_tables since its value can now be calculated.
1 parent 5a3bf84 commit c020d36

File tree

8 files changed

+341
-16
lines changed

8 files changed

+341
-16
lines changed

mysql-test/r/subselect_sj2.result

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,5 +1178,89 @@ id nombre
11781178
2 row 2
11791179
3 row 3
11801180
DROP TABLE t1, t2;
1181+
#
1182+
# MDEV-7474: Semi-Join's DuplicateWeedout strategy skipped for some values of optimizer_search_depth
1183+
#
1184+
CREATE TABLE t1 (
1185+
t1id BIGINT(20) NOT NULL,
1186+
code VARCHAR(20),
1187+
PRIMARY KEY (t1id)
1188+
) COLLATE='utf8mb4_bin' ENGINE=InnoDB;
1189+
CREATE TABLE t2 (
1190+
t2id BIGINT(20) NOT NULL,
1191+
t1idref BIGINT(20) NOT NULL,
1192+
code VARCHAR(20),
1193+
PRIMARY KEY (t2id),
1194+
INDEX FK_T2_T1Id (t1idref),
1195+
CONSTRAINT FK_T2_T1Id FOREIGN KEY (t1idref) REFERENCES t1 (t1id)
1196+
) COLLATE='utf8mb4_bin' ENGINE=InnoDB;
1197+
CREATE TABLE t3 (
1198+
t3idref BIGINT(20) NOT NULL,
1199+
t2idref BIGINT(20) NOT NULL,
1200+
sequencenumber INT(10) NOT NULL,
1201+
PRIMARY KEY (t3idref, t2idref),
1202+
INDEX FK_T3_T2Id (t2idref),
1203+
CONSTRAINT FK_T3_T2Id FOREIGN KEY (t2idref) REFERENCES t2 (t2id)
1204+
) COLLATE='utf8mb4_bin' ENGINE=InnoDB;
1205+
INSERT INTO t1 (t1id) VALUES (100001),(100017),(100018),(100026),(100027),(100028),(100029),(100030),
1206+
(100031),(100032),(100033),(100034),(100035),(100036),(100037),(100038),(100040),(100041),(100042),
1207+
(100043),(100044),(100045),(100046),(100047);
1208+
INSERT IGNORE INTO t2 (t2id, t1idref) SELECT t1id, t1id FROM t1;
1209+
INSERT IGNORE INTO t1 VALUES (200001, 'a');
1210+
INSERT IGNORE INTO t2 (t2id, t1idref) VALUES (200011, 200001),(200012, 200001),(200013, 200001);
1211+
INSERT IGNORE INTO t3 VALUES (1, 200011, 1), (1, 200012, 2), (1, 200013, 3);
1212+
set @tmp7474= @@optimizer_search_depth;
1213+
SET SESSION optimizer_search_depth = 1;
1214+
SELECT SQL_NO_CACHE
1215+
T2_0_.t1idref,
1216+
T2_0_.t2id
1217+
FROM
1218+
t2 T2_0_
1219+
WHERE
1220+
T2_0_.t1idref IN (
1221+
SELECT
1222+
T1_1_.t1id
1223+
FROM
1224+
t3 T3_0_
1225+
INNER JOIN
1226+
t2 T2_1_
1227+
ON T3_0_.t2idref=T2_1_.t2id
1228+
INNER JOIN
1229+
t1 T1_1_
1230+
ON T2_1_.t1idref=T1_1_.t1id
1231+
WHERE
1232+
T3_0_.t3idref= 1
1233+
);
1234+
t1idref t2id
1235+
200001 200011
1236+
200001 200012
1237+
200001 200013
1238+
explain SELECT SQL_NO_CACHE
1239+
T2_0_.t1idref,
1240+
T2_0_.t2id
1241+
FROM
1242+
t2 T2_0_
1243+
WHERE
1244+
T2_0_.t1idref IN (
1245+
SELECT
1246+
T1_1_.t1id
1247+
FROM
1248+
t3 T3_0_
1249+
INNER JOIN
1250+
t2 T2_1_
1251+
ON T3_0_.t2idref=T2_1_.t2id
1252+
INNER JOIN
1253+
t1 T1_1_
1254+
ON T2_1_.t1idref=T1_1_.t1id
1255+
WHERE
1256+
T3_0_.t3idref= 1
1257+
);
1258+
id select_type table type possible_keys key key_len ref rows Extra
1259+
1 PRIMARY T3_0_ ref PRIMARY,FK_T3_T2Id PRIMARY 8 const 3 Using index; Start temporary
1260+
1 PRIMARY T2_1_ eq_ref PRIMARY,FK_T2_T1Id PRIMARY 8 test.T3_0_.t2idref 1
1261+
1 PRIMARY T1_1_ eq_ref PRIMARY PRIMARY 8 test.T2_1_.t1idref 1 Using index
1262+
1 PRIMARY T2_0_ ref FK_T2_T1Id FK_T2_T1Id 8 test.T2_1_.t1idref 1 Using index; End temporary
1263+
drop table t3,t2,t1;
1264+
set optimizer_search_depth=@tmp7474;
11811265
# This must be the last in the file:
11821266
set optimizer_switch=@subselect_sj2_tmp;

mysql-test/r/subselect_sj2_jcl6.result

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,90 @@ id nombre
11931193
2 row 2
11941194
3 row 3
11951195
DROP TABLE t1, t2;
1196+
#
1197+
# MDEV-7474: Semi-Join's DuplicateWeedout strategy skipped for some values of optimizer_search_depth
1198+
#
1199+
CREATE TABLE t1 (
1200+
t1id BIGINT(20) NOT NULL,
1201+
code VARCHAR(20),
1202+
PRIMARY KEY (t1id)
1203+
) COLLATE='utf8mb4_bin' ENGINE=InnoDB;
1204+
CREATE TABLE t2 (
1205+
t2id BIGINT(20) NOT NULL,
1206+
t1idref BIGINT(20) NOT NULL,
1207+
code VARCHAR(20),
1208+
PRIMARY KEY (t2id),
1209+
INDEX FK_T2_T1Id (t1idref),
1210+
CONSTRAINT FK_T2_T1Id FOREIGN KEY (t1idref) REFERENCES t1 (t1id)
1211+
) COLLATE='utf8mb4_bin' ENGINE=InnoDB;
1212+
CREATE TABLE t3 (
1213+
t3idref BIGINT(20) NOT NULL,
1214+
t2idref BIGINT(20) NOT NULL,
1215+
sequencenumber INT(10) NOT NULL,
1216+
PRIMARY KEY (t3idref, t2idref),
1217+
INDEX FK_T3_T2Id (t2idref),
1218+
CONSTRAINT FK_T3_T2Id FOREIGN KEY (t2idref) REFERENCES t2 (t2id)
1219+
) COLLATE='utf8mb4_bin' ENGINE=InnoDB;
1220+
INSERT INTO t1 (t1id) VALUES (100001),(100017),(100018),(100026),(100027),(100028),(100029),(100030),
1221+
(100031),(100032),(100033),(100034),(100035),(100036),(100037),(100038),(100040),(100041),(100042),
1222+
(100043),(100044),(100045),(100046),(100047);
1223+
INSERT IGNORE INTO t2 (t2id, t1idref) SELECT t1id, t1id FROM t1;
1224+
INSERT IGNORE INTO t1 VALUES (200001, 'a');
1225+
INSERT IGNORE INTO t2 (t2id, t1idref) VALUES (200011, 200001),(200012, 200001),(200013, 200001);
1226+
INSERT IGNORE INTO t3 VALUES (1, 200011, 1), (1, 200012, 2), (1, 200013, 3);
1227+
set @tmp7474= @@optimizer_search_depth;
1228+
SET SESSION optimizer_search_depth = 1;
1229+
SELECT SQL_NO_CACHE
1230+
T2_0_.t1idref,
1231+
T2_0_.t2id
1232+
FROM
1233+
t2 T2_0_
1234+
WHERE
1235+
T2_0_.t1idref IN (
1236+
SELECT
1237+
T1_1_.t1id
1238+
FROM
1239+
t3 T3_0_
1240+
INNER JOIN
1241+
t2 T2_1_
1242+
ON T3_0_.t2idref=T2_1_.t2id
1243+
INNER JOIN
1244+
t1 T1_1_
1245+
ON T2_1_.t1idref=T1_1_.t1id
1246+
WHERE
1247+
T3_0_.t3idref= 1
1248+
);
1249+
t1idref t2id
1250+
200001 200011
1251+
200001 200012
1252+
200001 200013
1253+
explain SELECT SQL_NO_CACHE
1254+
T2_0_.t1idref,
1255+
T2_0_.t2id
1256+
FROM
1257+
t2 T2_0_
1258+
WHERE
1259+
T2_0_.t1idref IN (
1260+
SELECT
1261+
T1_1_.t1id
1262+
FROM
1263+
t3 T3_0_
1264+
INNER JOIN
1265+
t2 T2_1_
1266+
ON T3_0_.t2idref=T2_1_.t2id
1267+
INNER JOIN
1268+
t1 T1_1_
1269+
ON T2_1_.t1idref=T1_1_.t1id
1270+
WHERE
1271+
T3_0_.t3idref= 1
1272+
);
1273+
id select_type table type possible_keys key key_len ref rows Extra
1274+
1 PRIMARY T3_0_ ref PRIMARY,FK_T3_T2Id PRIMARY 8 const 3 Using index; Start temporary
1275+
1 PRIMARY T2_1_ eq_ref PRIMARY,FK_T2_T1Id PRIMARY 8 test.T3_0_.t2idref 1 Using join buffer (flat, BKA join); Key-ordered scan
1276+
1 PRIMARY T1_1_ eq_ref PRIMARY PRIMARY 8 test.T2_1_.t1idref 1 Using index
1277+
1 PRIMARY T2_0_ ref FK_T2_T1Id FK_T2_T1Id 8 test.T2_1_.t1idref 1 Using index; End temporary
1278+
drop table t3,t2,t1;
1279+
set optimizer_search_depth=@tmp7474;
11961280
# This must be the last in the file:
11971281
set optimizer_switch=@subselect_sj2_tmp;
11981282
#

mysql-test/r/subselect_sj2_mat.result

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,90 @@ id nombre
11801180
2 row 2
11811181
3 row 3
11821182
DROP TABLE t1, t2;
1183+
#
1184+
# MDEV-7474: Semi-Join's DuplicateWeedout strategy skipped for some values of optimizer_search_depth
1185+
#
1186+
CREATE TABLE t1 (
1187+
t1id BIGINT(20) NOT NULL,
1188+
code VARCHAR(20),
1189+
PRIMARY KEY (t1id)
1190+
) COLLATE='utf8mb4_bin' ENGINE=InnoDB;
1191+
CREATE TABLE t2 (
1192+
t2id BIGINT(20) NOT NULL,
1193+
t1idref BIGINT(20) NOT NULL,
1194+
code VARCHAR(20),
1195+
PRIMARY KEY (t2id),
1196+
INDEX FK_T2_T1Id (t1idref),
1197+
CONSTRAINT FK_T2_T1Id FOREIGN KEY (t1idref) REFERENCES t1 (t1id)
1198+
) COLLATE='utf8mb4_bin' ENGINE=InnoDB;
1199+
CREATE TABLE t3 (
1200+
t3idref BIGINT(20) NOT NULL,
1201+
t2idref BIGINT(20) NOT NULL,
1202+
sequencenumber INT(10) NOT NULL,
1203+
PRIMARY KEY (t3idref, t2idref),
1204+
INDEX FK_T3_T2Id (t2idref),
1205+
CONSTRAINT FK_T3_T2Id FOREIGN KEY (t2idref) REFERENCES t2 (t2id)
1206+
) COLLATE='utf8mb4_bin' ENGINE=InnoDB;
1207+
INSERT INTO t1 (t1id) VALUES (100001),(100017),(100018),(100026),(100027),(100028),(100029),(100030),
1208+
(100031),(100032),(100033),(100034),(100035),(100036),(100037),(100038),(100040),(100041),(100042),
1209+
(100043),(100044),(100045),(100046),(100047);
1210+
INSERT IGNORE INTO t2 (t2id, t1idref) SELECT t1id, t1id FROM t1;
1211+
INSERT IGNORE INTO t1 VALUES (200001, 'a');
1212+
INSERT IGNORE INTO t2 (t2id, t1idref) VALUES (200011, 200001),(200012, 200001),(200013, 200001);
1213+
INSERT IGNORE INTO t3 VALUES (1, 200011, 1), (1, 200012, 2), (1, 200013, 3);
1214+
set @tmp7474= @@optimizer_search_depth;
1215+
SET SESSION optimizer_search_depth = 1;
1216+
SELECT SQL_NO_CACHE
1217+
T2_0_.t1idref,
1218+
T2_0_.t2id
1219+
FROM
1220+
t2 T2_0_
1221+
WHERE
1222+
T2_0_.t1idref IN (
1223+
SELECT
1224+
T1_1_.t1id
1225+
FROM
1226+
t3 T3_0_
1227+
INNER JOIN
1228+
t2 T2_1_
1229+
ON T3_0_.t2idref=T2_1_.t2id
1230+
INNER JOIN
1231+
t1 T1_1_
1232+
ON T2_1_.t1idref=T1_1_.t1id
1233+
WHERE
1234+
T3_0_.t3idref= 1
1235+
);
1236+
t1idref t2id
1237+
200001 200011
1238+
200001 200012
1239+
200001 200013
1240+
explain SELECT SQL_NO_CACHE
1241+
T2_0_.t1idref,
1242+
T2_0_.t2id
1243+
FROM
1244+
t2 T2_0_
1245+
WHERE
1246+
T2_0_.t1idref IN (
1247+
SELECT
1248+
T1_1_.t1id
1249+
FROM
1250+
t3 T3_0_
1251+
INNER JOIN
1252+
t2 T2_1_
1253+
ON T3_0_.t2idref=T2_1_.t2id
1254+
INNER JOIN
1255+
t1 T1_1_
1256+
ON T2_1_.t1idref=T1_1_.t1id
1257+
WHERE
1258+
T3_0_.t3idref= 1
1259+
);
1260+
id select_type table type possible_keys key key_len ref rows Extra
1261+
1 PRIMARY T3_0_ ref PRIMARY,FK_T3_T2Id PRIMARY 8 const 3 Using index; Start temporary
1262+
1 PRIMARY T2_1_ eq_ref PRIMARY,FK_T2_T1Id PRIMARY 8 test.T3_0_.t2idref 1
1263+
1 PRIMARY T1_1_ eq_ref PRIMARY PRIMARY 8 test.T2_1_.t1idref 1 Using index
1264+
1 PRIMARY T2_0_ ref FK_T2_T1Id FK_T2_T1Id 8 test.T2_1_.t1idref 1 Using index; End temporary
1265+
drop table t3,t2,t1;
1266+
set optimizer_search_depth=@tmp7474;
11831267
# This must be the last in the file:
11841268
set optimizer_switch=@subselect_sj2_tmp;
11851269
set optimizer_switch=default;

mysql-test/t/subselect_sj2.test

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,5 +1320,76 @@ SELECT * FROM t1 WHERE id in (select distinct id_agente from t2);
13201320

13211321
DROP TABLE t1, t2;
13221322

1323+
--echo #
1324+
--echo # MDEV-7474: Semi-Join's DuplicateWeedout strategy skipped for some values of optimizer_search_depth
1325+
--echo #
1326+
1327+
CREATE TABLE t1 (
1328+
t1id BIGINT(20) NOT NULL,
1329+
code VARCHAR(20),
1330+
PRIMARY KEY (t1id)
1331+
) COLLATE='utf8mb4_bin' ENGINE=InnoDB;
1332+
1333+
CREATE TABLE t2 (
1334+
t2id BIGINT(20) NOT NULL,
1335+
t1idref BIGINT(20) NOT NULL,
1336+
code VARCHAR(20),
1337+
PRIMARY KEY (t2id),
1338+
INDEX FK_T2_T1Id (t1idref),
1339+
CONSTRAINT FK_T2_T1Id FOREIGN KEY (t1idref) REFERENCES t1 (t1id)
1340+
) COLLATE='utf8mb4_bin' ENGINE=InnoDB;
1341+
1342+
CREATE TABLE t3 (
1343+
t3idref BIGINT(20) NOT NULL,
1344+
t2idref BIGINT(20) NOT NULL,
1345+
sequencenumber INT(10) NOT NULL,
1346+
PRIMARY KEY (t3idref, t2idref),
1347+
INDEX FK_T3_T2Id (t2idref),
1348+
CONSTRAINT FK_T3_T2Id FOREIGN KEY (t2idref) REFERENCES t2 (t2id)
1349+
) COLLATE='utf8mb4_bin' ENGINE=InnoDB;
1350+
1351+
# Load up dummy data (needed to reproduce issue)
1352+
INSERT INTO t1 (t1id) VALUES (100001),(100017),(100018),(100026),(100027),(100028),(100029),(100030),
1353+
(100031),(100032),(100033),(100034),(100035),(100036),(100037),(100038),(100040),(100041),(100042),
1354+
(100043),(100044),(100045),(100046),(100047);
1355+
1356+
INSERT IGNORE INTO t2 (t2id, t1idref) SELECT t1id, t1id FROM t1;
1357+
1358+
# Now the test Data
1359+
INSERT IGNORE INTO t1 VALUES (200001, 'a');
1360+
INSERT IGNORE INTO t2 (t2id, t1idref) VALUES (200011, 200001),(200012, 200001),(200013, 200001);
1361+
INSERT IGNORE INTO t3 VALUES (1, 200011, 1), (1, 200012, 2), (1, 200013, 3);
1362+
1363+
set @tmp7474= @@optimizer_search_depth;
1364+
SET SESSION optimizer_search_depth = 1;
1365+
1366+
let $query=
1367+
SELECT SQL_NO_CACHE
1368+
T2_0_.t1idref,
1369+
T2_0_.t2id
1370+
FROM
1371+
t2 T2_0_
1372+
WHERE
1373+
T2_0_.t1idref IN (
1374+
SELECT
1375+
T1_1_.t1id
1376+
FROM
1377+
t3 T3_0_
1378+
INNER JOIN
1379+
t2 T2_1_
1380+
ON T3_0_.t2idref=T2_1_.t2id
1381+
INNER JOIN
1382+
t1 T1_1_
1383+
ON T2_1_.t1idref=T1_1_.t1id
1384+
WHERE
1385+
T3_0_.t3idref= 1
1386+
);
1387+
1388+
eval $query;
1389+
eval explain $query;
1390+
1391+
drop table t3,t2,t1;
1392+
set optimizer_search_depth=@tmp7474;
1393+
13231394
--echo # This must be the last in the file:
13241395
set optimizer_switch=@subselect_sj2_tmp;

0 commit comments

Comments
 (0)