Skip to content

MDEV-36092 New-style hint: [NO_]SPLIT_MATERIALIZED #4019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 12.0-mdev-36106-no-derived-condition-pushdown-hint
Choose a base branch
from

Conversation

DaveGosselin-MariaDB
Copy link
Member

Support for optimizer hints NO_SPLIT_MATERIALIZED and SPLIT_MATERIALIZED. These hints allow fine-grained control of the "lateral derived" optimization within a query.

@DaveGosselin-MariaDB DaveGosselin-MariaDB self-assigned this Apr 25, 2025
@DaveGosselin-MariaDB DaveGosselin-MariaDB force-pushed the 12.0-mdev-36106-no-derived-condition-pushdown-hint branch 2 times, most recently from 4090aa5 to 790b6c2 Compare April 29, 2025 19:20
@DaveGosselin-MariaDB DaveGosselin-MariaDB force-pushed the 12.0-mdev-36092-no-split-materialized-hint branch from 083fb77 to f9e6acd Compare April 29, 2025 19:30
@DaveGosselin-MariaDB DaveGosselin-MariaDB force-pushed the 12.0-mdev-36106-no-derived-condition-pushdown-hint branch from 790b6c2 to ed022e6 Compare April 30, 2025 15:24
@DaveGosselin-MariaDB DaveGosselin-MariaDB force-pushed the 12.0-mdev-36092-no-split-materialized-hint branch from f9e6acd to 14c7ce9 Compare April 30, 2025 15:30
@DaveGosselin-MariaDB DaveGosselin-MariaDB force-pushed the 12.0-mdev-36106-no-derived-condition-pushdown-hint branch from ed022e6 to db4f01b Compare May 1, 2025 17:28
@DaveGosselin-MariaDB DaveGosselin-MariaDB force-pushed the 12.0-mdev-36092-no-split-materialized-hint branch from 14c7ce9 to c908af3 Compare May 1, 2025 17:49
@DaveGosselin-MariaDB DaveGosselin-MariaDB force-pushed the 12.0-mdev-36106-no-derived-condition-pushdown-hint branch from db4f01b to 2421761 Compare May 2, 2025 13:47
@DaveGosselin-MariaDB DaveGosselin-MariaDB force-pushed the 12.0-mdev-36092-no-split-materialized-hint branch from c908af3 to 8647d01 Compare May 2, 2025 13:50
@DaveGosselin-MariaDB DaveGosselin-MariaDB force-pushed the 12.0-mdev-36106-no-derived-condition-pushdown-hint branch from 2421761 to ccbe612 Compare May 2, 2025 14:35
@DaveGosselin-MariaDB DaveGosselin-MariaDB force-pushed the 12.0-mdev-36092-no-split-materialized-hint branch from 8647d01 to 0ddc981 Compare May 2, 2025 14:39
@DaveGosselin-MariaDB DaveGosselin-MariaDB force-pushed the 12.0-mdev-36106-no-derived-condition-pushdown-hint branch from ccbe612 to 506a454 Compare May 5, 2025 20:26
@DaveGosselin-MariaDB DaveGosselin-MariaDB force-pushed the 12.0-mdev-36092-no-split-materialized-hint branch from 0ddc981 to 1328584 Compare May 5, 2025 20:52
@CLAassistant
Copy link

CLAassistant commented May 5, 2025

CLA assistant check
All committers have signed the CLA.

@DaveGosselin-MariaDB DaveGosselin-MariaDB force-pushed the 12.0-mdev-36106-no-derived-condition-pushdown-hint branch from 506a454 to ec3d71c Compare May 5, 2025 20:54
@DaveGosselin-MariaDB DaveGosselin-MariaDB force-pushed the 12.0-mdev-36106-no-derived-condition-pushdown-hint branch from ec3d71c to 4487f28 Compare May 6, 2025 20:17
@DaveGosselin-MariaDB DaveGosselin-MariaDB force-pushed the 12.0-mdev-36092-no-split-materialized-hint branch from 1328584 to f6fe780 Compare May 6, 2025 20:21
@DaveGosselin-MariaDB DaveGosselin-MariaDB force-pushed the 12.0-mdev-36106-no-derived-condition-pushdown-hint branch from 4487f28 to 656c220 Compare May 21, 2025 19:03
@DaveGosselin-MariaDB DaveGosselin-MariaDB force-pushed the 12.0-mdev-36092-no-split-materialized-hint branch 2 times, most recently from d4401c2 to b54e212 Compare May 22, 2025 20:44
@DaveGosselin-MariaDB DaveGosselin-MariaDB force-pushed the 12.0-mdev-36106-no-derived-condition-pushdown-hint branch from 656c220 to 6a60503 Compare May 23, 2025 14:51
@DaveGosselin-MariaDB DaveGosselin-MariaDB force-pushed the 12.0-mdev-36092-no-split-materialized-hint branch from b54e212 to c2038f7 Compare May 23, 2025 14:55
@DaveGosselin-MariaDB DaveGosselin-MariaDB requested review from spetrunia and removed request for Olernov May 23, 2025 18:28
@DaveGosselin-MariaDB DaveGosselin-MariaDB force-pushed the 12.0-mdev-36106-no-derived-condition-pushdown-hint branch from 6a60503 to e045f0c Compare May 23, 2025 20:32
Copy link
Member

