diff options
author | Monty <monty@mariadb.org> | 2022-05-04 17:26:43 +0300 |
---|---|---|
committer | Sergei Petrunia <sergey@mariadb.com> | 2023-02-02 22:32:57 +0300 |
commit | 5e5a8eda1641eda1d915a7eb5736e494d2179795 (patch) | |
tree | 0a3dbb394cbe52c6889815c8882dc58fbb0ca4c7 /sql/sql_derived.cc | |
parent | 868d577cb66f0771b2b42f0d5d948fcfafbfa872 (diff) | |
download | mariadb-git-5e5a8eda1641eda1d915a7eb5736e494d2179795.tar.gz |
Derived tables and union can now create distinct keys
The idea is that instead of marking all select_lex's with DISTINCT, we
only mark those that really need distinct result.
Benefits of this change:
- Temporary tables used with derived tables, UNION, IN are now smaller
as duplicates are removed already on the insert phase.
- The optimizer can now produce better plans with EQ_REF. This can be
seen from the tests where several queries does not anymore materialize
derived tables twice.
- Queries affected by 'in_predicate_conversion_threshold' where large IN
lists are converted to sub query produces better plans.
Other things:
- Removed on duplicate call to sel->init_select() in
LEX::add_primary_to_query_expression_body()
- I moved the testing of
tab->table->pos_in_table_list->is_materialized_derived()
in join_read_const_table() to the caller as it caused problems for
derived tables that could be proven to be const tables.
This also is likely to fix some bugs as if join_read_const_table()
was aborted, the table was left marked as JT_CONST, which cannot
be good. I added an ASSERT there for now that can be removed when
the code has been properly tested.
Diffstat (limited to 'sql/sql_derived.cc')
-rw-r--r-- | sql/sql_derived.cc | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/sql/sql_derived.cc b/sql/sql_derived.cc index 6f0857239dd..1fa5179875c 100644 --- a/sql/sql_derived.cc +++ b/sql/sql_derived.cc @@ -870,18 +870,20 @@ bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *derived) goto exit; /* - Temp table is created so that it hounours if UNION without ALL is to be + Temp table is created so that it honors if UNION without ALL is to be processed - As 'distinct' parameter we always pass FALSE (0), because underlying - query will control distinct condition by itself. Correct test of - distinct underlying query will be is_unit_op && + As 'distinct' parameter we pass unit->distinct, which tells us if + the values should be uniq. + Note that the underlying query will also control distinct condition. + Correct test of distinct underlying query will be is_unit_op && !unit->union_distinct->next_select() (i.e. it is union and last distinct SELECT is last SELECT of UNION). */ thd->create_tmp_table_for_derived= TRUE; if (!(derived->table) && - derived->derived_result->create_result_table(thd, &unit->types, FALSE, + derived->derived_result->create_result_table(thd, &unit->types, + unit->distinct, (first_select->options | thd->variables.option_bits | TMP_TABLE_ALL_COLUMNS), |