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.

754 lines
27 KiB

  1. set @tmp_opt_switch= @@optimizer_switch;
  2. set optimizer_switch='index_merge_sort_intersection=on';
  3. set optimizer_trace='enabled=on';
  4. create table t0 (a int);
  5. insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
  6. create table t1 (a int, b int, c int, filler char(100),
  7. key(a), key(b), key(c));
  8. insert into t1 select
  9. A.a * B.a*10 + C.a*100,
  10. A.a * B.a*10 + C.a*100,
  11. A.a,
  12. 'filler'
  13. from t0 A, t0 B, t0 C;
  14. This should use union:
  15. explain select * from t1 where a=1 or b=1;
  16. id select_type table type possible_keys key key_len ref rows Extra
  17. 1 SIMPLE t1 index_merge a,b a,b 5,5 NULL 2 Using union(a,b); Using where
  18. select * from information_schema.OPTIMIZER_TRACE;
  19. QUERY TRACE MISSING_BYTES_BEYOND_MAX_MEM_SIZE INSUFFICIENT_PRIVILEGES
  20. explain select * from t1 where a=1 or b=1 {
  21. "steps": [
  22. {
  23. "join_preparation": {
  24. "select_id": 1,
  25. "steps": [
  26. {
  27. "expanded_query": "select t1.a AS a,t1.b AS b,t1.c AS c,t1.filler AS filler from t1 where t1.a = 1 or t1.b = 1"
  28. }
  29. ]
  30. }
  31. },
  32. {
  33. "join_optimization": {
  34. "select_id": 1,
  35. "steps": [
  36. {
  37. "condition_processing": {
  38. "condition": "WHERE",
  39. "original_condition": "t1.a = 1 or t1.b = 1",
  40. "steps": [
  41. {
  42. "transformation": "equality_propagation",
  43. "resulting_condition": "multiple equal(1, t1.a) or multiple equal(1, t1.b)"
  44. },
  45. {
  46. "transformation": "constant_propagation",
  47. "resulting_condition": "multiple equal(1, t1.a) or multiple equal(1, t1.b)"
  48. },
  49. {
  50. "transformation": "trivial_condition_removal",
  51. "resulting_condition": "multiple equal(1, t1.a) or multiple equal(1, t1.b)"
  52. }
  53. ]
  54. }
  55. },
  56. {
  57. "table_dependencies": [
  58. {
  59. "table": "t1",
  60. "row_may_be_null": false,
  61. "map_bit": 0,
  62. "depends_on_map_bits": []
  63. }
  64. ]
  65. },
  66. {
  67. "ref_optimizer_key_uses": []
  68. },
  69. {
  70. "rows_estimation": [
  71. {
  72. "table": "t1",
  73. "range_analysis": {
  74. "table_scan": {
  75. "rows": 1000,
  76. "cost": 231.5878906
  77. },
  78. "potential_range_indexes": [
  79. {
  80. "index": "a",
  81. "usable": true,
  82. "key_parts": ["a"]
  83. },
  84. {
  85. "index": "b",
  86. "usable": true,
  87. "key_parts": ["b"]
  88. },
  89. {
  90. "index": "c",
  91. "usable": false,
  92. "cause": "not applicable"
  93. }
  94. ],
  95. "setup_range_conditions": [],
  96. "analyzing_range_alternatives": {
  97. "range_scan_alternatives": [],
  98. "analyzing_roworder_intersect": {
  99. "cause": "too few roworder scans"
  100. },
  101. "analyzing_sort_intersect": {},
  102. "analyzing_index_merge_union": [
  103. {
  104. "indexes_to_merge": [
  105. {
  106. "range_scan_alternatives": [
  107. {
  108. "index": "a",
  109. "ranges": ["(1) <= (a) <= (1)"],
  110. "rowid_ordered": true,
  111. "using_mrr": false,
  112. "index_only": true,
  113. "rows": 1,
  114. "cost": 0.345585794,
  115. "chosen": true
  116. }
  117. ],
  118. "index_to_merge": "a",
  119. "cumulated_cost": 0.345585794
  120. },
  121. {
  122. "range_scan_alternatives": [
  123. {
  124. "index": "b",
  125. "ranges": ["(1) <= (b) <= (1)"],
  126. "rowid_ordered": true,
  127. "using_mrr": false,
  128. "index_only": true,
  129. "rows": 1,
  130. "cost": 0.345585794,
  131. "chosen": true
  132. }
  133. ],
  134. "index_to_merge": "b",
  135. "cumulated_cost": 0.691171589
  136. }
  137. ],
  138. "cost_of_reading_ranges": 0.691171589,
  139. "use_roworder_union": true,
  140. "cause": "always cheaper than non roworder retrieval",
  141. "analyzing_roworder_scans": [
  142. {
  143. "type": "range_scan",
  144. "index": "a",
  145. "rows": 1,
  146. "ranges": ["(1) <= (a) <= (1)"],
  147. "analyzing_roworder_intersect": {
  148. "cause": "too few roworder scans"
  149. }
  150. },
  151. {
  152. "type": "range_scan",
  153. "index": "b",
  154. "rows": 1,
  155. "ranges": ["(1) <= (b) <= (1)"],
  156. "analyzing_roworder_intersect": {
  157. "cause": "too few roworder scans"
  158. }
  159. }
  160. ],
  161. "index_roworder_union_cost": 2.484903732,
  162. "members": 2,
  163. "chosen": true
  164. }
  165. ]
  166. },
  167. "group_index_range": {
  168. "chosen": false,
  169. "cause": "no group by or distinct"
  170. },
  171. "chosen_range_access_summary": {
  172. "range_access_plan": {
  173. "type": "index_roworder_union",
  174. "union_of": [
  175. {
  176. "type": "range_scan",
  177. "index": "a",
  178. "rows": 1,
  179. "ranges": ["(1) <= (a) <= (1)"]
  180. },
  181. {
  182. "type": "range_scan",
  183. "index": "b",
  184. "rows": 1,
  185. "ranges": ["(1) <= (b) <= (1)"]
  186. }
  187. ]
  188. },
  189. "rows_for_plan": 2,
  190. "cost_for_plan": 2.484903732,
  191. "chosen": true
  192. }
  193. }
  194. },
  195. {
  196. "selectivity_for_indexes": [],
  197. "selectivity_for_columns": [],
  198. "cond_selectivity": 0.002
  199. }
  200. ]
  201. },
  202. {
  203. "considered_execution_plans": [
  204. {
  205. "plan_prefix": [],
  206. "table": "t1",
  207. "best_access_path": {
  208. "considered_access_paths": [
  209. {
  210. "access_type": "index_merge",
  211. "resulting_rows": 2,
  212. "cost": 2.484903732,
  213. "chosen": true
  214. }
  215. ],
  216. "chosen_access_method": {
  217. "type": "index_merge",
  218. "records": 2,
  219. "cost": 2.484903732,
  220. "uses_join_buffering": false
  221. }
  222. },
  223. "rows_for_plan": 2,
  224. "cost_for_plan": 2.884903732,
  225. "estimated_join_cardinality": 2
  226. }
  227. ]
  228. },
  229. {
  230. "best_join_order": ["t1"]
  231. },
  232. {
  233. "attaching_conditions_to_tables": {
  234. "original_condition": "t1.a = 1 or t1.b = 1",
  235. "attached_conditions_computation": [],
  236. "attached_conditions_summary": [
  237. {
  238. "table": "t1",
  239. "attached": "t1.a = 1 or t1.b = 1"
  240. }
  241. ]
  242. }
  243. }
  244. ]
  245. }
  246. },
  247. {
  248. "join_execution": {
  249. "select_id": 1,
  250. "steps": []
  251. }
  252. }
  253. ]
  254. } 0 0
  255. drop table t0,t1;
  256. set optimizer_trace="enabled=off";
  257. set @@optimizer_switch= @tmp_opt_switch;
  258. # More tests added index_merge access
  259. create table t1
  260. (
  261. /* Field names reflect value(rowid) distribution, st=STairs, swt= SaWTooth */
  262. st_a int not null default 0,
  263. swt1a int not null default 0,
  264. swt2a int not null default 0,
  265. st_b int not null default 0,
  266. swt1b int not null default 0,
  267. swt2b int not null default 0,
  268. /* fields/keys for row retrieval tests */
  269. key1 int,
  270. key2 int,
  271. key3 int,
  272. key4 int,
  273. /* make rows much bigger then keys */
  274. filler1 char (200),
  275. filler2 char (200),
  276. filler3 char (200),
  277. filler4 char (200),
  278. filler5 char (200),
  279. filler6 char (200),
  280. /* order of keys is important */
  281. key sta_swt12a(st_a,swt1a,swt2a),
  282. key sta_swt1a(st_a,swt1a),
  283. key sta_swt2a(st_a,swt2a),
  284. key sta_swt21a(st_a,swt2a,swt1a),
  285. key st_a(st_a),
  286. key stb_swt1a_2b(st_b,swt1b,swt2a),
  287. key stb_swt1b(st_b,swt1b),
  288. key st_b(st_b),
  289. key(key1),
  290. key(key2),
  291. key(key3),
  292. key(key4)
  293. ) ;
  294. create table t0 as select * from t1;
  295. # Printing of many insert into t0 values (....) disabled.
  296. alter table t1 disable keys;
  297. # Printing of many insert into t1 select .... from t0 disabled.
  298. # Printing of many insert into t1 (...) values (....) disabled.
  299. alter table t1 enable keys;
  300. insert into t1 (key1, key2, key3, key4, filler1) values (100, 100, -1, -1, 'key1-key2');
  301. insert into t1 (key1, key2, key3, key4, filler1) values (-1, -1, 100, 100, 'key4-key3');
  302. set optimizer_trace='enabled=on';
  303. # 3-way ROR-intersection
  304. explain select key1,key2,key3 from t1 where key1=100 and key2=100 and key3=100;
  305. id select_type table type possible_keys key key_len ref rows Extra
  306. 1 SIMPLE t1 index_merge key1,key2,key3 key1,key2,key3 5,5,5 NULL 2 Using intersect(key1,key2,key3); Using where; Using index
  307. select JSON_DETAILED(JSON_EXTRACT(trace, '$**.analyzing_range_alternatives')) from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
  308. JSON_DETAILED(JSON_EXTRACT(trace, '$**.analyzing_range_alternatives'))
  309. [
  310. {
  311. "range_scan_alternatives":
  312. [
  313. {
  314. "index": "key1",
  315. "ranges":
  316. [
  317. "(100) <= (key1) <= (100)"
  318. ],
  319. "rowid_ordered": true,
  320. "using_mrr": false,
  321. "index_only": false,
  322. "rows": 2243,
  323. "cost": 2700.058937,
  324. "chosen": true
  325. },
  326. {
  327. "index": "key2",
  328. "ranges":
  329. [
  330. "(100) <= (key2) <= (100)"
  331. ],
  332. "rowid_ordered": true,
  333. "using_mrr": false,
  334. "index_only": false,
  335. "rows": 2243,
  336. "cost": 2700.058937,
  337. "chosen": false,
  338. "cause": "cost"
  339. },
  340. {
  341. "index": "key3",
  342. "ranges":
  343. [
  344. "(100) <= (key3) <= (100)"
  345. ],
  346. "rowid_ordered": true,
  347. "using_mrr": false,
  348. "index_only": false,
  349. "rows": 2243,
  350. "cost": 2700.058937,
  351. "chosen": false,
  352. "cause": "cost"
  353. }
  354. ],
  355. "analyzing_roworder_intersect":
  356. {
  357. "intersecting_indexes":
  358. [
  359. {
  360. "index": "key1",
  361. "index_scan_cost": 10.31393703,
  362. "cumulated_index_scan_cost": 10.31393703,
  363. "disk_sweep_cost": 1923.144061,
  364. "cumulative_total_cost": 1933.457998,
  365. "usable": true,
  366. "matching_rows_now": 2243,
  367. "intersect_covering_with_this_index": false,
  368. "chosen": true
  369. },
  370. {
  371. "index": "key2",
  372. "index_scan_cost": 10.31393703,
  373. "cumulated_index_scan_cost": 20.62787405,
  374. "disk_sweep_cost": 84.51771758,
  375. "cumulative_total_cost": 105.1455916,
  376. "usable": true,
  377. "matching_rows_now": 77.6360508,
  378. "intersect_covering_with_this_index": false,
  379. "chosen": true
  380. },
  381. {
  382. "index": "key3",
  383. "index_scan_cost": 10.31393703,
  384. "cumulated_index_scan_cost": 30.94181108,
  385. "disk_sweep_cost": 0,
  386. "cumulative_total_cost": 30.94181108,
  387. "usable": true,
  388. "matching_rows_now": 2.687185191,
  389. "intersect_covering_with_this_index": true,
  390. "chosen": true
  391. }
  392. ],
  393. "clustered_pk":
  394. {
  395. "clustered_pk_added_to_intersect": false,
  396. "cause": "no clustered pk index"
  397. },
  398. "rows": 2,
  399. "cost": 30.94181108,
  400. "covering": true,
  401. "chosen": true
  402. },
  403. "analyzing_index_merge_union":
  404. [
  405. ]
  406. }
  407. ]
  408. select JSON_DETAILED(JSON_EXTRACT(trace, '$**.chosen_range_access_summary')) from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
  409. JSON_DETAILED(JSON_EXTRACT(trace, '$**.chosen_range_access_summary'))
  410. [
  411. {
  412. "range_access_plan":
  413. {
  414. "type": "index_roworder_intersect",
  415. "rows": 2,
  416. "cost": 30.94181108,
  417. "covering": true,
  418. "clustered_pk_scan": false,
  419. "intersect_of":
  420. [
  421. {
  422. "type": "range_scan",
  423. "index": "key1",
  424. "rows": 2243,
  425. "ranges":
  426. [
  427. "(100) <= (key1) <= (100)"
  428. ]
  429. },
  430. {
  431. "type": "range_scan",
  432. "index": "key2",
  433. "rows": 2243,
  434. "ranges":
  435. [
  436. "(100) <= (key2) <= (100)"
  437. ]
  438. },
  439. {
  440. "type": "range_scan",
  441. "index": "key3",
  442. "rows": 2243,
  443. "ranges":
  444. [
  445. "(100) <= (key3) <= (100)"
  446. ]
  447. }
  448. ]
  449. },
  450. "rows_for_plan": 2,
  451. "cost_for_plan": 30.94181108,
  452. "chosen": true
  453. }
  454. ]
  455. # ROR-union(ROR-intersection, ROR-range)
  456. explain select key1,key2,key3,key4 from t1 where key1=100 and key2=100 or key3=100 and key4=100;
  457. id select_type table type possible_keys key key_len ref rows Extra
  458. 1 SIMPLE t1 index_merge key1,key2,key3,key4 key1,key2,key3,key4 5,5,5,5 NULL 154 Using union(intersect(key1,key2),intersect(key3,key4)); Using where
  459. select JSON_DETAILED(JSON_EXTRACT(trace, '$**.analyzing_range_alternatives')) from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
  460. JSON_DETAILED(JSON_EXTRACT(trace, '$**.analyzing_range_alternatives'))
  461. [
  462. {
  463. "range_scan_alternatives":
  464. [
  465. ],
  466. "analyzing_roworder_intersect":
  467. {
  468. "cause": "too few roworder scans"
  469. },
  470. "analyzing_index_merge_union":
  471. [
  472. {
  473. "indexes_to_merge":
  474. [
  475. {
  476. "range_scan_alternatives":
  477. [
  478. {
  479. "index": "key1",
  480. "ranges":
  481. [
  482. "(100) <= (key1) <= (100)"
  483. ],
  484. "rowid_ordered": true,
  485. "using_mrr": false,
  486. "index_only": true,
  487. "rows": 2243,
  488. "cost": 457.058937,
  489. "chosen": true
  490. },
  491. {
  492. "index": "key2",
  493. "ranges":
  494. [
  495. "(100) <= (key2) <= (100)"
  496. ],
  497. "rowid_ordered": true,
  498. "using_mrr": false,
  499. "index_only": true,
  500. "rows": 2243,
  501. "cost": 457.058937,
  502. "chosen": false,
  503. "cause": "cost"
  504. }
  505. ],
  506. "index_to_merge": "key1",
  507. "cumulated_cost": 457.058937
  508. },
  509. {
  510. "range_scan_alternatives":
  511. [
  512. {
  513. "index": "key3",
  514. "ranges":
  515. [
  516. "(100) <= (key3) <= (100)"
  517. ],
  518. "rowid_ordered": true,
  519. "using_mrr": false,
  520. "index_only": true,
  521. "rows": 2243,
  522. "cost": 457.058937,
  523. "chosen": true
  524. },
  525. {
  526. "index": "key4",
  527. "ranges":
  528. [
  529. "(100) <= (key4) <= (100)"
  530. ],
  531. "rowid_ordered": true,
  532. "using_mrr": false,
  533. "index_only": true,
  534. "rows": 2243,
  535. "cost": 457.058937,
  536. "chosen": false,
  537. "cause": "cost"
  538. }
  539. ],
  540. "index_to_merge": "key3",
  541. "cumulated_cost": 914.1178741
  542. }
  543. ],
  544. "cost_of_reading_ranges": 914.1178741,
  545. "use_roworder_union": true,
  546. "cause": "always cheaper than non roworder retrieval",
  547. "analyzing_roworder_scans":
  548. [
  549. {
  550. "type": "range_scan",
  551. "index": "key1",
  552. "rows": 2243,
  553. "ranges":
  554. [
  555. "(100) <= (key1) <= (100)"
  556. ],
  557. "analyzing_roworder_intersect":
  558. {
  559. "intersecting_indexes":
  560. [
  561. {
  562. "index": "key1",
  563. "index_scan_cost": 10.31393703,
  564. "cumulated_index_scan_cost": 10.31393703,
  565. "disk_sweep_cost": 1923.144061,
  566. "cumulative_total_cost": 1933.457998,
  567. "usable": true,
  568. "matching_rows_now": 2243,
  569. "intersect_covering_with_this_index": false,
  570. "chosen": true
  571. },
  572. {
  573. "index": "key2",
  574. "index_scan_cost": 10.31393703,
  575. "cumulated_index_scan_cost": 20.62787405,
  576. "disk_sweep_cost": 84.51771758,
  577. "cumulative_total_cost": 105.1455916,
  578. "usable": true,
  579. "matching_rows_now": 77.6360508,
  580. "intersect_covering_with_this_index": false,
  581. "chosen": true
  582. }
  583. ],
  584. "clustered_pk":
  585. {
  586. "clustered_pk_added_to_intersect": false,
  587. "cause": "no clustered pk index"
  588. },
  589. "rows": 77,
  590. "cost": 105.1455916,
  591. "covering": false,
  592. "chosen": true
  593. }
  594. },
  595. {
  596. "type": "range_scan",
  597. "index": "key3",
  598. "rows": 2243,
  599. "ranges":
  600. [
  601. "(100) <= (key3) <= (100)"
  602. ],
  603. "analyzing_roworder_intersect":
  604. {
  605. "intersecting_indexes":
  606. [
  607. {
  608. "index": "key3",
  609. "index_scan_cost": 10.31393703,
  610. "cumulated_index_scan_cost": 10.31393703,
  611. "disk_sweep_cost": 1923.144061,
  612. "cumulative_total_cost": 1933.457998,
  613. "usable": true,
  614. "matching_rows_now": 2243,
  615. "intersect_covering_with_this_index": false,
  616. "chosen": true
  617. },
  618. {
  619. "index": "key4",
  620. "index_scan_cost": 10.31393703,
  621. "cumulated_index_scan_cost": 20.62787405,
  622. "disk_sweep_cost": 84.51771758,
  623. "cumulative_total_cost": 105.1455916,
  624. "usable": true,
  625. "matching_rows_now": 77.6360508,
  626. "intersect_covering_with_this_index": false,
  627. "chosen": true
  628. }
  629. ],
  630. "clustered_pk":
  631. {
  632. "clustered_pk_added_to_intersect": false,
  633. "cause": "no clustered pk index"
  634. },
  635. "rows": 77,
  636. "cost": 105.1455916,
  637. "covering": false,
  638. "chosen": true
  639. }
  640. }
  641. ],
  642. "index_roworder_union_cost": 194.9771115,
  643. "members": 2,
  644. "chosen": true
  645. }
  646. ]
  647. }
  648. ]
  649. select JSON_DETAILED(JSON_EXTRACT(trace, '$**.chosen_range_access_summary')) from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
  650. JSON_DETAILED(JSON_EXTRACT(trace, '$**.chosen_range_access_summary'))
  651. [
  652. {
  653. "range_access_plan":
  654. {
  655. "type": "index_roworder_union",
  656. "union_of":
  657. [
  658. {
  659. "type": "index_roworder_intersect",
  660. "rows": 77,
  661. "cost": 105.1455916,
  662. "covering": false,
  663. "clustered_pk_scan": false,
  664. "intersect_of":
  665. [
  666. {
  667. "type": "range_scan",
  668. "index": "key1",
  669. "rows": 2243,
  670. "ranges":
  671. [
  672. "(100) <= (key1) <= (100)"
  673. ]
  674. },
  675. {
  676. "type": "range_scan",
  677. "index": "key2",
  678. "rows": 2243,
  679. "ranges":
  680. [
  681. "(100) <= (key2) <= (100)"
  682. ]
  683. }
  684. ]
  685. },
  686. {
  687. "type": "index_roworder_intersect",
  688. "rows": 77,
  689. "cost": 105.1455916,
  690. "covering": false,
  691. "clustered_pk_scan": false,
  692. "intersect_of":
  693. [
  694. {
  695. "type": "range_scan",
  696. "index": "key3",
  697. "rows": 2243,
  698. "ranges":
  699. [
  700. "(100) <= (key3) <= (100)"
  701. ]
  702. },
  703. {
  704. "type": "range_scan",
  705. "index": "key4",
  706. "rows": 2243,
  707. "ranges":
  708. [
  709. "(100) <= (key4) <= (100)"
  710. ]
  711. }
  712. ]
  713. }
  714. ]
  715. },
  716. "rows_for_plan": 154,
  717. "cost_for_plan": 194.9771115,
  718. "chosen": true
  719. }
  720. ]
  721. drop table t0,t1;
  722. set optimizer_trace="enabled=off";