From 5aba88bb80857f6e08f5042b499f56e6b812088f Mon Sep 17 00:00:00 2001 From: Jinghui Hu Date: Thu, 4 Jul 2024 14:13:05 +0800 Subject: [PATCH] add exp_json and opt_trace for queries --- assets/subquery-examples.org | 136 +++++++++++++++++++++++++++++++++-- 1 file changed, 130 insertions(+), 6 deletions(-) diff --git a/assets/subquery-examples.org b/assets/subquery-examples.org index 011205e..47cb024 100644 --- a/assets/subquery-examples.org +++ b/assets/subquery-examples.org @@ -109,7 +109,7 @@ * subq10 ** subq10 - Default -- EXISTS 子查询 [[file:subq04_exists_exp.sql.json][exp_json]] | [[file:subq04_exists_opt.sql.json][opt_trace]] +- 默认 [[file:subq10_sj_default_exp.sql.json][exp_json]] | [[file:subq10_sj_default_opt.sql.json][opt_trace]] #+BEGIN_SRC sql explain select @@ -134,6 +134,7 @@ | 2 | MATERIALIZED | a | NULL | ALL | dept_no | NULL | NULL | NULL | 24 | 33.33 | Using where | ** subq10 - DuplicateWeedout +- [[file:subq11_sj_duplicate-weedout_exp.sql.json][exp_json]] | [[file:subq11_sj_duplicate-weedout_opt.sql.json][opt_trace]] #+BEGIN_SRC sql explain @@ -159,7 +160,7 @@ | 1 | SIMPLE | d | NULL | eq_ref | PRIMARY | PRIMARY | 16 | employees.a.dept_no | 1 | 100.00 | End temporary | ** subq10 - FirstMatch -- xx +- [[file:subq12_sj_first-match_exp.sql.json][exp_json]] | [[file:subq12_sj_first-match_opt.sql.json][opt_trace]] #+BEGIN_SRC sql explain @@ -179,7 +180,7 @@ #+END_SRC ** subq10 - LooseScan -- xx +- [[file:subq13_sj_loose-scan_exp.sql.json][exp_json]] | [[file:subq13_sj_loose-scan_opt.sql.json][opt_trace]] #+BEGIN_SRC sql explain @@ -205,7 +206,7 @@ | 1 | SIMPLE | d | NULL | eq_ref | PRIMARY | PRIMARY | 16 | employees.a.dept_no | 1 | 100.00 | End temporary | ** subq10 - Materialize -- xx +- [[file:subq14_sj_materialization_exp.sql.json][exp_json]] | [[file:subq14_sj_materialization_opt.sql.json][opt_trace]] #+BEGIN_SRC sql explain @@ -231,5 +232,128 @@ | 1 | SIMPLE | | NULL | eq_ref | | | 16 | employees.d.dept_no | 1 | 100.00 | NULL | | 2 | MATERIALIZED | a | NULL | ALL | dept_no | NULL | NULL | NULL | 24 | 33.33 | Using where | -* subq14 -- MaterializeScan 参考 | [[*subq03][subq03]] +* subq20 +** subq20 - Default +- [[file:subq20_sj_default_exp.sql.json][exp_json]] | [[file:subq20_sj_default_opt.sql.json][opt_trace]] + #+BEGIN_SRC sql + explain + select + ,* + from + employees e + where + e.emp_no in ( + select + a.emp_no + from + dept_manager a + where + a.dept_no >= 'd003'); + #+END_SRC + + #+RESULTS: + | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | + |----+-------------+-------+------------+--------+-----------------+---------+---------+--------------------+------+----------+-------------------------------------| + | 1 | SIMPLE | a | NULL | index | PRIMARY,dept_no | PRIMARY | 20 | NULL | 24 | 100.00 | Using where; Using index; LooseScan | + | 1 | SIMPLE | e | NULL | eq_ref | PRIMARY | PRIMARY | 4 | employees.a.emp_no | 1 | 100.00 | NULL | + +** subq20 - DuplicateWeedout +- [[file:subq21_sj_duplicate-weedout_exp.sql.json][exp_json]] | [[file:subq21_sj_duplicate-weedout_opt.sql.json][opt_trace]] + #+BEGIN_SRC sql + explain + select + ,* + from + employees e + where + e.emp_no in ( + select + /*+ SEMIJOIN(DUPSWEEDOUT) */ + a.emp_no + from + dept_manager a + where + a.dept_no >= 'd003'); + #+END_SRC + + #+RESULTS: + | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | + |----+-------------+-------+------------+--------+-----------------+---------+---------+--------------------+------+----------+-------------------------------------------| + | 1 | SIMPLE | a | NULL | range | PRIMARY,dept_no | dept_no | 16 | NULL | 20 | 100.00 | Using where; Using index; Start temporary | + | 1 | SIMPLE | e | NULL | eq_ref | PRIMARY | PRIMARY | 4 | employees.a.emp_no | 1 | 100.00 | End temporary | + +** subq20 - FirstMatch +- [[file:subq22_sj_first-match_exp.sql.json][exp_json]] + #+BEGIN_SRC sql + explain + select + ,* + from + employees e + where + e.emp_no in ( + select + /*+ SEMIJOIN(FIRSTMATCH) */ + a.emp_no + from + dept_manager a + where + a.dept_no >= 'd003'); + #+END_SRC + + #+RESULTS: + | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | + |----+-------------+-------+------------+------+-----------------+---------+---------+--------------------+--------+----------+-----------------------------------------| + | 1 | SIMPLE | e | NULL | ALL | PRIMARY | NULL | NULL | NULL | 276023 | 100.00 | NULL | + | 1 | SIMPLE | a | NULL | ref | PRIMARY,dept_no | PRIMARY | 4 | employees.e.emp_no | 1 | 83.33 | Using where; Using index; FirstMatch(e) | + +** subq20 - LooseScan +- [[file:subq23_sj_loose-scan_exp.sql.json][exp_json]] | [[file:subq23_sj_loose-scan_opt.sql.json][opt_trace]] + #+BEGIN_SRC sql + explain + select + ,* + from + employees e + where + e.emp_no in ( + select + /*+ SEMIJOIN(LOOSESCAN) */ + a.emp_no + from + dept_manager a + where + a.dept_no >= 'd003'); + #+END_SRC + + #+RESULTS: + | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | + |----+-------------+-------+------------+--------+-----------------+---------+---------+--------------------+------+----------+-------------------------------------| + | 1 | SIMPLE | a | NULL | index | PRIMARY,dept_no | PRIMARY | 20 | NULL | 24 | 100.00 | Using where; Using index; LooseScan | + | 1 | SIMPLE | e | NULL | eq_ref | PRIMARY | PRIMARY | 4 | employees.a.emp_no | 1 | 100.00 | NULL | + +** subq20 - Materialize +- [[file:subq24_sj_materialization_exp.sql.json][exp_json]] | [[file:subq24_sj_materialization_opt.sql.json][opt_trace]] + #+BEGIN_SRC sql + explain + select + ,* + from + employees e + where + e.emp_no in ( + select + /*+ SEMIJOIN(MATERIALIZATION) */ + a.emp_no + from + dept_manager a + where + a.dept_no >= 'd003'); + #+END_SRC + + #+RESULTS: + | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | + |----+--------------+-------------+------------+--------+-----------------+---------+---------+--------------------+------+----------+--------------------------| + | 1 | SIMPLE | | NULL | ALL | NULL | NULL | NULL | NULL | NULL | 100.00 | NULL | + | 1 | SIMPLE | e | NULL | eq_ref | PRIMARY | PRIMARY | 4 | .emp_no | 1 | 100.00 | NULL | + | 2 | MATERIALIZED | a | NULL | range | PRIMARY,dept_no | dept_no | 16 | NULL | 20 | 100.00 | Using where; Using index |