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.

565 lines
19 KiB

Back-ported the patch of the mysql-5.6 code line that fixed several defects in the greedy optimization: 1) The greedy optimizer calculated the 'compare-cost' (CPU-cost) for iterating over the partial plan result at each level in the query plan as 'record_count / (double) TIME_FOR_COMPARE' This cost was only used locally for 'best' calculation at each level, and *not* accumulated into the total cost for the query plan. This fix added the 'CPU-cost' of processing 'current_record_count' records at each level to 'current_read_time' *before* it is used as 'accumulated cost' argument to recursive best_extension_by_limited_search() calls. This ensured that the cost of a huge join-fanout early in the QEP was correctly reflected in the cost of the final QEP. To get identical cost for a 'best' optimized query and a straight_join with the same join order, the same change was also applied to optimize_straight_join() and get_partial_join_cost() 2) Furthermore to get equal cost for 'best' optimized query and a straight_join the new code substrcated the same '0.001' in optimize_straight_join() as it had been already done in best_extension_by_limited_search() 3) When best_extension_by_limited_search() aggregated the 'best' plan a plan was 'best' by the check : 'if ((search_depth == 1) || (current_read_time < join->best_read))' The term '(search_depth == 1' incorrectly caused a new best plan to be collected whenever the specified 'search_depth' was reached - even if this partial query plan was more expensive than what we had already found.
14 years ago
  1. set @subselect_mat_cost=@@optimizer_switch;
  2. set optimizer_switch='mrr=on,mrr_sort_keys=on,index_condition_pushdown=on';
  3. set log_slow_time=0.1;
  4. TEST GROUP 1:
  5. Typical cases of in-to-exists and materialization subquery strategies
  6. =====================================================================
  7. drop database if exists world;
  8. set names utf8;
  9. create database world;
  10. use world;
  11. CREATE TABLE Country (
  12. Code char(3) NOT NULL default '',
  13. Name char(52) NOT NULL default '',
  14. SurfaceArea float(10,2) NOT NULL default '0.00',
  15. Population int(11) NOT NULL default '0',
  16. Capital int(11) default NULL,
  17. PRIMARY KEY (Code),
  18. UNIQUE INDEX (Name)
  19. );
  20. CREATE TABLE City (
  21. ID int(11) NOT NULL auto_increment,
  22. Name char(35) NOT NULL default '',
  23. Country char(3) NOT NULL default '',
  24. Population int(11) NOT NULL default '0',
  25. PRIMARY KEY (ID),
  26. INDEX (Population),
  27. INDEX (Country)
  28. );
  29. CREATE TABLE CountryLanguage (
  30. Country char(3) NOT NULL default '',
  31. Language char(30) NOT NULL default '',
  32. Percentage float(3,1) NOT NULL default '0.0',
  33. PRIMARY KEY (Country, Language),
  34. INDEX (Percentage)
  35. );
  36. Make the schema and data more diverse by adding more indexes, nullable
  37. columns, and NULL data.
  38. create index SurfaceArea on Country(SurfaceArea);
  39. create index Language on CountryLanguage(Language);
  40. create index CityName on City(Name);
  41. alter table City change population population int(11) null default 0;
  42. select max(id) from City into @max_city_id;
  43. insert into City values (@max_city_id + 1,'Kilifarevo','BGR',NULL);
  44. SELECT COUNT(*) FROM Country;
  45. COUNT(*)
  46. 239
  47. SELECT COUNT(*) FROM City;
  48. COUNT(*)
  49. 4080
  50. SELECT COUNT(*) FROM CountryLanguage;
  51. COUNT(*)
  52. 984
  53. set @@optimizer_switch = 'in_to_exists=on,semijoin=on,materialization=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on';
  54. 1. Subquery in a disjunctive WHERE clause of the outer query.
  55. Q1.1m:
  56. MATERIALIZATION: there are too many rows in the outer query
  57. to be looked up in the inner table.
  58. EXPLAIN
  59. SELECT Name FROM Country
  60. WHERE (Code IN (select Country from City where City.Population > 100000) OR
  61. Name LIKE 'L%') AND
  62. surfacearea > 1000000;
  63. id select_type table type possible_keys key key_len ref rows Extra
  64. 1 PRIMARY Country ALL Name,SurfaceArea NULL NULL NULL 239 Using where
  65. 2 MATERIALIZED City ALL Population,Country NULL NULL NULL 4080 Using where
  66. SELECT Name FROM Country
  67. WHERE (Code IN (select Country from City where City.Population > 100000) OR
  68. Name LIKE 'L%') AND
  69. surfacearea > 1000000;
  70. Name
  71. Algeria
  72. Angola
  73. Argentina
  74. Australia
  75. Bolivia
  76. Brazil
  77. Egypt
  78. South Africa
  79. Ethiopia
  80. Indonesia
  81. India
  82. Iran
  83. Canada
  84. Kazakstan
  85. China
  86. Colombia
  87. Congo, The Democratic Republic of the
  88. Libyan Arab Jamahiriya
  89. Mali
  90. Mauritania
  91. Mexico
  92. Mongolia
  93. Niger
  94. Peru
  95. Saudi Arabia
  96. Sudan
  97. Chad
  98. Russian Federation
  99. United States
  100. Q1.1e:
  101. IN-EXISTS: the materialization cost is the same as above, but
  102. there are much fewer outer rows to be looked up, thus the
  103. materialization cost is too high to compensate for fast lookups.
  104. EXPLAIN
  105. SELECT Name FROM Country
  106. WHERE (Code IN (select Country from City where City.Population > 100000) OR
  107. Name LIKE 'L%') AND
  108. surfacearea > 10*1000000;
  109. id select_type table type possible_keys key key_len ref rows Extra
  110. 1 PRIMARY Country range Name,SurfaceArea SurfaceArea 4 NULL 5 Using index condition; Using where; Rowid-ordered scan
  111. 2 DEPENDENT SUBQUERY City index_subquery Population,Country Country 3 func 18 Using where
  112. SELECT Name FROM Country
  113. WHERE (Code IN (select Country from City where City.Population > 100000) OR
  114. Name LIKE 'L%') AND
  115. surfacearea > 10*1000000;
  116. Name
  117. Russian Federation
  118. Q1.2m:
  119. MATERIALIZATION: the IN predicate is pushed (attached) to the last table
  120. in the join order (Country, City), therefore there are too many row
  121. combinations to filter by re-executing the subquery for each combination.
  122. EXPLAIN
  123. SELECT *
  124. FROM Country, City
  125. WHERE City.Country = Country.Code AND
  126. Country.SurfaceArea < 3000 AND Country.SurfaceArea > 10 AND
  127. (City.Name IN
  128. (select Language from CountryLanguage where Percentage > 50) OR
  129. City.name LIKE '%Island%');
  130. id select_type table type possible_keys key key_len ref rows Extra
  131. 1 PRIMARY Country ALL PRIMARY,SurfaceArea NULL NULL NULL 239 Using where
  132. 1 PRIMARY City ref Country Country 3 world.Country.Code 18 Using where
  133. 2 MATERIALIZED CountryLanguage ALL Percentage,Language NULL NULL NULL 984 Using where
  134. SELECT *
  135. FROM Country, City
  136. WHERE City.Country = Country.Code AND
  137. Country.SurfaceArea < 3000 AND Country.SurfaceArea > 10 AND
  138. (City.Name IN
  139. (select Language from CountryLanguage where Percentage > 50) OR
  140. City.name LIKE '%Island%');
  141. Code Name SurfaceArea Population Capital ID Name Country population
  142. CCK Cocos (Keeling) Islands 14.00 600 2317 2317 West Island CCK 167
  143. Q1.2e:
  144. IN_EXISTS: join order is the same, but the left IN operand refers to
  145. only the first table in the join order (Country), so there are much
  146. fewer rows to filter by subquery re-execution.
  147. EXPLAIN
  148. SELECT *
  149. FROM Country, City
  150. WHERE City.Country = Country.Code AND
  151. Country.SurfaceArea < 3000 AND Country.SurfaceArea > 10 AND
  152. (Country.Name IN
  153. (select Language from CountryLanguage where Percentage > 50) OR
  154. Country.name LIKE '%Island%');
  155. id select_type table type possible_keys key key_len ref rows Extra
  156. 1 PRIMARY Country ALL PRIMARY,SurfaceArea NULL NULL NULL 239 Using where
  157. 1 PRIMARY City ref Country Country 3 world.Country.Code 18
  158. 2 DEPENDENT SUBQUERY CountryLanguage index_subquery Percentage,Language Language 30 func 2 Using where
  159. SELECT *
  160. FROM Country, City
  161. WHERE City.Country = Country.Code AND
  162. Country.SurfaceArea < 3000 AND Country.SurfaceArea > 10 AND
  163. (Country.Name IN
  164. (select Language from CountryLanguage where Percentage > 50) OR
  165. Country.name LIKE '%Island%');
  166. Code Name SurfaceArea Population Capital ID Name Country population
  167. VGB Virgin Islands, British 151.00 21000 537 537 Road Town VGB 8000
  168. CYM Cayman Islands 264.00 38000 553 553 George Town CYM 19600
  169. COK Cook Islands 236.00 20000 583 583 Avarua COK 11900
  170. FRO Faroe Islands 1399.00 43000 901 901 Tórshavn FRO 14542
  171. CXR Christmas Island 135.00 2500 1791 1791 Flying Fish Cove CXR 700
  172. KIR Kiribati 726.00 83000 2256 2255 Bikenibeu KIR 5055
  173. KIR Kiribati 726.00 83000 2256 2256 Bairiki KIR 2226
  174. CCK Cocos (Keeling) Islands 14.00 600 2317 2316 Bantam CCK 503
  175. CCK Cocos (Keeling) Islands 14.00 600 2317 2317 West Island CCK 167
  176. MHL Marshall Islands 181.00 64000 2507 2507 Dalap-Uliga-Darrit MHL 28000
  177. NRU Nauru 21.00 12000 2728 2727 Yangor NRU 4050
  178. NRU Nauru 21.00 12000 2728 2728 Yaren NRU 559
  179. NFK Norfolk Island 36.00 2000 2806 2806 Kingston NFK 800
  180. PLW Palau 459.00 19000 2881 2881 Koror PLW 12000
  181. MNP Northern Mariana Islands 464.00 78000 2913 2913 Garapan MNP 9200
  182. TCA Turks and Caicos Islands 430.00 17000 3423 3423 Cockburn Town TCA 4800
  183. TUV Tuvalu 26.00 12000 3424 3424 Funafuti TUV 4600
  184. VIR Virgin Islands, U.S. 347.00 93000 4067 4067 Charlotte Amalie VIR 13000
  185. Q1.3:
  186. For the same reasons as in Q2 IN-EXISTS and MATERIALIZATION chosen
  187. for each respective subquery.
  188. EXPLAIN
  189. SELECT City.Name, Country.Name
  190. FROM City,Country
  191. WHERE City.Country = Country.Code AND
  192. Country.SurfaceArea < 30000 AND Country.SurfaceArea > 10 AND
  193. ((Country.Code, Country.Name) IN
  194. (select Country, Language from CountryLanguage where Percentage > 50) AND
  195. Country.Population > 3000000
  196. OR
  197. (Country.Code, City.Name) IN
  198. (select Country, Language from CountryLanguage));
  199. id select_type table type possible_keys key key_len ref rows Extra
  200. 1 PRIMARY Country ALL PRIMARY,SurfaceArea NULL NULL NULL 239 Using where
  201. 1 PRIMARY City ref Country Country 3 world.Country.Code 18 Using where
  202. 3 MATERIALIZED CountryLanguage index PRIMARY,Language PRIMARY 33 NULL 984 Using index
  203. 2 DEPENDENT SUBQUERY CountryLanguage unique_subquery PRIMARY,Percentage,Language PRIMARY 33 func,func 1 Using where
  204. SELECT City.Name, Country.Name
  205. FROM City,Country
  206. WHERE City.Country = Country.Code AND
  207. Country.SurfaceArea < 30000 AND Country.SurfaceArea > 10 AND
  208. ((Country.Code, Country.Name) IN
  209. (select Country, Language from CountryLanguage where Percentage > 50) AND
  210. Country.Population > 3000000
  211. OR
  212. (Country.Code, City.Name) IN
  213. (select Country, Language from CountryLanguage));
  214. Name Name
  215. Kigali Rwanda
  216. 2. NOT IN subqueries
  217. Q2.1:
  218. Number of cities that are not capitals in countries with small population.
  219. MATERIALIZATION is 50 times faster because the cost of each subquery
  220. re-execution is much higher than the cost of index lookups into the
  221. materialized subquery.
  222. EXPLAIN
  223. select count(*) from City
  224. where City.id not in (select capital from Country
  225. where capital is not null and population < 100000);
  226. id select_type table type possible_keys key key_len ref rows Extra
  227. 1 PRIMARY City index NULL PRIMARY 4 NULL 4080 Using where; Using index
  228. 2 MATERIALIZED Country ALL NULL NULL NULL NULL 239 Using where
  229. Q2.2e:
  230. Countries that speak French, but do not speak English
  231. IN-EXISTS because the outer query filters many rows, thus
  232. there are few lookups to make.
  233. EXPLAIN
  234. SELECT Country.Name
  235. FROM Country, CountryLanguage
  236. WHERE Code NOT IN (SELECT Country FROM CountryLanguage WHERE Language = 'English')
  237. AND CountryLanguage.Language = 'French'
  238. AND Code = Country;
  239. id select_type table type possible_keys key key_len ref rows Extra
  240. 1 PRIMARY CountryLanguage ref PRIMARY,Language Language 30 const 20 Using index condition
  241. 1 PRIMARY Country eq_ref PRIMARY PRIMARY 3 world.CountryLanguage.Country 1 Using where
  242. 2 DEPENDENT SUBQUERY CountryLanguage unique_subquery PRIMARY,Language PRIMARY 33 func,const 1 Using index; Using where
  243. SELECT Country.Name
  244. FROM Country, CountryLanguage
  245. WHERE Code NOT IN (SELECT Country FROM CountryLanguage WHERE Language = 'English')
  246. AND CountryLanguage.Language = 'French'
  247. AND Code = Country;
  248. Name
  249. France
  250. Saint Pierre and Miquelon
  251. Belgium
  252. Burundi
  253. Guadeloupe
  254. Haiti
  255. Madagascar
  256. Martinique
  257. Mayotte
  258. French Polynesia
  259. Rwanda
  260. Sao Tome and Principe
  261. Switzerland
  262. New Caledonia
  263. Lebanon
  264. Mauritius
  265. Andorra
  266. Italy
  267. Luxembourg
  268. Q2.2m:
  269. Countries that speak French OR Spanish, but do not speak English
  270. MATERIALIZATION because the outer query filters less rows than Q5-a,
  271. so there are more lookups.
  272. EXPLAIN
  273. SELECT Country.Name
  274. FROM Country, CountryLanguage
  275. WHERE Code NOT IN (SELECT Country FROM CountryLanguage WHERE Language = 'English')
  276. AND (CountryLanguage.Language = 'French' OR CountryLanguage.Language = 'Spanish')
  277. AND Code = Country;
  278. id select_type table type possible_keys key key_len ref rows Extra
  279. 1 PRIMARY CountryLanguage range PRIMARY,Language Language 30 NULL 45 Using index condition; Using where; Rowid-ordered scan
  280. 1 PRIMARY Country eq_ref PRIMARY PRIMARY 3 world.CountryLanguage.Country 1 Using where
  281. 2 MATERIALIZED CountryLanguage ref PRIMARY,Language Language 30 const 47 Using index condition
  282. SELECT Country.Name
  283. FROM Country, CountryLanguage
  284. WHERE Code NOT IN (SELECT Country FROM CountryLanguage WHERE Language = 'English')
  285. AND (CountryLanguage.Language = 'French' OR CountryLanguage.Language = 'Spanish')
  286. AND Code = Country;
  287. Name
  288. Andorra
  289. Argentina
  290. Bolivia
  291. Chile
  292. Costa Rica
  293. Dominican Republic
  294. Ecuador
  295. El Salvador
  296. Spain
  297. Guatemala
  298. Honduras
  299. Colombia
  300. Cuba
  301. Mexico
  302. Nicaragua
  303. Panama
  304. Paraguay
  305. Peru
  306. France
  307. Saint Pierre and Miquelon
  308. Uruguay
  309. Venezuela
  310. Belgium
  311. Burundi
  312. Guadeloupe
  313. Haiti
  314. Madagascar
  315. Martinique
  316. Mayotte
  317. French Polynesia
  318. Rwanda
  319. Sao Tome and Principe
  320. Switzerland
  321. New Caledonia
  322. Lebanon
  323. Mauritius
  324. Andorra
  325. Italy
  326. Luxembourg
  327. France
  328. Sweden
  329. Q2.3e:
  330. Not a very meaningful query that tests NOT IN.
  331. IN-EXISTS because the outer query is cheap enough to reexecute many times.
  332. EXPLAIN
  333. select count(*)
  334. from CountryLanguage
  335. where (Language, Country) NOT IN
  336. (SELECT City.Name, Country.Code
  337. FROM City LEFT JOIN Country ON (Country = Code and City.Population < 10000))
  338. AND Language IN ('English','Spanish');
  339. id select_type table type possible_keys key key_len ref rows Extra
  340. 1 PRIMARY CountryLanguage range Language Language 30 NULL 72 Using index condition; Using where; Rowid-ordered scan
  341. 2 DEPENDENT SUBQUERY City ref CityName CityName 35 func 1 Using index condition; Using where
  342. 2 DEPENDENT SUBQUERY Country eq_ref PRIMARY PRIMARY 3 world.City.Country 1 Using where; Using index
  343. select count(*)
  344. from CountryLanguage
  345. where (Language, Country) NOT IN
  346. (SELECT City.Name, Country.Code
  347. FROM City LEFT JOIN Country ON (Country = Code and City.Population < 10000))
  348. AND Language IN ('English','Spanish');
  349. count(*)
  350. 88
  351. Q2.3m:
  352. MATERIALIZATION with the PARTIAL_MATCH_MERGE strategy, because the HAVING
  353. clause prevents the use of the index on City(Name), and in practice reduces
  354. radically the size of the temp table.
  355. EXPLAIN
  356. select count(*)
  357. from CountryLanguage
  358. where (Language, Country) NOT IN
  359. (SELECT City.Name, Country.Code
  360. FROM City LEFT JOIN Country ON (Country = Code)
  361. HAVING City.Name LIKE "Santa%");
  362. id select_type table type possible_keys key key_len ref rows Extra
  363. 1 PRIMARY CountryLanguage index NULL PRIMARY 33 NULL 984 Using where; Using index
  364. 2 MATERIALIZED City ALL NULL NULL NULL NULL 4080
  365. 2 MATERIALIZED Country eq_ref PRIMARY PRIMARY 3 world.City.Country 1 Using index
  366. select count(*)
  367. from CountryLanguage
  368. where (Language, Country) NOT IN
  369. (SELECT City.Name, Country.Code
  370. FROM City LEFT JOIN Country ON (Country = Code)
  371. HAVING City.Name LIKE "Santa%");
  372. count(*)
  373. 984
  374. 3. Subqueries with GROUP BY, HAVING, and aggregate functions
  375. Q3.1:
  376. Languages that are spoken in countries with 10 or 11 languages
  377. MATERIALIZATION is about 100 times faster than IN-EXISTS.
  378. EXPLAIN
  379. select count(*)
  380. from CountryLanguage
  381. where
  382. (Country, 10) IN (SELECT Code, COUNT(*) FROM CountryLanguage, Country
  383. WHERE Code = Country GROUP BY Code)
  384. OR
  385. (Country, 11) IN (SELECT Code, COUNT(*) FROM CountryLanguage, Country
  386. WHERE Code = Country GROUP BY Code)
  387. order by Country;
  388. id select_type table type possible_keys key key_len ref rows Extra
  389. 1 PRIMARY CountryLanguage index NULL PRIMARY 33 NULL 984 Using where; Using index
  390. 3 MATERIALIZED CountryLanguage index PRIMARY PRIMARY 33 NULL 984 Using index; Using temporary
  391. 3 MATERIALIZED Country eq_ref PRIMARY PRIMARY 3 world.CountryLanguage.Country 1 Using index
  392. 2 MATERIALIZED CountryLanguage index PRIMARY PRIMARY 33 NULL 984 Using index; Using temporary
  393. 2 MATERIALIZED Country eq_ref PRIMARY PRIMARY 3 world.CountryLanguage.Country 1 Using index
  394. select count(*)
  395. from CountryLanguage
  396. where
  397. (Country, 10) IN (SELECT Code, COUNT(*) FROM CountryLanguage, Country
  398. WHERE Code = Country GROUP BY Code)
  399. OR
  400. (Country, 11) IN (SELECT Code, COUNT(*) FROM CountryLanguage, Country
  401. WHERE Code = Country GROUP BY Code)
  402. order by Country;
  403. count(*)
  404. 102
  405. Q3.2:
  406. Countries whose capital is a city name that names more than one
  407. cities.
  408. MATERIALIZATION because the cost of single subquery execution is
  409. close to that of materializing the subquery.
  410. EXPLAIN
  411. select * from Country, City
  412. where capital = id and
  413. (City.name in (SELECT name FROM City
  414. GROUP BY name HAVING Count(*) > 2) OR
  415. capital is null);
  416. id select_type table type possible_keys key key_len ref rows Extra
  417. 1 PRIMARY Country ALL NULL NULL NULL NULL 239 Using where
  418. 1 PRIMARY City eq_ref PRIMARY PRIMARY 4 world.Country.Capital 1 Using where
  419. 2 MATERIALIZED City index NULL CityName 35 NULL 4080 Using index
  420. select * from Country, City
  421. where capital = id and
  422. (City.name in (SELECT name FROM City
  423. GROUP BY name HAVING Count(*) > 2) OR
  424. capital is null);
  425. Code Name SurfaceArea Population Capital ID Name Country population
  426. BMU Bermuda 53.00 65000 191 191 Hamilton BMU 1200
  427. BOL Bolivia 1098581.00 8329000 194 194 La Paz BOL 758141
  428. CRI Costa Rica 51100.00 4023000 584 584 San José CRI 339131
  429. HKG Hong Kong 1075.00 6782000 937 937 Victoria HKG 1312637
  430. SYC Seychelles 455.00 77000 3206 3206 Victoria SYC 41000
  431. Q3.3: MATERIALIZATION is 25 times faster than IN-EXISTS
  432. EXPLAIN
  433. SELECT Name
  434. FROM Country
  435. WHERE Country.Code NOT IN
  436. (SELECT Country FROM City GROUP BY Name HAVING COUNT(Name) = 1);
  437. id select_type table type possible_keys key key_len ref rows Extra
  438. 1 PRIMARY Country ALL NULL NULL NULL NULL 239 Using where
  439. 2 MATERIALIZED City ALL NULL NULL NULL NULL 4080 Using temporary
  440. SELECT Name
  441. FROM Country
  442. WHERE Country.Code NOT IN
  443. (SELECT Country FROM City GROUP BY Name HAVING COUNT(Name) = 1);
  444. Name
  445. Antigua and Barbuda
  446. Costa Rica
  447. Montserrat
  448. Norfolk Island
  449. Seychelles
  450. Antarctica
  451. Bouvet Island
  452. British Indian Ocean Territory
  453. South Georgia and the South Sandwich Islands
  454. Heard Island and McDonald Islands
  455. French Southern territories
  456. United States Minor Outlying Islands
  457. 4. Subqueries in the SELECT and HAVING clauses
  458. Q4.1m:
  459. Capital information about very big cities
  460. MATERIALIZATION
  461. EXPLAIN
  462. select Name, City.id in (select capital from Country where capital is not null) as is_capital
  463. from City
  464. where City.population > 10000000;
  465. id select_type table type possible_keys key key_len ref rows Extra
  466. 1 PRIMARY City range Population Population 5 NULL 4 Using index condition; Rowid-ordered scan
  467. 2 MATERIALIZED Country ALL NULL NULL NULL NULL 239 Using where
  468. select Name, City.id in (select capital from Country where capital is not null) as is_capital
  469. from City
  470. where City.population > 10000000;
  471. Name is_capital
  472. Mumbai (Bombay) 0
  473. Q4.1e:
  474. IN-TO-EXISTS after adding an index to make the subquery re-execution
  475. efficient.
  476. create index CountryCapital on Country(capital);
  477. EXPLAIN
  478. select Name, City.id in (select capital from Country where capital is not null) as is_capital
  479. from City
  480. where City.population > 10000000;
  481. id select_type table type possible_keys key key_len ref rows Extra
  482. 1 PRIMARY City range Population Population 5 NULL 4 Using index condition; Rowid-ordered scan
  483. 2 DEPENDENT SUBQUERY Country index_subquery CountryCapital CountryCapital 5 func 2 Using index; Using where
  484. select Name, City.id in (select capital from Country where capital is not null) as is_capital
  485. from City
  486. where City.population > 10000000;
  487. Name is_capital
  488. Mumbai (Bombay) 0
  489. drop index CountryCapital on Country;
  490. Q4.2:
  491. MATERIALIZATION
  492. EXPLAIN
  493. SELECT City.Name, City.Population
  494. FROM City JOIN Country ON City.Country = Country.Code
  495. GROUP BY City.Name
  496. HAVING City.Name IN (select Name from Country where population < 1000000);
  497. id select_type table type possible_keys key key_len ref rows Extra
  498. 1 PRIMARY Country index PRIMARY PRIMARY 3 NULL 239 Using index; Using temporary; Using filesort
  499. 1 PRIMARY City ref Country Country 3 world.Country.Code 18
  500. 2 MATERIALIZED Country ALL Name NULL NULL NULL 239 Using where
  501. SELECT City.Name, City.Population
  502. FROM City JOIN Country ON City.Country = Country.Code
  503. GROUP BY City.Name
  504. HAVING City.Name IN (select Name from Country where population < 1000000);
  505. Name Population
  506. Djibouti 383000
  507. Gibraltar 27025
  508. Macao 437500
  509. San Marino 2294
  510. 5. Subqueries with UNION
  511. Q5.1:
  512. EXPLAIN
  513. SELECT * from City where (Name, 91) in
  514. (SELECT Name, round(Population/1000)
  515. FROM City
  516. WHERE Country = "IND" AND Population > 2500000
  517. UNION
  518. SELECT Name, round(Population/1000)
  519. FROM City
  520. WHERE Country = "IND" AND Population < 100000);
  521. id select_type table type possible_keys key key_len ref rows Extra
  522. 1 PRIMARY City ALL NULL NULL NULL NULL 4080 Using where
  523. 2 DEPENDENT SUBQUERY City ref Population,Country,CityName CityName 35 func 1 Using where
  524. 3 DEPENDENT UNION City ref Population,Country,CityName CityName 35 func 1 Using where
  525. NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
  526. SELECT * from City where (Name, 91) in
  527. (SELECT Name, round(Population/1000)
  528. FROM City
  529. WHERE Country = "IND" AND Population > 2500000
  530. UNION
  531. SELECT Name, round(Population/1000)
  532. FROM City
  533. WHERE Country = "IND" AND Population < 100000);
  534. ID Name Country population
  535. 1359 Hassan IND 90803
  536. 1360 Ambala Sadar IND 90712
  537. 1361 Baidyabati IND 90601
  538. set @@optimizer_switch='default';
  539. drop database world;
  540. TEST GROUP 2:
  541. Tests of various combinations of optimizer switches, types of queries,
  542. available indexes, column nullability, constness of tables/predicates.
  543. =====================================================================
  544. set optimizer_switch=@subselect_mat_cost;