[MDEV-26278] Table elimination does not work across derived tables Created: 2021-07-29 Updated: 2023-03-21 Resolved: 2022-07-31 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Optimizer |
| Fix Version/s: | 10.10.0 |
| Type: | Task | Priority: | Critical |
| Reporter: | Sergei Petrunia | Assignee: | Oleg Smirnov |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | Preview_10.10, optimizer-easy, optimizer-feature | ||
| Issue Links: |
|
||||||||||||||||||||||||||||||||
| Description |
|
A dataset (not necessarily minimal):
A non-mergeable view where table t12 can be eliminated:
EXPLAIN shows it it is indeed eliminated:
Now, let's add a column from t12 into the select list:
and run a query that doesn't use it:
EXPLAIN shows t12 was not eliminated:
|
| Comments |
| Comment by Sergei Petrunia [ 2022-01-18 ] | ||||||||||||||||
|
Related to MDEV-27201. MDEV-27201 focuses on the point that the unused fields in derived tables are still created and computed. | ||||||||||||||||
| Comment by Sergei Petrunia [ 2022-01-18 ] | ||||||||||||||||
|
The report text in this MDEV describes how Table Elimination could be made to work when eliminated tables are used in unused columns of derived tables. This is worth implementing, there is also another MDEV about the same topic - MDEV-27201. Another goal which wasn't explicitly mentioned so far: can we eliminate the whole derived table in this case, like it is shown here: The idea is fairly basic: VIEWs v2b and v2c have "group by t11.a" which means v2b.a (or v2c.a) is the derived table's "primary key". If Table Elimination in the top-level select is aware that v2b.a is v2b's PRIMARY_KEY:
then it will be able to eliminate v2b entirely. | ||||||||||||||||
| Comment by Sergei Petrunia [ 2022-01-18 ] | ||||||||||||||||
|
Question: could we borrow functional dependency check code from https://github.com/MariaDB/server/tree/10.5-mdev-11588 ? | ||||||||||||||||
| Comment by Sergei Petrunia [ 2022-01-21 ] | ||||||||||||||||
|
... upon a closer examination, it looks like we could not. | ||||||||||||||||
| Comment by Oleg Smirnov [ 2022-02-05 ] | ||||||||||||||||
|
Sergey, I can see the derived table v2c has one key as I debug the elimination code, but this key doesn't have HA_NOSAME flag and thus not considered as primary: (opt_table_elimination.cc line 1594): } Since the key exists, I believe there should be some specific code that assigns keys for derived tables but I can't find it. Can you please give me a hint where to look at? | ||||||||||||||||
| Comment by Oleg Smirnov [ 2022-02-11 ] | ||||||||||||||||
|
Answering to my previous question to Sergey: there is a special code for generating keys for derived tables: And eventually this function calls: I.e. generated keys are always non-unique and this prevents the table elimination logic from correct determination of functional dependency. So now the task is to determine whether the generated key is unique and pass the correct bool flag to add_tmp_table(). | ||||||||||||||||
| Comment by Sergei Petrunia [ 2022-02-13 ] | ||||||||||||||||
|
generate_derived_keys() generates "keys"(or rather, their descriptions) for derived_with_keys optimization. Initially, my intent for this MDEV was not to make use of derived_with_keys. Now, I'm thinking of whether we should do that. One argument against it is that it doesn't generate the set of keys we need.
Debugging, I can see two keys created:
while the unique key would be the KEY(a,b) here. | ||||||||||||||||
| Comment by Sergei Petrunia [ 2022-02-13 ] | ||||||||||||||||
|
A piece in TABLE::add_tmp_key() which might be relevant to this MDEV:
They detect another case where the derived table has no duplicates. They only adjust the index statistics, though. | ||||||||||||||||
| Comment by Oleg Smirnov [ 2022-04-02 ] | ||||||||||||||||
|
The implementation is pushed to bb-10.9- | ||||||||||||||||
| Comment by Sergei Petrunia [ 2022-05-19 ] | ||||||||||||||||
|
Review: https://lists.launchpad.net/maria-developers/msg13155.html | ||||||||||||||||
| Comment by Oleg Smirnov [ 2022-05-24 ] | ||||||||||||||||
|
I believe I have addressed all the review comments. | ||||||||||||||||
| Comment by Sergei Petrunia [ 2022-06-09 ] | ||||||||||||||||
|
Second review: https://lists.launchpad.net/maria-developers/msg13159.html | ||||||||||||||||
| Comment by Sergei Petrunia [ 2022-06-12 ] | ||||||||||||||||
|
Third review: https://lists.launchpad.net/maria-developers/msg13161.html | ||||||||||||||||
| Comment by Oleg Smirnov [ 2022-06-13 ] | ||||||||||||||||
|
Final version is pushed to new branch preview-10.10-optimizer | ||||||||||||||||
| Comment by Alice Sherepa [ 2022-07-20 ] | ||||||||||||||||
|
It is ok to push it into 10.10. Thanks! |