1
0
mirror of https://github.com/MariaDB/server.git synced 2025-08-07 00:04:31 +03:00

MDEV-26278 Add functionality to eliminate derived tables from the query

Elimination of unnecessary tables from SQL queries is already present
in MariaDB. But it only works for regular tables and not for derived ones.

Imagine we have a view:
  CREATE VIEW v1 AS SELECT a, b, max(c) AS maxc FROM t1 GROUP BY a, b

Due to "GROUP BY a, b" the values of combinations {a, b} are unique,
and this fact can be treated as like derived table "v1" has a unique key
on fields {a, b}.

Suppose we have a SQL query:
  SELECT t2.* FROM t2 LEFT JOIN v1 ON t2.a=v1.a and t2.b=v1.b

1. Since {v1.a, v1.b} is unique and both these fields are bound to t2,
   "v1" is functionally dependent on t2.
   This means every record of "t2" will be either joined with
   a single record of "v1" or NULL-complemented.
2. No fields of "v1" are present on the SELECT list

These two facts allow the server to completely exclude (eliminate)
the derived table "v1" from the query.
This commit is contained in:
Oleg Smirnov
2022-02-17 22:53:37 +07:00
parent 1f0187ff8d
commit 7f0201a2b5
5 changed files with 650 additions and 20 deletions

View File

@@ -3400,7 +3400,7 @@ bool st_select_lex::test_limit()
st_select_lex* st_select_lex_unit::outer_select()
st_select_lex* st_select_lex_unit::outer_select() const
{
return (st_select_lex*) master;
}
@@ -11917,15 +11917,24 @@ bool SELECT_LEX_UNIT::explainable() const
EXPLAIN/ANALYZE unit, when:
(1) if it's a subquery - it's not part of eliminated WHERE/ON clause.
(2) if it's a CTE - it's not hanging (needed for execution)
(3) if it's a derived - it's not merged
(3) if it's a derived - it's not merged or eliminated
if it's not 1/2/3 - it's some weird internal thing, ignore it
*/
return item ?
!item->eliminated : // (1)
with_element ?
derived && derived->derived_result &&
!with_element->is_hanging_recursive(): // (2)
derived ?
derived->is_materialized_derived() : // (3)
derived->is_materialized_derived() && // (3)
!is_derived_eliminated() :
false;
}
bool SELECT_LEX_UNIT::is_derived_eliminated() const
{
if (!derived)
return false;
return derived->table->map & outer_select()->join->eliminated_tables;
}