You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

826 lines
29 KiB

  1. drop table if exists t0, t1, t2, t10, t11, t12;
  2. create table t0 (a int);
  3. insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
  4. create table t1(a int, b int);
  5. insert into t1 values (0,0),(1,1),(2,2);
  6. create table t2 as select * from t1;
  7. create table t11(a int, b int);
  8. create table t10 (pk int, a int, primary key(pk));
  9. insert into t10 select a,a from t0;
  10. create table t12 like t10;
  11. insert into t12 select * from t10;
  12. Flattened because of dependency, t10=func(t1)
  13. explain select * from t1 where a in (select pk from t10);
  14. id select_type table type possible_keys key key_len ref rows Extra
  15. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3
  16. 1 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 Using index
  17. select * from t1 where a in (select pk from t10);
  18. a b
  19. 0 0
  20. 1 1
  21. 2 2
  22. A confluent case of dependency
  23. explain select * from t1 where a in (select a from t10 where pk=12);
  24. id select_type table type possible_keys key key_len ref rows Extra
  25. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  26. select * from t1 where a in (select a from t10 where pk=12);
  27. a b
  28. explain select * from t1 where a in (select a from t10 where pk=9);
  29. id select_type table type possible_keys key key_len ref rows Extra
  30. 1 PRIMARY t10 const PRIMARY PRIMARY 4 const 1
  31. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
  32. select * from t1 where a in (select a from t10 where pk=9);
  33. a b
  34. An empty table inside
  35. explain select * from t1 where a in (select a from t11);
  36. id select_type table type possible_keys key key_len ref rows Extra
  37. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  38. select * from t1 where a in (select a from t11);
  39. a b
  40. explain select * from t1 where a in (select pk from t10) and b in (select pk from t10);
  41. id select_type table type possible_keys key key_len ref rows Extra
  42. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3
  43. 1 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 Using index
  44. 1 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.b 1 Using index
  45. select * from t1 where a in (select pk from t10) and b in (select pk from t10);
  46. a b
  47. 0 0
  48. 1 1
  49. 2 2
  50. flattening a nested subquery
  51. explain select * from t1 where a in (select pk from t10 where t10.a in (select pk from t12));
  52. id select_type table type possible_keys key key_len ref rows Extra
  53. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3
  54. 1 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.a 1
  55. 1 PRIMARY t12 eq_ref PRIMARY PRIMARY 4 test.t10.a 1 Using index
  56. select * from t1 where a in (select pk from t10 where t10.a in (select pk from t12));
  57. a b
  58. 0 0
  59. 1 1
  60. 2 2
  61. flattening subquery w/ several tables
  62. explain extended select * from t1 where a in (select t10.pk from t10, t12 where t12.pk=t10.a);
  63. id select_type table type possible_keys key key_len ref rows filtered Extra
  64. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00
  65. 1 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 100.00
  66. 1 PRIMARY t12 eq_ref PRIMARY PRIMARY 4 test.t10.a 1 100.00 Using index
  67. Warnings:
  68. Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t10` join `test`.`t12` join `test`.`t1` where ((`test`.`t10`.`pk` = `test`.`t1`.`a`) and (`test`.`t12`.`pk` = `test`.`t10`.`a`))
  69. subqueries within outer joins go into ON expr.
  70. explAin extended
  71. select * from t1 left join (t2 A, t2 B) on ( A.A= t1.A And B.A in (select pk from t10));
  72. id select_type tABle type possiBle_keys key key_len ref rows filtered ExtrA
  73. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00
  74. 1 PRIMARY A ALL NULL NULL NULL NULL 3 100.00 Using where
  75. 1 PRIMARY B ALL NULL NULL NULL NULL 3 100.00 Using where
  76. 2 DEPENDENT SUBQUERY t10 unique_suBquery PRIMARY PRIMARY 4 func 1 100.00 Using index
  77. Warnings:
  78. Note 1003 select `test`.`t1`.`A` AS `A`,`test`.`t1`.`B` AS `B`,`test`.`A`.`A` AS `A`,`test`.`A`.`B` AS `B`,`test`.`B`.`A` AS `A`,`test`.`B`.`B` AS `B` from `test`.`t1` left join (`test`.`t2` `A` join `test`.`t2` `B`) on(((`test`.`A`.`A` = `test`.`t1`.`A`) And <in_optimizer>(`test`.`B`.`A`,<exists>(<primAry_index_lookup>(<cAche>(`test`.`B`.`A`) in t10 on PRIMARY))))) where 1
  79. t2 should be wrapped into OJ-nest, so we have "t1 LJ (t2 J t10)"
  80. explAin extended
  81. select * from t1 left join t2 on (t2.A= t1.A And t2.A in (select pk from t10));
  82. id select_type tABle type possiBle_keys key key_len ref rows filtered ExtrA
  83. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00
  84. 1 PRIMARY t2 ALL NULL NULL NULL NULL 3 100.00 Using where
  85. 2 DEPENDENT SUBQUERY t10 unique_suBquery PRIMARY PRIMARY 4 func 1 100.00 Using index
  86. Warnings:
  87. Note 1003 select `test`.`t1`.`A` AS `A`,`test`.`t1`.`B` AS `B`,`test`.`t2`.`A` AS `A`,`test`.`t2`.`B` AS `B` from `test`.`t1` left join `test`.`t2` on(((`test`.`t2`.`A` = `test`.`t1`.`A`) And <in_optimizer>(`test`.`t2`.`A`,<exists>(<primAry_index_lookup>(<cAche>(`test`.`t2`.`A`) in t10 on PRIMARY))))) where 1
  88. we shouldn't flatten if we're going to get a join of > MAX_TABLES.
  89. explain select * from
  90. t1 s00, t1 s01, t1 s02, t1 s03, t1 s04,t1 s05,t1 s06,t1 s07,t1 s08,t1 s09,
  91. t1 s10, t1 s11, t1 s12, t1 s13, t1 s14,t1 s15,t1 s16,t1 s17,t1 s18,t1 s19,
  92. t1 s20, t1 s21, t1 s22, t1 s23, t1 s24,t1 s25,t1 s26,t1 s27,t1 s28,t1 s29,
  93. t1 s30, t1 s31, t1 s32, t1 s33, t1 s34,t1 s35,t1 s36,t1 s37,t1 s38,t1 s39,
  94. t1 s40, t1 s41, t1 s42, t1 s43, t1 s44,t1 s45,t1 s46,t1 s47,t1 s48,t1 s49
  95. where
  96. s00.a in (
  97. select m00.a from
  98. t1 m00, t1 m01, t1 m02, t1 m03, t1 m04,t1 m05,t1 m06,t1 m07,t1 m08,t1 m09,
  99. t1 m10, t1 m11, t1 m12, t1 m13, t1 m14,t1 m15,t1 m16,t1 m17,t1 m18,t1 m19
  100. );
  101. id select_type table type possible_keys key key_len ref rows Extra
  102. 1 PRIMARY s00 ALL NULL NULL NULL NULL 3 Using where
  103. 1 PRIMARY s01 ALL NULL NULL NULL NULL 3 Using join buffer
  104. 1 PRIMARY s02 ALL NULL NULL NULL NULL 3 Using join buffer
  105. 1 PRIMARY s03 ALL NULL NULL NULL NULL 3 Using join buffer
  106. 1 PRIMARY s04 ALL NULL NULL NULL NULL 3 Using join buffer
  107. 1 PRIMARY s05 ALL NULL NULL NULL NULL 3 Using join buffer
  108. 1 PRIMARY s06 ALL NULL NULL NULL NULL 3 Using join buffer
  109. 1 PRIMARY s07 ALL NULL NULL NULL NULL 3 Using join buffer
  110. 1 PRIMARY s08 ALL NULL NULL NULL NULL 3 Using join buffer
  111. 1 PRIMARY s09 ALL NULL NULL NULL NULL 3 Using join buffer
  112. 1 PRIMARY s10 ALL NULL NULL NULL NULL 3 Using join buffer
  113. 1 PRIMARY s11 ALL NULL NULL NULL NULL 3 Using join buffer
  114. 1 PRIMARY s12 ALL NULL NULL NULL NULL 3 Using join buffer
  115. 1 PRIMARY s13 ALL NULL NULL NULL NULL 3 Using join buffer
  116. 1 PRIMARY s14 ALL NULL NULL NULL NULL 3 Using join buffer
  117. 1 PRIMARY s15 ALL NULL NULL NULL NULL 3 Using join buffer
  118. 1 PRIMARY s16 ALL NULL NULL NULL NULL 3 Using join buffer
  119. 1 PRIMARY s17 ALL NULL NULL NULL NULL 3 Using join buffer
  120. 1 PRIMARY s18 ALL NULL NULL NULL NULL 3 Using join buffer
  121. 1 PRIMARY s19 ALL NULL NULL NULL NULL 3 Using join buffer
  122. 1 PRIMARY s20 ALL NULL NULL NULL NULL 3 Using join buffer
  123. 1 PRIMARY s21 ALL NULL NULL NULL NULL 3 Using join buffer
  124. 1 PRIMARY s22 ALL NULL NULL NULL NULL 3 Using join buffer
  125. 1 PRIMARY s23 ALL NULL NULL NULL NULL 3 Using join buffer
  126. 1 PRIMARY s24 ALL NULL NULL NULL NULL 3 Using join buffer
  127. 1 PRIMARY s25 ALL NULL NULL NULL NULL 3 Using join buffer
  128. 1 PRIMARY s26 ALL NULL NULL NULL NULL 3 Using join buffer
  129. 1 PRIMARY s27 ALL NULL NULL NULL NULL 3 Using join buffer
  130. 1 PRIMARY s28 ALL NULL NULL NULL NULL 3 Using join buffer
  131. 1 PRIMARY s29 ALL NULL NULL NULL NULL 3 Using join buffer
  132. 1 PRIMARY s30 ALL NULL NULL NULL NULL 3 Using join buffer
  133. 1 PRIMARY s31 ALL NULL NULL NULL NULL 3 Using join buffer
  134. 1 PRIMARY s32 ALL NULL NULL NULL NULL 3 Using join buffer
  135. 1 PRIMARY s33 ALL NULL NULL NULL NULL 3 Using join buffer
  136. 1 PRIMARY s34 ALL NULL NULL NULL NULL 3 Using join buffer
  137. 1 PRIMARY s35 ALL NULL NULL NULL NULL 3 Using join buffer
  138. 1 PRIMARY s36 ALL NULL NULL NULL NULL 3 Using join buffer
  139. 1 PRIMARY s37 ALL NULL NULL NULL NULL 3 Using join buffer
  140. 1 PRIMARY s38 ALL NULL NULL NULL NULL 3 Using join buffer
  141. 1 PRIMARY s39 ALL NULL NULL NULL NULL 3 Using join buffer
  142. 1 PRIMARY s40 ALL NULL NULL NULL NULL 3 Using join buffer
  143. 1 PRIMARY s41 ALL NULL NULL NULL NULL 3 Using join buffer
  144. 1 PRIMARY s42 ALL NULL NULL NULL NULL 3 Using join buffer
  145. 1 PRIMARY s43 ALL NULL NULL NULL NULL 3 Using join buffer
  146. 1 PRIMARY s44 ALL NULL NULL NULL NULL 3 Using join buffer
  147. 1 PRIMARY s45 ALL NULL NULL NULL NULL 3 Using join buffer
  148. 1 PRIMARY s46 ALL NULL NULL NULL NULL 3 Using join buffer
  149. 1 PRIMARY s47 ALL NULL NULL NULL NULL 3 Using join buffer
  150. 1 PRIMARY s48 ALL NULL NULL NULL NULL 3 Using join buffer
  151. 1 PRIMARY s49 ALL NULL NULL NULL NULL 3 Using join buffer
  152. 2 DEPENDENT SUBQUERY m00 ALL NULL NULL NULL NULL 3 Using where
  153. 2 DEPENDENT SUBQUERY m01 ALL NULL NULL NULL NULL 3 Using join buffer
  154. 2 DEPENDENT SUBQUERY m02 ALL NULL NULL NULL NULL 3 Using join buffer
  155. 2 DEPENDENT SUBQUERY m03 ALL NULL NULL NULL NULL 3 Using join buffer
  156. 2 DEPENDENT SUBQUERY m04 ALL NULL NULL NULL NULL 3 Using join buffer
  157. 2 DEPENDENT SUBQUERY m05 ALL NULL NULL NULL NULL 3 Using join buffer
  158. 2 DEPENDENT SUBQUERY m06 ALL NULL NULL NULL NULL 3 Using join buffer
  159. 2 DEPENDENT SUBQUERY m07 ALL NULL NULL NULL NULL 3 Using join buffer
  160. 2 DEPENDENT SUBQUERY m08 ALL NULL NULL NULL NULL 3 Using join buffer
  161. 2 DEPENDENT SUBQUERY m09 ALL NULL NULL NULL NULL 3 Using join buffer
  162. 2 DEPENDENT SUBQUERY m10 ALL NULL NULL NULL NULL 3 Using join buffer
  163. 2 DEPENDENT SUBQUERY m11 ALL NULL NULL NULL NULL 3 Using join buffer
  164. 2 DEPENDENT SUBQUERY m12 ALL NULL NULL NULL NULL 3 Using join buffer
  165. 2 DEPENDENT SUBQUERY m13 ALL NULL NULL NULL NULL 3 Using join buffer
  166. 2 DEPENDENT SUBQUERY m14 ALL NULL NULL NULL NULL 3 Using join buffer
  167. 2 DEPENDENT SUBQUERY m15 ALL NULL NULL NULL NULL 3 Using join buffer
  168. 2 DEPENDENT SUBQUERY m16 ALL NULL NULL NULL NULL 3 Using join buffer
  169. 2 DEPENDENT SUBQUERY m17 ALL NULL NULL NULL NULL 3 Using join buffer
  170. 2 DEPENDENT SUBQUERY m18 ALL NULL NULL NULL NULL 3 Using join buffer
  171. 2 DEPENDENT SUBQUERY m19 ALL NULL NULL NULL NULL 3 Using where; Using join buffer
  172. select * from
  173. t1 left join t2 on (t2.a= t1.a and t2.a in (select pk from t10))
  174. where t1.a < 5;
  175. a b a b
  176. 0 0 0 0
  177. 1 1 1 1
  178. 2 2 2 2
  179. prepare s1 from
  180. ' select * from
  181. t1 left join t2 on (t2.a= t1.a and t2.a in (select pk from t10))
  182. where t1.a < 5';
  183. execute s1;
  184. a b a b
  185. 0 0 0 0
  186. 1 1 1 1
  187. 2 2 2 2
  188. execute s1;
  189. a b a b
  190. 0 0 0 0
  191. 1 1 1 1
  192. 2 2 2 2
  193. insert into t1 select (A.a + 10 * B.a),1 from t0 A, t0 B;
  194. explain extended select * from t1 where a in (select pk from t10 where pk<3);
  195. id select_type table type possible_keys key key_len ref rows filtered Extra
  196. 1 PRIMARY t10 range PRIMARY PRIMARY 4 NULL 4 100.00 Using where; Using index
  197. 1 PRIMARY t1 ALL NULL NULL NULL NULL 103 100.00 Using where; Using join buffer
  198. Warnings:
  199. Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t10` join `test`.`t1` where ((`test`.`t1`.`a` = `test`.`t10`.`pk`) and (`test`.`t10`.`pk` < 3))
  200. BUG#37120 optimizer_switch allowable values not according to specification
  201. select @@optimizer_switch;
  202. @@optimizer_switch
  203. index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_condition_pushdown=on,firstmatch=on,loosescan=on,materialization=on,semijoin=on
  204. set optimizer_switch='default,materialization=off';
  205. select @@optimizer_switch;
  206. @@optimizer_switch
  207. index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_condition_pushdown=on,firstmatch=on,loosescan=on,materialization=off,semijoin=on
  208. set optimizer_switch='default,semijoin=off';
  209. select @@optimizer_switch;
  210. @@optimizer_switch
  211. index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_condition_pushdown=on,firstmatch=on,loosescan=on,materialization=on,semijoin=off
  212. set optimizer_switch='default,loosescan=off';
  213. select @@optimizer_switch;
  214. @@optimizer_switch
  215. index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_condition_pushdown=on,firstmatch=on,loosescan=off,materialization=on,semijoin=on
  216. set optimizer_switch='default,semijoin=off,materialization=off';
  217. select @@optimizer_switch;
  218. @@optimizer_switch
  219. index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_condition_pushdown=on,firstmatch=on,loosescan=on,materialization=off,semijoin=off
  220. set optimizer_switch='default,materialization=off,semijoin=off';
  221. select @@optimizer_switch;
  222. @@optimizer_switch
  223. index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_condition_pushdown=on,firstmatch=on,loosescan=on,materialization=off,semijoin=off
  224. set optimizer_switch='default,semijoin=off,materialization=off,loosescan=off';
  225. select @@optimizer_switch;
  226. @@optimizer_switch
  227. index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_condition_pushdown=on,firstmatch=on,loosescan=off,materialization=off,semijoin=off
  228. set optimizer_switch='default,semijoin=off,loosescan=off';
  229. select @@optimizer_switch;
  230. @@optimizer_switch
  231. index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_condition_pushdown=on,firstmatch=on,loosescan=off,materialization=on,semijoin=off
  232. set optimizer_switch='default,materialization=off,loosescan=off';
  233. select @@optimizer_switch;
  234. @@optimizer_switch
  235. index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_condition_pushdown=on,firstmatch=on,loosescan=off,materialization=off,semijoin=on
  236. set optimizer_switch=default;
  237. drop table t0, t1, t2;
  238. drop table t10, t11, t12;
  239. Bug#37899: Wrongly checked optimization prerequisite caused failed
  240. assertion.
  241. CREATE TABLE t1 (
  242. `pk` int(11),
  243. `varchar_nokey` varchar(5)
  244. );
  245. INSERT INTO t1 VALUES
  246. (1,'qk'),(2,'j'),(3,'aew');
  247. SELECT *
  248. FROM t1
  249. WHERE varchar_nokey IN (
  250. SELECT
  251. varchar_nokey
  252. FROM
  253. t1
  254. ) XOR pk = 30;
  255. pk varchar_nokey
  256. 1 qk
  257. 2 j
  258. 3 aew
  259. drop table t1;
  260. #
  261. # BUG#41842: Semi-join materialization strategy crashes when the upper query has HAVING
  262. #
  263. CREATE TABLE t1 (
  264. pk int(11) NOT NULL AUTO_INCREMENT,
  265. int_nokey int(11) NOT NULL,
  266. time_key time NOT NULL,
  267. datetime_key datetime NOT NULL,
  268. datetime_nokey datetime NOT NULL,
  269. varchar_key varchar(1) NOT NULL,
  270. varchar_nokey varchar(1) NOT NULL,
  271. PRIMARY KEY (pk),
  272. KEY time_key (time_key),
  273. KEY datetime_key (datetime_key),
  274. KEY varchar_key (varchar_key)
  275. );
  276. INSERT INTO t1 VALUES
  277. (1,0, '00:16:10','2008-09-03 14:25:40','2008-09-03 14:25:40','h','h'),
  278. (2,7, '00:00:00','2001-01-13 00:00:00','2001-01-13 00:00:00','',''),
  279. (3,0, '00:00:00','0000-00-00 00:00:00','0000-00-00 00:00:00','x','x'),
  280. (4,2, '16:29:24','2000-10-16 01:39:08','2000-10-16 01:39:08','w','w'),
  281. (5,1, '09:23:32','0000-00-00 00:00:00','0000-00-00 00:00:00','p','p'),
  282. (6,3, '00:00:00','2007-12-02 00:00:00','2007-12-02 00:00:00','o','o'),
  283. (7,3, '00:00:00','2008-09-11 00:00:00','2008-09-11 00:00:00','',''),
  284. (8,0, '13:59:04','0000-00-00 00:00:00','0000-00-00 00:00:00','s','s'),
  285. (9,7, '09:01:06','0000-00-00 00:00:00','0000-00-00 00:00:00','d','d'),
  286. (10,5,'00:00:00','0000-00-00 00:00:00','0000-00-00 00:00:00','n','n'),
  287. (11,0,'21:06:46','0000-00-00 00:00:00','0000-00-00 00:00:00','o','o'),
  288. (12,2,'00:00:00','0000-00-00 00:00:00','0000-00-00 00:00:00','',''),
  289. (13,6,'14:45:34','2003-07-28 02:34:08','2003-07-28 02:34:08','w','w'),
  290. (14,1,'15:04:12','0000-00-00 00:00:00','0000-00-00 00:00:00','o','o'),
  291. (15,0,'00:00:00','0000-00-00 00:00:00','0000-00-00 00:00:00','x','x'),
  292. (16,0,'15:55:23','2004-03-17 00:32:27','2004-03-17 00:32:27','p','p'),
  293. (17,1,'16:30:00','2004-12-27 19:20:00','2004-12-27 19:20:00','d','d'),
  294. (18,0,'00:00:00','0000-00-00 00:00:00','0000-00-00 00:00:00','h','h'),
  295. (19,0,'14:13:26','2008-11-09 05:53:48','2008-11-09 05:53:48','o','o'),
  296. (20,0,'00:00:00','2009-10-11 06:58:04','2009-10-11 06:58:04','k','k');
  297. CREATE TABLE t2 (
  298. pk int(11) NOT NULL AUTO_INCREMENT,
  299. int_nokey int(11) NOT NULL,
  300. time_key time NOT NULL,
  301. datetime_key datetime NOT NULL,
  302. datetime_nokey datetime NOT NULL,
  303. varchar_key varchar(1) NOT NULL,
  304. varchar_nokey varchar(1) NOT NULL,
  305. PRIMARY KEY (pk),
  306. KEY time_key (time_key),
  307. KEY datetime_key (datetime_key),
  308. KEY varchar_key (varchar_key)
  309. );
  310. INSERT INTO t2 VALUES
  311. (10,0,'19:39:13','0000-00-00 00:00:00','0000-00-00 00:00:00','g','g'),
  312. (11,8,'03:43:53','0000-00-00 00:00:00','0000-00-00 00:00:00','b','b');
  313. SELECT OUTR.datetime_nokey AS X FROM t1 AS OUTR
  314. WHERE
  315. OUTR.varchar_nokey IN (SELECT
  316. INNR . varchar_nokey AS Y
  317. FROM t2 AS INNR
  318. WHERE
  319. INNR . datetime_key >= INNR . time_key OR
  320. INNR . pk = INNR . int_nokey
  321. )
  322. AND OUTR . varchar_nokey <= 'w'
  323. HAVING X > '2012-12-12';
  324. X
  325. drop table t1, t2;
  326. #
  327. # Bug#45191: Incorrectly initialized semi-join led to a wrong result.
  328. #
  329. CREATE TABLE STAFF (EMPNUM CHAR(3) NOT NULL,
  330. EMPNAME CHAR(20), GRADE DECIMAL(4), CITY CHAR(15));
  331. CREATE TABLE PROJ (PNUM CHAR(3) NOT NULL,
  332. PNAME CHAR(20), PTYPE CHAR(6),
  333. BUDGET DECIMAL(9),
  334. CITY CHAR(15));
  335. CREATE TABLE WORKS (EMPNUM CHAR(3) NOT NULL,
  336. PNUM CHAR(3) NOT NULL, HOURS DECIMAL(5));
  337. INSERT INTO STAFF VALUES ('E1','Alice',12,'Deale');
  338. INSERT INTO STAFF VALUES ('E2','Betty',10,'Vienna');
  339. INSERT INTO STAFF VALUES ('E3','Carmen',13,'Vienna');
  340. INSERT INTO STAFF VALUES ('E4','Don',12,'Deale');
  341. INSERT INTO STAFF VALUES ('E5','Ed',13,'Akron');
  342. INSERT INTO PROJ VALUES ('P1','MXSS','Design',10000,'Deale');
  343. INSERT INTO PROJ VALUES ('P2','CALM','Code',30000,'Vienna');
  344. INSERT INTO PROJ VALUES ('P3','SDP','Test',30000,'Tampa');
  345. INSERT INTO PROJ VALUES ('P4','SDP','Design',20000,'Deale');
  346. INSERT INTO PROJ VALUES ('P5','IRM','Test',10000,'Vienna');
  347. INSERT INTO PROJ VALUES ('P6','PAYR','Design',50000,'Deale');
  348. INSERT INTO WORKS VALUES ('E1','P1',40);
  349. INSERT INTO WORKS VALUES ('E1','P2',20);
  350. INSERT INTO WORKS VALUES ('E1','P3',80);
  351. INSERT INTO WORKS VALUES ('E1','P4',20);
  352. INSERT INTO WORKS VALUES ('E1','P5',12);
  353. INSERT INTO WORKS VALUES ('E1','P6',12);
  354. INSERT INTO WORKS VALUES ('E2','P1',40);
  355. INSERT INTO WORKS VALUES ('E2','P2',80);
  356. INSERT INTO WORKS VALUES ('E3','P2',20);
  357. INSERT INTO WORKS VALUES ('E4','P2',20);
  358. INSERT INTO WORKS VALUES ('E4','P4',40);
  359. INSERT INTO WORKS VALUES ('E4','P5',80);
  360. set optimizer_switch='default,materialization=off';
  361. explain SELECT EMPNUM, EMPNAME
  362. FROM STAFF
  363. WHERE EMPNUM IN
  364. (SELECT EMPNUM FROM WORKS
  365. WHERE PNUM IN
  366. (SELECT PNUM FROM PROJ));
  367. id select_type table type possible_keys key key_len ref rows Extra
  368. 1 PRIMARY STAFF ALL NULL NULL NULL NULL 5
  369. 1 PRIMARY PROJ ALL NULL NULL NULL NULL 6
  370. 1 PRIMARY WORKS ALL NULL NULL NULL NULL 12 Using where; FirstMatch(STAFF)
  371. SELECT EMPNUM, EMPNAME
  372. FROM STAFF
  373. WHERE EMPNUM IN
  374. (SELECT EMPNUM FROM WORKS
  375. WHERE PNUM IN
  376. (SELECT PNUM FROM PROJ));
  377. EMPNUM EMPNAME
  378. E1 Alice
  379. E2 Betty
  380. E3 Carmen
  381. E4 Don
  382. set optimizer_switch='default';
  383. drop table STAFF,WORKS,PROJ;
  384. # End of bug#45191
  385. #
  386. # Bug#46550 Azalea returning duplicate results for some IN subqueries
  387. # w/ semijoin=on
  388. #
  389. DROP TABLE IF EXISTS t0, t1, t2;
  390. CREATE TABLE t0 (
  391. int_key int(11) DEFAULT NULL,
  392. varchar_key varchar(1) DEFAULT NULL,
  393. varchar_nokey varchar(1) DEFAULT NULL,
  394. KEY int_key (int_key),
  395. KEY varchar_key (varchar_key,int_key)
  396. );
  397. INSERT INTO t0 VALUES
  398. (1,'m','m'),
  399. (40,'h','h'),
  400. (1,'r','r'),
  401. (1,'h','h'),
  402. (9,'x','x'),
  403. (NULL,'q','q'),
  404. (NULL,'k','k'),
  405. (7,'l','l'),
  406. (182,'k','k'),
  407. (202,'a','a'),
  408. (7,'x','x'),
  409. (6,'j','j'),
  410. (119,'z','z'),
  411. (4,'d','d'),
  412. (5,'h','h'),
  413. (1,'u','u'),
  414. (3,'q','q'),
  415. (7,'a','a'),
  416. (3,'e','e'),
  417. (6,'l','l');
  418. CREATE TABLE t1 (
  419. int_key int(11) DEFAULT NULL,
  420. varchar_key varchar(1) DEFAULT NULL,
  421. varchar_nokey varchar(1) DEFAULT NULL,
  422. KEY int_key (int_key),
  423. KEY varchar_key (varchar_key,int_key)
  424. );
  425. INSERT INTO t1 VALUES (7,NULL,NULL),(4,'x','x');
  426. CREATE TABLE t2 (
  427. int_key int(11) DEFAULT NULL,
  428. varchar_key varchar(1) DEFAULT NULL,
  429. varchar_nokey varchar(1) DEFAULT NULL,
  430. KEY int_key (int_key),
  431. KEY varchar_key (varchar_key,int_key)
  432. );
  433. INSERT INTO t2 VALUES (123,NULL,NULL);
  434. SELECT int_key
  435. FROM t0
  436. WHERE varchar_nokey IN (
  437. SELECT t1 .varchar_key from t1
  438. );
  439. int_key
  440. 9
  441. 7
  442. SELECT t0.int_key
  443. FROM t0
  444. WHERE t0.varchar_nokey IN (
  445. SELECT t1_1 .varchar_key
  446. FROM t1 AS t1_1 JOIN t1 AS t1_2 ON t1_1 .int_key
  447. );
  448. int_key
  449. 9
  450. 7
  451. SELECT t0.int_key
  452. FROM t0, t2
  453. WHERE t0.varchar_nokey IN (
  454. SELECT t1_1 .varchar_key
  455. FROM t1 AS t1_1 JOIN t1 AS t1_2 ON t1_1 .int_key
  456. );
  457. int_key
  458. 9
  459. 7
  460. DROP TABLE t0, t1, t2;
  461. # End of bug#46550
  462. #
  463. # Bug #46744 Crash in optimize_semijoin_nests on empty view
  464. # with limit and procedure.
  465. #
  466. DROP TABLE IF EXISTS t1, t2;
  467. DROP VIEW IF EXISTS v1;
  468. DROP PROCEDURE IF EXISTS p1;
  469. CREATE TABLE t1 ( f1 int );
  470. CREATE TABLE t2 ( f1 int );
  471. insert into t2 values (5), (7);
  472. CREATE OR REPLACE VIEW v1 AS SELECT * FROM t1 LIMIT 2;
  473. create procedure p1()
  474. select COUNT(*)
  475. FROM v1 WHERE f1 IN
  476. (SELECT f1 FROM t2 WHERE f1 = ANY (SELECT f1 FROM v1));
  477. SET SESSION optimizer_switch = 'semijoin=on';
  478. CALL p1();
  479. COUNT(*)
  480. 0
  481. SET SESSION optimizer_switch = 'semijoin=off';
  482. CALL p1();
  483. COUNT(*)
  484. 0
  485. drop table t1, t2;
  486. drop view v1;
  487. drop procedure p1;
  488. set SESSION optimizer_switch='default';
  489. # End of bug#46744
  490. Bug#46797 "Crash in fix_semijoin_strategies_for_picked_join_order
  491. with semijoin=on"
  492. CREATE TABLE t1 (
  493. varchar_key varchar(1) DEFAULT NULL,
  494. KEY varchar_key (varchar_key)
  495. );
  496. CREATE TABLE t2 (
  497. varchar_key varchar(1) DEFAULT NULL,
  498. KEY varchar_key (varchar_key)
  499. );
  500. INSERT INTO t2 VALUES
  501. (NULL),(NULL),(NULL),(NULL),('a'),('a'),('a'),('b'),('b'),('b'),('b'),('c'),
  502. ('c'),('c'),('c'),('c'),('c'),('c'),('d'),('d'),('d'),('d'),('d'),('d'),('e'),
  503. ('e'),('e'),('e'),('e'),('e'),('f'),('f'),('f'),('g'),('g'),('h'),('h'),('h'),
  504. ('h'),('i'),('j'),('j'),('j'),('k'),('k'),('l'),('l'),('m'),('m'),('m'),('m'),
  505. ('n'),('n'),('n'),('o'),('o'),('o'),('p'),('p'),('p'),('q'),('q'),('q'),('r'),
  506. ('r'),('r'),('r'),('s'),('s'),('s'),('s'),('t'),('t'),('t'),('t'),('u'),('u'),
  507. ('u'),('u'),('v'),('v'),('v'),('v'),('w'),('w'),('w'),('w'),('w'),('w'),('x'),
  508. ('x'),('x'),('y'),('y'),('y'),('y'),('z'),('z'),('z'),('z');
  509. CREATE TABLE t3 (
  510. varchar_key varchar(1) DEFAULT NULL,
  511. KEY varchar_key (varchar_key)
  512. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  513. INSERT INTO t3 VALUES
  514. (NULL),('c'),('d'),('e'),('f'),('h'),('j'),('k'),('k'),('m'),('m'),('m'),
  515. ('n'),('o'),('r'),('t'),('t'),('u'),('w'),('y');
  516. SELECT varchar_key FROM t3
  517. WHERE (SELECT varchar_key FROM t3
  518. WHERE (varchar_key,varchar_key)
  519. IN (SELECT t1.varchar_key, t2 .varchar_key
  520. FROM t1 RIGHT JOIN t2 ON t1.varchar_key
  521. )
  522. );
  523. varchar_key
  524. DROP TABLE t1, t2, t3;
  525. #
  526. # Bug#46556 Returning incorrect, empty results for some IN subqueries
  527. # w/semijoin=on
  528. #
  529. CREATE TABLE t0 (
  530. pk INTEGER,
  531. vkey VARCHAR(1),
  532. vnokey VARCHAR(1),
  533. PRIMARY KEY (pk),
  534. KEY vkey(vkey)
  535. );
  536. INSERT INTO t0
  537. VALUES (1,'g','g'), (2,'v','v'), (3,'t','t'), (4,'u','u'), (5,'n','n');
  538. EXPLAIN EXTENDED SELECT vkey FROM t0 WHERE pk IN
  539. (SELECT t1.pk FROM t0 t1 JOIN t0 t2 ON t2.vkey = t1.vnokey);
  540. id select_type table type possible_keys key key_len ref rows filtered Extra
  541. 1 PRIMARY t0 ALL PRIMARY NULL NULL NULL 5 100.00
  542. 1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.t0.pk 1 100.00
  543. 1 PRIMARY t2 ref vkey vkey 4 test.t1.vnokey 2 100.00 Using index; FirstMatch(t1)
  544. Warnings:
  545. Note 1003 select `test`.`t0`.`vkey` AS `vkey` from `test`.`t0` `t1` semi join (`test`.`t0` `t2`) join `test`.`t0` where ((`test`.`t2`.`vkey` = `test`.`t1`.`vnokey`) and (`test`.`t1`.`pk` = `test`.`t0`.`pk`))
  546. SELECT vkey FROM t0 WHERE pk IN
  547. (SELECT t1.pk FROM t0 t1 JOIN t0 t2 ON t2.vkey = t1.vnokey);
  548. vkey
  549. g
  550. v
  551. t
  552. u
  553. n
  554. DROP TABLE t0;
  555. # End of bug#46556
  556. Bug #48073 Subquery on char columns from view crashes Mysql
  557. DROP TABLE IF EXISTS t1, t2;
  558. DROP VIEW IF EXISTS v1;
  559. CREATE TABLE t1 (
  560. city VARCHAR(50) NOT NULL,
  561. country_id SMALLINT UNSIGNED NOT NULL
  562. );
  563. INSERT INTO t1 VALUES
  564. ('Batna',2),
  565. ('Bchar',2),
  566. ('Skikda',2),
  567. ('Tafuna',3),
  568. ('Algeria',2) ;
  569. CREATE TABLE t2 (
  570. country_id SMALLINT UNSIGNED NOT NULL,
  571. country VARCHAR(50) NOT NULL
  572. );
  573. INSERT INTO t2 VALUES
  574. (2,'Algeria'),
  575. (3,'American Samoa') ;
  576. CREATE VIEW v1 AS
  577. SELECT country_id, country
  578. FROM t2
  579. WHERE LEFT(country,1) = "A"
  580. ;
  581. SELECT city, country_id
  582. FROM t1
  583. WHERE city IN (
  584. SELECT country
  585. FROM t2
  586. WHERE LEFT(country, 1) = "A"
  587. );
  588. city country_id
  589. Algeria 2
  590. SELECT city, country_id
  591. FROM t1
  592. WHERE city IN (
  593. SELECT country
  594. FROM v1
  595. );
  596. city country_id
  597. Algeria 2
  598. drop table t1, t2;
  599. drop view v1;
  600. # End of bug#48073
  601. Bug#48834: Procedure with view + subquery + semijoin=on
  602. crashes on second call.
  603. SET SESSION optimizer_switch ='semijoin=on';
  604. CREATE TABLE t1 ( t1field integer, primary key (t1field));
  605. CREATE TABLE t2 ( t2field integer, primary key (t2field));
  606. CREATE VIEW v1 AS
  607. SELECT t1field as v1field
  608. FROM t1 A
  609. WHERE A.t1field IN (SELECT t1field FROM t2 );
  610. CREATE VIEW v2 AS
  611. SELECT t2field as v2field
  612. FROM t2 A
  613. WHERE A.t2field IN (SELECT t2field FROM t2 );
  614. CREATE PROCEDURE p1 ()
  615. BEGIN
  616. SELECT v1field
  617. FROM v1
  618. WHERE v1field IN ( SELECT v2field as vf_inner FROM v2 );
  619. END|
  620. INSERT INTO t1 VALUES (1),(2),(3);
  621. INSERT INTO t2 VALUES (2),(3),(4);
  622. CALL p1;
  623. v1field
  624. 2
  625. 3
  626. CALL p1;
  627. v1field
  628. 2
  629. 3
  630. DROP TABLE t1,t2;
  631. DROP VIEW v1,v2;
  632. DROP PROCEDURE p1;
  633. set SESSION optimizer_switch='default';
  634. # End of BUG#48834
  635. Bug#49097 subquery with view generates wrong result with
  636. non-prepared statement
  637. DROP TABLE IF EXISTS t1, t2;
  638. DROP VIEW IF EXISTS v1;
  639. CREATE TABLE t1 (
  640. city VARCHAR(50) NOT NULL,
  641. country_id SMALLINT UNSIGNED NOT NULL
  642. );
  643. INSERT INTO t1 VALUES
  644. ('Batna',2),
  645. ('Bchar',2),
  646. ('Skikda',2),
  647. ('Tafuna',3),
  648. ('Algeria',2) ;
  649. CREATE TABLE t2 (
  650. country_id SMALLINT UNSIGNED NOT NULL,
  651. country VARCHAR(50) NOT NULL
  652. );
  653. INSERT INTO t2 VALUES
  654. (2,'Algeria'),
  655. (3,'XAmerican Samoa') ;
  656. CREATE VIEW v1 AS
  657. SELECT country_id, country
  658. FROM t2
  659. WHERE LEFT(country,1) = "A"
  660. ;
  661. SELECT city, country_id
  662. FROM t1
  663. WHERE country_id IN (
  664. SELECT country_id
  665. FROM t2
  666. WHERE LEFT(country,1) = "A"
  667. );
  668. city country_id
  669. Batna 2
  670. Bchar 2
  671. Skikda 2
  672. Algeria 2
  673. SELECT city, country_id
  674. FROM t1
  675. WHERE country_id IN (
  676. SELECT country_id
  677. FROM v1
  678. );
  679. city country_id
  680. Batna 2
  681. Bchar 2
  682. Skikda 2
  683. Algeria 2
  684. PREPARE stmt FROM
  685. "
  686. SELECT city, country_id
  687. FROM t1
  688. WHERE country_id IN (
  689. SELECT country_id
  690. FROM v1
  691. );
  692. ";
  693. execute stmt;
  694. city country_id
  695. Batna 2
  696. Bchar 2
  697. Skikda 2
  698. Algeria 2
  699. deallocate prepare stmt;
  700. drop table t1, t2;
  701. drop view v1;
  702. # End of Bug#49097
  703. #
  704. # BUG#38075: Wrong result: rows matching a subquery with outer join not returned
  705. #
  706. DROP TABLE IF EXISTS ot1, it1, it2;
  707. CREATE TABLE it2 (
  708. int_key int(11) NOT NULL,
  709. datetime_key datetime NOT NULL,
  710. KEY int_key (int_key),
  711. KEY datetime_key (datetime_key)
  712. );
  713. INSERT INTO it2 VALUES
  714. (5,'2002-04-10 14:25:30'), (0,'0000-00-00 00:00:00'),
  715. (0,'2006-09-14 04:01:02'), (4,'0000-00-00 00:00:00'),
  716. (8,'0000-00-00 00:00:00'), (5,'0000-00-00 00:00:00'),
  717. (9,'0000-00-00 00:00:00'), (8,'2007-04-01 11:04:17'),
  718. (1,'0000-00-00 00:00:00'), (7,'2009-01-12 00:00:00'),
  719. (0,'2009-06-05 00:00:00'), (3,'2006-02-14 18:06:35'),
  720. (5,'2006-02-21 07:08:16'), (0,'0000-00-00 00:00:00'),
  721. (7,'0000-00-00 00:00:00'), (0,'0000-00-00 00:00:00'),
  722. (0,'2007-02-13 00:00:00'), (1,'0000-00-00 00:00:00'),
  723. (0,'0000-00-00 00:00:00'), (1,'2003-08-11 00:00:00');
  724. CREATE TABLE ot1 (
  725. int_nokey int(11) NOT NULL,
  726. int_key int(11) NOT NULL,
  727. KEY int_key (int_key)
  728. );
  729. INSERT INTO ot1 VALUES
  730. (5,0), (3,0), (0,2), (3,0), (1,3), (0,0), (1,7), (7,0), (1,7), (0,7),
  731. (0,9), (8,2), (4,4), (9,3), (0,9), (2,5), (0,5), (8,0), (5,8), (1,5);
  732. CREATE TABLE it1 (
  733. int_nokey int(11) NOT NULL,
  734. int_key int(11) NOT NULL,
  735. KEY int_key (int_key)
  736. );
  737. INSERT INTO it1 VALUES
  738. (9,5), (0,4);
  739. SELECT int_key FROM ot1
  740. WHERE int_nokey IN (SELECT it2.int_key
  741. FROM it1 LEFT JOIN it2 ON it2.datetime_key);
  742. int_key
  743. 0
  744. 0
  745. 2
  746. 0
  747. 3
  748. 0
  749. 7
  750. 0
  751. 7
  752. 7
  753. 9
  754. 2
  755. 9
  756. 5
  757. 0
  758. 8
  759. 5
  760. EXPLAIN
  761. SELECT int_key FROM ot1
  762. WHERE int_nokey IN (SELECT it2.int_key
  763. FROM it1 LEFT JOIN it2 ON it2.datetime_key);
  764. id select_type table type possible_keys key key_len ref rows Extra
  765. 1 PRIMARY it1 index NULL int_key 4 NULL 2 Using index; Start temporary
  766. 1 PRIMARY ot1 ALL NULL NULL NULL NULL 20 Using join buffer
  767. 1 PRIMARY it2 ALL NULL NULL NULL NULL 20 Using where; End temporary
  768. DROP TABLE ot1, it1, it2;
  769. # End of BUG#38075
  770. #
  771. # BUG#31480: Incorrect result for nested subquery when executed via semi join
  772. #
  773. create table t1 (a int not null, b int not null);
  774. create table t2 (c int not null, d int not null);
  775. create table t3 (e int not null);
  776. insert into t1 values (1,10);
  777. insert into t1 values (2,10);
  778. insert into t1 values (1,20);
  779. insert into t1 values (2,20);
  780. insert into t1 values (3,20);
  781. insert into t1 values (2,30);
  782. insert into t1 values (4,40);
  783. insert into t2 values (2,10);
  784. insert into t2 values (2,20);
  785. insert into t2 values (4,10);
  786. insert into t2 values (5,10);
  787. insert into t2 values (3,20);
  788. insert into t2 values (2,40);
  789. insert into t3 values (10);
  790. insert into t3 values (30);
  791. insert into t3 values (10);
  792. insert into t3 values (20);
  793. explain extended
  794. select a from t1
  795. where a in (select c from t2 where d >= some(select e from t3 where b=e));
  796. id select_type table type possible_keys key key_len ref rows filtered Extra
  797. 1 PRIMARY t2 ALL NULL NULL NULL NULL 6 100.00 Start temporary
  798. 1 PRIMARY t1 ALL NULL NULL NULL NULL 7 100.00 Using where; End temporary; Using join buffer
  799. 3 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 4 100.00 Using where
  800. Warnings:
  801. Note 1276 Field or reference 'test.t1.b' of SELECT #3 was resolved in SELECT #1
  802. Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` semi join (`test`.`t2`) where ((`test`.`t1`.`a` = `test`.`t2`.`c`) and <nop>(<in_optimizer>(`test`.`t2`.`d`,<exists>(select 1 AS `Not_used` from `test`.`t3` where ((`test`.`t1`.`b` = `test`.`t3`.`e`) and (<cache>(`test`.`t2`.`d`) >= `test`.`t3`.`e`))))))
  803. show warnings;
  804. Level Code Message
  805. Note 1276 Field or reference 'test.t1.b' of SELECT #3 was resolved in SELECT #1
  806. Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` semi join (`test`.`t2`) where ((`test`.`t1`.`a` = `test`.`t2`.`c`) and <nop>(<in_optimizer>(`test`.`t2`.`d`,<exists>(select 1 AS `Not_used` from `test`.`t3` where ((`test`.`t1`.`b` = `test`.`t3`.`e`) and (<cache>(`test`.`t2`.`d`) >= `test`.`t3`.`e`))))))
  807. select a from t1
  808. where a in (select c from t2 where d >= some(select e from t3 where b=e));
  809. a
  810. 2
  811. 2
  812. 3
  813. 2
  814. drop table t1, t2, t3;