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.

568 lines
22 KiB

MWL#17: Table elimination - Make elimination work with aggregate functions. The problem was that aggregate functions reported all table bits in used_tables(), and that prevented table elimination. Fixed by making aggregate functions return more correct value from used_tables(). mysql-test/r/ps_11bugs.result: MWL#17: Table elimination - Update test results. The difference is because of Item_ref change: outer references to constants are now recognized as constants, too. mysql-test/r/subselect.result: - Update test results. The difference is because of Item_ref change: outer references to constants are now recognized as constants, too. mysql-test/r/table_elim.result: MWL#17: Table elimination - Check that elimination works in presense of aggreagate functions mysql-test/t/table_elim.test: MWL#17: Table elimination - Check that elimination works in presense of aggreagate functions sql/item.h: MWL#17: Table elimination - Add Item_ref::const_item() which calls (*ref)->const_item(). Before this diff Item_ref used the default implementation of const_item(){ return used_tables()==0; }. This is no longer true, as COUNT(*) now has used_tables()==0 but const_item()==FALSE. sql/item_sum.cc: MWL#17: Table elimination - Make Item_sum() and it descendants not to return all bits in used_tables(). This is needed because otherwise table elimination can't work in presense of aggregate functions - COUNT(*) now has used_tables()==0 and const_item()==FALSE. Had to change Item_ref::const_item() to account for this. sql/item_sum.h: MWL#17: Table elimination - Add comments
17 years ago
  1. drop table if exists t0, t1, t2, t3, t4, t5, t6;
  2. drop view if exists v1, v2;
  3. create table t1 (a int);
  4. insert into t1 values (0),(1),(2),(3);
  5. create table t0 as select * from t1;
  6. create table t2 (a int primary key, b int)
  7. as select a, a as b from t1 where a in (1,2);
  8. create table t3 (a int primary key, b int)
  9. as select a, a as b from t1 where a in (1,3);
  10. # This will be eliminated:
  11. explain select t1.a from t1 left join t2 on t2.a=t1.a;
  12. id select_type table type possible_keys key key_len ref rows Extra
  13. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4
  14. explain extended select t1.a from t1 left join t2 on t2.a=t1.a;
  15. id select_type table type possible_keys key key_len ref rows filtered Extra
  16. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4 100.00
  17. Warnings:
  18. Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where 1
  19. select t1.a from t1 left join t2 on t2.a=t1.a;
  20. a
  21. 0
  22. 1
  23. 2
  24. 3
  25. # This will not be eliminated as t2.b is in in select list:
  26. explain select * from t1 left join t2 on t2.a=t1.a;
  27. id select_type table type possible_keys key key_len ref rows Extra
  28. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4
  29. 1 SIMPLE t2 eq_ref PRIMARY PRIMARY 4 test.t1.a 1
  30. # This will not be eliminated as t2.b is in in order list:
  31. explain select t1.a from t1 left join t2 on t2.a=t1.a order by t2.b;
  32. id select_type table type possible_keys key key_len ref rows Extra
  33. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4 Using temporary; Using filesort
  34. 1 SIMPLE t2 eq_ref PRIMARY PRIMARY 4 test.t1.a 1
  35. # This will not be eliminated as t2.b is in group list:
  36. explain select t1.a from t1 left join t2 on t2.a=t1.a group by t2.b;
  37. id select_type table type possible_keys key key_len ref rows Extra
  38. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4 Using temporary; Using filesort
  39. 1 SIMPLE t2 eq_ref PRIMARY PRIMARY 4 test.t1.a 1
  40. # This will not be eliminated as t2.b is in the WHERE
  41. explain select t1.a from t1 left join t2 on t2.a=t1.a where t2.b < 3 or t2.b is null;
  42. id select_type table type possible_keys key key_len ref rows Extra
  43. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4
  44. 1 SIMPLE t2 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 Using where
  45. # Elimination of multiple tables:
  46. explain select t1.a from t1 left join (t2 join t3) on t2.a=t1.a and t3.a=t1.a;
  47. id select_type table type possible_keys key key_len ref rows Extra
  48. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4
  49. # Elimination of multiple tables (2):
  50. explain select t1.a from t1 left join (t2 join t3 on t2.b=t3.b) on t2.a=t1.a and t3.a=t1.a;
  51. id select_type table type possible_keys key key_len ref rows Extra
  52. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4
  53. # Elimination when done within an outer join nest:
  54. explain extended
  55. select t0.*
  56. from
  57. t0 left join (t1 left join (t2 join t3 on t2.b=t3.b) on t2.a=t1.a and
  58. t3.a=t1.a) on t0.a=t1.a;
  59. id select_type table type possible_keys key key_len ref rows filtered Extra
  60. 1 SIMPLE t0 ALL NULL NULL NULL NULL 4 100.00
  61. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4 100.00
  62. Warnings:
  63. Note 1003 select `test`.`t0`.`a` AS `a` from `test`.`t0` left join (`test`.`t1`) on((`test`.`t1`.`a` = `test`.`t0`.`a`)) where 1
  64. # Elimination with aggregate functions
  65. explain select count(*) from t1 left join t2 on t2.a=t1.a;
  66. id select_type table type possible_keys key key_len ref rows Extra
  67. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4
  68. explain select count(1) from t1 left join t2 on t2.a=t1.a;
  69. id select_type table type possible_keys key key_len ref rows Extra
  70. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4
  71. explain select count(1) from t1 left join t2 on t2.a=t1.a group by t1.a;
  72. id select_type table type possible_keys key key_len ref rows Extra
  73. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4 Using temporary; Using filesort
  74. This must not use elimination:
  75. explain select count(1) from t1 left join t2 on t2.a=t1.a group by t2.a;
  76. id select_type table type possible_keys key key_len ref rows Extra
  77. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4 Using temporary; Using filesort
  78. 1 SIMPLE t2 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 Using index
  79. drop table t0, t1, t2, t3;
  80. create table t0 ( id integer, primary key (id));
  81. create table t1 (
  82. id integer,
  83. attr1 integer,
  84. primary key (id),
  85. key (attr1)
  86. );
  87. create table t2 (
  88. id integer,
  89. attr2 integer,
  90. fromdate date,
  91. primary key (id, fromdate),
  92. key (attr2,fromdate)
  93. );
  94. insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
  95. insert into t0 select A.id + 10*B.id from t0 A, t0 B where B.id > 0;
  96. insert into t1 select id, id from t0;
  97. insert into t2 select id, id, date_add('2009-06-22', interval id day) from t0;
  98. insert into t2 select id, id+1, date_add('2008-06-22', interval id day) from t0;
  99. create view v1 as
  100. select
  101. f.id, a1.attr1, a2.attr2
  102. from
  103. t0 f
  104. left join t1 a1 on a1.id=f.id
  105. left join t2 a2 on a2.id=f.id and
  106. a2.fromdate=(select MAX(fromdate) from
  107. t2 where id=a2.id);
  108. create view v2 as
  109. select
  110. f.id, a1.attr1, a2.attr2
  111. from
  112. t0 f
  113. left join t1 a1 on a1.id=f.id
  114. left join t2 a2 on a2.id=f.id and
  115. a2.fromdate=(select MAX(fromdate) from
  116. t2 where id=f.id);
  117. This should use one table:
  118. explain select id from v1 where id=2;
  119. id select_type table type possible_keys key key_len ref rows Extra
  120. 1 PRIMARY f const PRIMARY PRIMARY 4 const 1 Using index
  121. This should use one table:
  122. explain extended select id from v1 where id in (1,2,3,4);
  123. id select_type table type possible_keys key key_len ref rows filtered Extra
  124. 1 PRIMARY f range PRIMARY PRIMARY 4 NULL 4 100.00 Using where; Using index
  125. Warnings:
  126. Note 1276 Field or reference 'test.a2.id' of SELECT #3 was resolved in SELECT #1
  127. Note 1003 select `f`.`id` AS `id` from `test`.`t0` `f` where (`f`.`id` in (1,2,3,4))
  128. This should use facts and a1 tables:
  129. explain extended select id from v1 where attr1 between 12 and 14;
  130. id select_type table type possible_keys key key_len ref rows filtered Extra
  131. 1 PRIMARY a1 range PRIMARY,attr1 attr1 5 NULL 2 100.00 Using where
  132. 1 PRIMARY f eq_ref PRIMARY PRIMARY 4 test.a1.id 1 100.00 Using index
  133. Warnings:
  134. Note 1276 Field or reference 'test.a2.id' of SELECT #3 was resolved in SELECT #1
  135. Note 1003 select `f`.`id` AS `id` from `test`.`t0` `f` join `test`.`t1` `a1` where ((`f`.`id` = `a1`.`id`) and (`a1`.`attr1` between 12 and 14))
  136. This should use facts, a2 and its subquery:
  137. explain extended select id from v1 where attr2 between 12 and 14;
  138. id select_type table type possible_keys key key_len ref rows filtered Extra
  139. 1 PRIMARY a2 range PRIMARY,attr2 attr2 5 NULL 5 100.00 Using where
  140. 1 PRIMARY f eq_ref PRIMARY PRIMARY 4 test.a2.id 1 100.00 Using index
  141. 3 DEPENDENT SUBQUERY t2 ref PRIMARY PRIMARY 4 test.a2.id 2 100.00 Using index
  142. Warnings:
  143. Note 1276 Field or reference 'test.a2.id' of SELECT #3 was resolved in SELECT #1
  144. Note 1003 select `f`.`id` AS `id` from `test`.`t0` `f` join `test`.`t2` `a2` where ((`f`.`id` = `a2`.`id`) and (`a2`.`attr2` between 12 and 14) and (`a2`.`fromdate` = (select max(`test`.`t2`.`fromdate`) from `test`.`t2` where (`test`.`t2`.`id` = `a2`.`id`))))
  145. This should use one table:
  146. explain select id from v2 where id=2;
  147. id select_type table type possible_keys key key_len ref rows Extra
  148. 1 PRIMARY f const PRIMARY PRIMARY 4 const 1 Using index
  149. This should use one table:
  150. explain extended select id from v2 where id in (1,2,3,4);
  151. id select_type table type possible_keys key key_len ref rows filtered Extra
  152. 1 PRIMARY f range PRIMARY PRIMARY 4 NULL 4 100.00 Using where; Using index
  153. Warnings:
  154. Note 1276 Field or reference 'test.f.id' of SELECT #3 was resolved in SELECT #1
  155. Note 1003 select `f`.`id` AS `id` from `test`.`t0` `f` where (`f`.`id` in (1,2,3,4))
  156. This should use facts and a1 tables:
  157. explain extended select id from v2 where attr1 between 12 and 14;
  158. id select_type table type possible_keys key key_len ref rows filtered Extra
  159. 1 PRIMARY a1 range PRIMARY,attr1 attr1 5 NULL 2 100.00 Using where
  160. 1 PRIMARY f eq_ref PRIMARY PRIMARY 4 test.a1.id 1 100.00 Using index
  161. Warnings:
  162. Note 1276 Field or reference 'test.f.id' of SELECT #3 was resolved in SELECT #1
  163. Note 1003 select `f`.`id` AS `id` from `test`.`t0` `f` join `test`.`t1` `a1` where ((`f`.`id` = `a1`.`id`) and (`a1`.`attr1` between 12 and 14))
  164. This should use facts, a2 and its subquery:
  165. explain extended select id from v2 where attr2 between 12 and 14;
  166. id select_type table type possible_keys key key_len ref rows filtered Extra
  167. 1 PRIMARY a2 range PRIMARY,attr2 attr2 5 NULL 5 100.00 Using where
  168. 1 PRIMARY f eq_ref PRIMARY PRIMARY 4 test.a2.id 1 100.00 Using where; Using index
  169. 3 DEPENDENT SUBQUERY t2 ref PRIMARY PRIMARY 4 test.f.id 2 100.00 Using index
  170. Warnings:
  171. Note 1276 Field or reference 'test.f.id' of SELECT #3 was resolved in SELECT #1
  172. Note 1003 select `f`.`id` AS `id` from `test`.`t0` `f` join `test`.`t2` `a2` where ((`f`.`id` = `a2`.`id`) and (`a2`.`attr2` between 12 and 14) and (`a2`.`fromdate` = (select max(`test`.`t2`.`fromdate`) from `test`.`t2` where (`test`.`t2`.`id` = `f`.`id`))))
  173. drop view v1, v2;
  174. drop table t0, t1, t2;
  175. create table t1 (a int);
  176. insert into t1 values (0),(1),(2),(3);
  177. create table t2 (pk1 int, pk2 int, pk3 int, col int, primary key(pk1, pk2, pk3));
  178. insert into t2 select a,a,a,a from t1;
  179. This must use only t1:
  180. explain select t1.* from t1 left join t2 on t2.pk1=t1.a and
  181. t2.pk2=t2.pk1+1 and
  182. t2.pk3=t2.pk2+1;
  183. id select_type table type possible_keys key key_len ref rows Extra
  184. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4
  185. This must use only t1:
  186. explain select t1.* from t1 left join t2 on t2.pk1=t1.a and
  187. t2.pk3=t2.pk1+1 and
  188. t2.pk2=t2.pk3+1;
  189. id select_type table type possible_keys key key_len ref rows Extra
  190. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4
  191. This must use both:
  192. explain select t1.* from t1 left join t2 on t2.pk1=t1.a and
  193. t2.pk3=t2.pk1+1 and
  194. t2.pk2=t2.pk3+t2.col;
  195. id select_type table type possible_keys key key_len ref rows Extra
  196. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4
  197. 1 SIMPLE t2 ref PRIMARY PRIMARY 4 test.t1.a 1
  198. This must use only t1:
  199. explain select t1.* from t1 left join t2 on t2.pk2=t1.a and
  200. t2.pk1=t2.pk2+1 and
  201. t2.pk3=t2.pk1;
  202. id select_type table type possible_keys key key_len ref rows Extra
  203. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4
  204. drop table t1, t2;
  205. create table t1 (pk int primary key, col int);
  206. insert into t1 values (1,1),(2,2);
  207. create table t2 like t1;
  208. insert into t2 select * from t1;
  209. create table t3 like t1;
  210. insert into t3 select * from t1;
  211. explain
  212. select t1.* from t1 left join ( t2 left join t3 on t3.pk=t2.col) on t2.col=t1.col;
  213. id select_type table type possible_keys key key_len ref rows Extra
  214. 1 SIMPLE t1 ALL NULL NULL NULL NULL 2
  215. 1 SIMPLE t2 ALL NULL NULL NULL NULL 2
  216. explain
  217. select t1.*, t2.* from t1 left join (t2 left join t3 on t3.pk=t2.col) on t2.pk=t1.col;
  218. id select_type table type possible_keys key key_len ref rows Extra
  219. 1 SIMPLE t1 ALL NULL NULL NULL NULL 2
  220. 1 SIMPLE t2 eq_ref PRIMARY PRIMARY 4 test.t1.col 1
  221. explain select t1.*
  222. from
  223. t1 left join ( t2 left join t3 on t3.pk=t2.col or t3.pk=t2.col)
  224. on t2.col=t1.col or t2.col=t1.col;
  225. id select_type table type possible_keys key key_len ref rows Extra
  226. 1 SIMPLE t1 ALL NULL NULL NULL NULL 2
  227. 1 SIMPLE t2 ALL NULL NULL NULL NULL 2
  228. explain select t1.*, t2.*
  229. from
  230. t1 left join
  231. (t2 left join t3 on t3.pk=t2.col or t3.pk=t2.col)
  232. on t2.pk=t1.col or t2.pk=t1.col;
  233. id select_type table type possible_keys key key_len ref rows Extra
  234. 1 SIMPLE t1 ALL NULL NULL NULL NULL 2
  235. 1 SIMPLE t2 eq_ref PRIMARY PRIMARY 4 test.t1.col 1
  236. drop table t1, t2, t3;
  237. #
  238. # Check things that look like functional dependencies but really are not
  239. #
  240. create table t1 (a char(10) character set latin1 collate latin1_general_ci primary key);
  241. insert into t1 values ('foo');
  242. insert into t1 values ('bar');
  243. create table t2 (a char(10) character set latin1 collate latin1_general_cs primary key);
  244. insert into t2 values ('foo');
  245. insert into t2 values ('FOO');
  246. this must not use table elimination:
  247. explain select t1.* from t1 left join t2 on t2.a='foo' collate latin1_general_ci;
  248. id select_type table type possible_keys key key_len ref rows Extra
  249. 1 SIMPLE t1 index NULL PRIMARY 10 NULL 2 Using index
  250. 1 SIMPLE t2 index PRIMARY PRIMARY 10 NULL 2 Using index
  251. this must not use table elimination:
  252. explain select t1.* from t1 left join t2 on t2.a=t1.a collate latin1_general_ci;
  253. id select_type table type possible_keys key key_len ref rows Extra
  254. 1 SIMPLE t1 index NULL PRIMARY 10 NULL 2 Using index
  255. 1 SIMPLE t2 index PRIMARY PRIMARY 10 NULL 2 Using index
  256. drop table t1,t2;
  257. create table t1 (a int primary key);
  258. insert into t1 values (1),(2);
  259. create table t2 (a char(10) primary key);
  260. insert into t2 values ('1'),('1.0');
  261. this must not use table elimination:
  262. explain select t1.* from t1 left join t2 on t2.a=1;
  263. id select_type table type possible_keys key key_len ref rows Extra
  264. 1 SIMPLE t1 index NULL PRIMARY 4 NULL 2 Using index
  265. 1 SIMPLE t2 index PRIMARY PRIMARY 10 NULL 2 Using index
  266. this must not use table elimination:
  267. explain select t1.* from t1 left join t2 on t2.a=t1.a;
  268. id select_type table type possible_keys key key_len ref rows Extra
  269. 1 SIMPLE t1 index NULL PRIMARY 4 NULL 2 Using index
  270. 1 SIMPLE t2 index PRIMARY PRIMARY 10 NULL 2 Using index
  271. drop table t1, t2;
  272. create table t1 (a char(10) primary key);
  273. insert into t1 values ('foo'),('bar');
  274. create table t2 (a char(10), unique key(a(2)));
  275. insert into t2 values ('foo'),('bar');
  276. explain select t1.* from t1 left join t2 on t2.a=t1.a;
  277. id select_type table type possible_keys key key_len ref rows Extra
  278. 1 SIMPLE t1 index NULL PRIMARY 10 NULL 2 Using index
  279. 1 SIMPLE t2 ref a a 3 test.t1.a 2
  280. drop table t1, t2;
  281. #
  282. # check UPDATE/DELETE that look like they could be eliminated
  283. #
  284. create table t1 (a int primary key, b int);
  285. insert into t1 values (1,1),(2,2),(3,3);
  286. create table t2 like t1;
  287. insert into t2 select * from t1;
  288. update t1 left join t2 using (a) set t2.a=t2.a+100;
  289. select * from t1;
  290. a b
  291. 1 1
  292. 2 2
  293. 3 3
  294. select * from t2;
  295. a b
  296. 101 1
  297. 102 2
  298. 103 3
  299. delete from t2;
  300. insert into t2 select * from t1;
  301. delete t2 from t1 left join t2 using (a);
  302. select * from t1;
  303. a b
  304. 1 1
  305. 2 2
  306. 3 3
  307. select * from t2;
  308. a b
  309. drop table t1, t2;
  310. #
  311. # Tests with various edge-case ON expressions
  312. #
  313. create table t1 (a int, b int, c int, d int);
  314. insert into t1 values (0,0,0,0),(1,1,1,1),(2,2,2,2),(3,3,3,3);
  315. create table t2 (pk int primary key, b int)
  316. as select a as pk, a as b from t1 where a in (1,2);
  317. create table t3 (pk1 int, pk2 int, b int, unique(pk1,pk2));
  318. insert into t3 select a as pk1, a as pk2, a as b from t1 where a in (1,3);
  319. explain select t1.a from t1 left join t2 on t2.pk=t1.a and t2.b<t1.b;
  320. id select_type table type possible_keys key key_len ref rows Extra
  321. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4
  322. explain select t1.a from t1 left join t2 on t2.pk=t1.a or t2.b<t1.b;
  323. id select_type table type possible_keys key key_len ref rows Extra
  324. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4
  325. 1 SIMPLE t2 ALL PRIMARY NULL NULL NULL 2
  326. explain select t1.a from t1 left join t2 on t2.b<t1.b or t2.pk=t1.a;
  327. id select_type table type possible_keys key key_len ref rows Extra
  328. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4
  329. 1 SIMPLE t2 ALL PRIMARY NULL NULL NULL 2
  330. explain select t1.a from t1 left join t2 on t2.pk between 10 and 20;
  331. id select_type table type possible_keys key key_len ref rows Extra
  332. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4
  333. 1 SIMPLE t2 index PRIMARY PRIMARY 4 NULL 2 Using index
  334. explain select t1.a from t1 left join t2 on t2.pk between 0.5 and 1.5;
  335. id select_type table type possible_keys key key_len ref rows Extra
  336. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4
  337. 1 SIMPLE t2 index PRIMARY PRIMARY 4 NULL 2 Using index
  338. explain select t1.a from t1 left join t2 on t2.pk between 10 and 10;
  339. id select_type table type possible_keys key key_len ref rows Extra
  340. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4
  341. explain select t1.a from t1 left join t2 on t2.pk in (10);
  342. id select_type table type possible_keys key key_len ref rows Extra
  343. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4
  344. explain select t1.a from t1 left join t2 on t2.pk in (t1.a);
  345. id select_type table type possible_keys key key_len ref rows Extra
  346. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4
  347. explain select t1.a from t1 left join t2 on TRUE;
  348. id select_type table type possible_keys key key_len ref rows Extra
  349. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4
  350. 1 SIMPLE t2 index NULL PRIMARY 4 NULL 2 Using index
  351. explain select t1.a from t1 left join t3 on t3.pk1=t1.a and t3.pk2 IS NULL;
  352. id select_type table type possible_keys key key_len ref rows Extra
  353. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4
  354. drop table t1,t2,t3;
  355. #
  356. # Multi-equality tests
  357. #
  358. create table t1 (a int, b int, c int, d int);
  359. insert into t1 values (0,0,0,0),(1,1,1,1),(2,2,2,2),(3,3,3,3);
  360. create table t2 (pk int primary key, b int, c int);
  361. insert into t2 select a,a,a from t1 where a in (1,2);
  362. explain
  363. select t1.*
  364. from t1 left join t2 on t2.pk=t2.c and t2.b=t1.a and t1.a=t1.b and t2.c=t2.b
  365. where t1.d=1;
  366. id select_type table type possible_keys key key_len ref rows Extra
  367. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4 Using where
  368. explain
  369. select t1.*
  370. from
  371. t1
  372. left join
  373. t2
  374. on (t2.pk=t2.c and t2.b=t1.a and t1.a=t1.b and t2.c=t2.b) or
  375. (t2.pk=t2.c and t2.b=t1.a and t1.a=t1.b and t2.c=t2.b)
  376. where t1.d=1;
  377. id select_type table type possible_keys key key_len ref rows Extra
  378. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4 Using where
  379. #This can't be eliminated:
  380. explain
  381. select t1.*
  382. from
  383. t1
  384. left join
  385. t2
  386. on (t2.pk=t2.c and t2.b=t1.a and t2.c=t1.b) or
  387. (t2.pk=t2.c and t1.a=t1.b and t2.c=t1.b)
  388. where t1.d=1;
  389. id select_type table type possible_keys key key_len ref rows Extra
  390. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4 Using where
  391. 1 SIMPLE t2 eq_ref PRIMARY PRIMARY 4 test.t1.b 1
  392. explain
  393. select t1.*
  394. from
  395. t1
  396. left join
  397. t2
  398. on (t2.pk=t2.c and t2.b=t1.a and t2.c=t1.b) or
  399. (t2.pk=t2.c and t2.c=t1.b)
  400. ;
  401. id select_type table type possible_keys key key_len ref rows Extra
  402. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4
  403. explain
  404. select t1.*
  405. from t1 left join t2 on t2.pk=3 or t2.pk= 4;
  406. id select_type table type possible_keys key key_len ref rows Extra
  407. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4
  408. 1 SIMPLE t2 index PRIMARY PRIMARY 4 NULL 2 Using index
  409. explain
  410. select t1.*
  411. from t1 left join t2 on t2.pk=3 or t2.pk= 3;
  412. id select_type table type possible_keys key key_len ref rows Extra
  413. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4
  414. explain
  415. select t1.*
  416. from t1 left join t2 on (t2.pk=3 and t2.b=3) or (t2.pk= 4 and t2.b=3);
  417. id select_type table type possible_keys key key_len ref rows Extra
  418. 1 SIMPLE t1 ALL NULL NULL NULL NULL 4
  419. 1 SIMPLE t2 range PRIMARY PRIMARY 4 NULL 2 Using where
  420. drop table t1, t2;
  421. #
  422. # LPBUG#523593: Running RQG optimizer_no_subquery crashes MariaDB
  423. #
  424. CREATE TABLE t1 (
  425. `pk` int(11) NOT NULL AUTO_INCREMENT,
  426. `col_int_nokey` int(11) DEFAULT NULL,
  427. `col_int_key` int(11) DEFAULT NULL,
  428. `col_date_key` date DEFAULT NULL,
  429. `col_date_nokey` date DEFAULT NULL,
  430. `col_time_key` time DEFAULT NULL,
  431. `col_time_nokey` time DEFAULT NULL,
  432. `col_datetime_key` datetime DEFAULT NULL,
  433. `col_datetime_nokey` datetime DEFAULT NULL,
  434. `col_varchar_key` varchar(1) DEFAULT NULL,
  435. `col_varchar_nokey` varchar(1) DEFAULT NULL,
  436. PRIMARY KEY (`pk`),
  437. KEY `col_int_key` (`col_int_key`),
  438. KEY `col_date_key` (`col_date_key`),
  439. KEY `col_time_key` (`col_time_key`),
  440. KEY `col_datetime_key` (`col_datetime_key`),
  441. KEY `col_varchar_key` (`col_varchar_key`,`col_int_key`)
  442. );
  443. CREATE TABLE t2 LIKE t1;
  444. INSERT INTO t1 VALUES
  445. (10,7,8,NULL,NULL,'01:27:35','01:27:35','2002-02-26 06:14:37','2002-02-26 06:14:37','v','v'),
  446. (11,1,9,'2006-06-14','2006-06-14','19:48:31','19:48:31','1900-01-01 00:00:00','1900-01-01 00:00:00','r','r');
  447. INSERT INTO t2 SELECT * FROM t1;
  448. SELECT table2.col_int_key AS field1
  449. FROM (
  450. t2 AS table1
  451. RIGHT OUTER JOIN
  452. (
  453. ( t1 AS table2 STRAIGHT_JOIN
  454. t1 AS table3 ON (
  455. (table3.col_varchar_nokey = table2.col_varchar_key ) AND
  456. (table3.pk = table2.col_int_key))
  457. )
  458. ) ON
  459. (
  460. (table3.col_varchar_key = table2.col_varchar_key) OR
  461. (table3.col_int_key = table2.pk)
  462. )
  463. )
  464. HAVING field1 < 216;
  465. field1
  466. DROP TABLE t1, t2;
  467. #
  468. # LPBUG#524025 Running RQG outer_join test leads to crash
  469. #
  470. CREATE TABLE t0 (
  471. pk int(11) NOT NULL AUTO_INCREMENT,
  472. PRIMARY KEY (pk)
  473. );
  474. CREATE TABLE t1 (
  475. col_int int(11) DEFAULT NULL,
  476. col_int_key int(11) DEFAULT NULL,
  477. pk int(11) NOT NULL AUTO_INCREMENT,
  478. col_varchar_10_latin1 varchar(10) DEFAULT NULL,
  479. PRIMARY KEY (pk)
  480. );
  481. INSERT INTO t1 VALUES (5,5,1,'t'), (NULL,NULL,2,'y');
  482. CREATE TABLE t2 (
  483. col_int int(11) DEFAULT NULL
  484. );
  485. INSERT INTO t2 VALUES (8), (4);
  486. CREATE TABLE t3 (
  487. pk int(11) NOT NULL AUTO_INCREMENT,
  488. PRIMARY KEY (pk)
  489. );
  490. INSERT INTO t3 VALUES (1),(8);
  491. CREATE TABLE t4 (
  492. pk int(11) NOT NULL AUTO_INCREMENT,
  493. col_varchar_1024_latin1_key varchar(1024) DEFAULT NULL,
  494. col_int int(11) DEFAULT NULL,
  495. PRIMARY KEY (pk)
  496. );
  497. INSERT INTO t4 VALUES (1,'o',1), (2,'w',2);
  498. CREATE TABLE t5 (
  499. col_varchar_1024_utf8_key varchar(1024) CHARACTER SET utf8 DEFAULT NULL,
  500. col_varchar_1024_latin1_key varchar(1024) DEFAULT NULL,
  501. col_varchar_10_utf8_key varchar(1024) CHARACTER SET utf8 DEFAULT NULL,
  502. pk int(11) NOT NULL AUTO_INCREMENT,
  503. col_int_key int(11) DEFAULT NULL,
  504. PRIMARY KEY (pk)
  505. );
  506. INSERT INTO t5 VALUES ('k','a','z',1,2),('x','a','w',2,7);
  507. CREATE TABLE t6 (
  508. col_int int(11) DEFAULT NULL,
  509. col_int_key int(11) DEFAULT NULL
  510. );
  511. INSERT INTO t6 VALUES (6,1),(8,3);
  512. SELECT
  513. table3.col_int AS field1,
  514. table1.col_int AS field2,
  515. table1.col_int_key AS field3,
  516. table1.pk AS field4,
  517. table1.col_int AS field5,
  518. table2.col_int AS field6
  519. FROM
  520. t1 AS table1
  521. LEFT OUTER JOIN
  522. t4 AS table2
  523. LEFT JOIN t6 AS table3
  524. RIGHT JOIN t3 AS table4
  525. LEFT JOIN t5 AS table5 ON table4.pk = table5.pk
  526. LEFT JOIN t0 AS table6 ON table5.col_int_key = table6.pk
  527. ON table3.col_int_key = table5.pk
  528. ON table2.col_varchar_1024_latin1_key = table5.col_varchar_10_utf8_key
  529. LEFT JOIN t6 AS table7 ON table2.pk = table7.col_int
  530. ON table1.col_varchar_10_latin1 = table5.col_varchar_1024_latin1_key
  531. LEFT JOIN t2 AS table8 ON table3.col_int = table8.col_int
  532. WHERE
  533. table1.col_int_key < table2.pk
  534. HAVING
  535. field4 != 6;
  536. field1 field2 field3 field4 field5 field6
  537. drop table t0,t1,t2,t3,t4,t5,t6;
  538. #
  539. # BUG#675118: Elimination of a table results in an invalid execution plan
  540. #
  541. CREATE TABLE t1 (f1 int(11), PRIMARY KEY (f1)) ;
  542. CREATE TABLE t2 (f4 varchar(1024), KEY (f4)) ;
  543. Warnings:
  544. Warning 1071 Specified key was too long; max key length is 1000 bytes
  545. INSERT IGNORE INTO t2 VALUES ('xcddwntkbxyorzdv'),
  546. ('cnxxcddwntkbxyor'),('r'),('r'), ('did'),('I'),('when'),
  547. ('hczkfqjeggivdvac'),('e'),('okay'),('up');
  548. CREATE TABLE t3 (f4 varchar(1024), f1 int(11), f2 int(11)) ;
  549. INSERT IGNORE INTO t3 VALUES ('f','4','0'),('n','5','-996540416');
  550. CREATE TABLE t4 (f1 int(11), f3 varchar(10)) ;
  551. INSERT IGNORE INTO t4 VALUES ('8','n'),('9','nwzcerzsgx'),('10','c');
  552. CREATE TABLE t5 (f5 int(11), KEY (f5)) ;
  553. EXPLAIN
  554. SELECT t3.f2
  555. FROM t2
  556. LEFT JOIN t3
  557. LEFT JOIN t4
  558. LEFT JOIN t1 ON t4.f1 = t1.f1
  559. JOIN t5 ON t4.f3 ON t3.f1 = t5.f5 ON t2.f4 = t3.f4
  560. WHERE t3.f2 ;
  561. id select_type table type possible_keys key key_len ref rows Extra
  562. 1 SIMPLE t3 ALL NULL NULL NULL NULL 2 Using where
  563. 1 SIMPLE t5 ref f5 f5 5 test.t3.f1 2 Using index
  564. 1 SIMPLE t4 ALL NULL NULL NULL NULL 3
  565. 1 SIMPLE t2 ALL f4 NULL NULL NULL 11 Using where; Using join buffer
  566. # ^^ The above must not produce a QEP of t3,t5,t2,t4
  567. # as that violates the "no interleaving of outer join nests" rule.
  568. DROP TABLE t1,t2,t3,t4,t5;