@spetrunia spetrunia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DaveGosselin-MariaDB , example where hint doesn't force it:

create table ten(a int);
insert into ten select seq from seq_1_to_10;

create table one_k(a int);
insert into one_k select seq from seq_1_to_1000;

CREATE TABLE `t1000` (
  `grp` int(11) DEFAULT NULL,
  `val` int(11) DEFAULT NULL,
  KEY `grp` (`grp`)
);

insert into t1000 select A.seq, B.seq from seq_1_to_100 A, seq_1_to_10 B;
analyze table t1000;

This works:

explain 
select /*+ SPLIT_MATERIALIZED(TBL) */ * 
from 
  ten T1, (select grp, count(*) from t1000 group by grp) TBL where TBL.grp=T1.a;

+------+-----------------+------------+------+---------------+------+---------+---------+------+-------------+
| id   | select_type     | table      | type | possible_keys | key  | key_len | ref     | rows | Extra       |
+------+-----------------+------------+------+---------------+------+---------+---------+------+-------------+
|    1 | PRIMARY         | T1         | ALL  | NULL          | NULL | NULL    | NULL    | 10   | Using where |
|    1 | PRIMARY         | <derived2> | ref  | key0          | key0 | 5       | j4.T1.a | 1    |             |
|    2 | LATERAL DERIVED | t1000      | ref  | grp           | grp  | 5       | j4.T1.a | 10   | Using index |
+------+-----------------+------------+------+---------------+------+---------+---------+------+-------------+

This doesn't (the query is the same except table ten is changed to one_k)

explain 
select /*+ SPLIT_MATERIALIZED(TBL) */ * 
from 
  one_k T1, (select grp, count(*) from t1000 group by grp) TBL where TBL.grp=T1.a;
+------+-------------+------------+-------+---------------+------+---------+---------+------+----------------------------------------------+
| id   | select_type | table      | type  | possible_keys | key  | key_len | ref     | rows | Extra                                        |
+------+-------------+------------+-------+---------------+------+---------+---------+------+----------------------------------------------+
|    1 | PRIMARY     | T1         | ALL   | NULL          | NULL | NULL    | NULL    | 1000 | Using where                                  |
|    1 | PRIMARY     | <derived2> | ref   | key0          | key0 | 5       | j4.T1.a | 10   |                                              |
|    2 | DERIVED     | t1000      | index | grp           | grp  | 5       | NULL    | 1000 | Using index; Using temporary; Using filesort |
+------+-------------+------------+-------+---------------+------+---------+---------+------+----------------------------------------------+

@DaveGosselin-MariaDB DaveGosselin-MariaDB force-pushed the 12.0-mdev-36106-no-derived-condition-pushdown-hint branch from e045f0c to cde6fdf Compare May 29, 2025 14:37
@DaveGosselin-MariaDB DaveGosselin-MariaDB force-pushed the 12.0-mdev-36092-no-split-materialized-hint branch 2 times, most recently from 8724d9c to 9464945 Compare May 30, 2025 12:48
@DaveGosselin-MariaDB
Copy link
Member Author

@DaveGosselin-MariaDB , example where hint doesn't force it:

Fixed in latest patch, thank you for this great example!

@spetrunia
Copy link
Member

Ok, retrying the example above on the latest patch and indeed one can use a hint to force split-materialized...

analyze format=json select  * from one_k T1, (select grp, count(*) from t1000 group by grp) TBL where TBL.grp=T1.a\G
analyze format=json select /*+ SPLIT_MATERIALIZED(TBL) */ * from one_k T1, (select grp, count(*) from t1000 group by grp) TBL where TBL.grp=T1.a\G

It's interesting that the cost becomes lower:

  • without hint: query_block.cost= 2.047789395
  • with hint: query_block.cost= 0.4491132

Looks like two things are wrong: if the cost is lower with split-materialized why didn't we pick it by default, second, why is the cost lower if we're going to access all GROUP BY groups?

Also filed https://jira.mariadb.org/browse/MDEV-36913.
Well, all of this seems to be outside of the scope of this MDEV.

@spetrunia spetrunia self-requested a review June 1, 2025 11:36
Copy link
Member

@spetrunia spetrunia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check and apply this patch https://gist.github.com/spetrunia/efdf4394e386a367e441a75d5c27ea22

it does this:

  • print "forced_by_hint" only if it's true (which is rare)
  • move hint_table_state() declaration to be together with others.
  • fix coding style in Table_levelhint_table_state_hint::resolve and
  • make is_split_materialized_allowed use pointer for OUT parameter

Ok to push after this is done

Support for optimizer hints NO_SPLIT_MATERIALIZED and
SPLIT_MATERIALIZED.  These hints allow fine-grained control
of the "lateral derived" optimization within a query.

Introduces new overload of hint_table_state function which
tells both a hint's value as well as whether it is present.
This is useful to disambiguate cases that the other version
of hint_table_state cannot, such as when a hint is forcing
a behavior in the optimizer that it would not normally do
and the corresponding optimizer switch is enabled.
@DaveGosselin-MariaDB DaveGosselin-MariaDB force-pushed the 12.0-mdev-36092-no-split-materialized-hint branch from 9464945 to 4d1e4d7 Compare June 2, 2025 13:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

4 participants