Skip to content

MDEV-39349 NO_INDEX hint using QB_NAME doesn't work inside VIEWs#4996

Open
Olernov wants to merge 2 commits into12.3from
12.3-MDEV-39349-hints-views
Open

MDEV-39349 NO_INDEX hint using QB_NAME doesn't work inside VIEWs#4996
Olernov wants to merge 2 commits into12.3from
12.3-MDEV-39349-hints-views

Conversation

@Olernov
Copy link
Copy Markdown
Contributor

@Olernov Olernov commented Apr 27, 2026

Refine the logic of storing hints inside view definitions.

The same procedures are used for printing hints in both
EXPLAIN EXTENDED of regular statements and when storing view
definitions. For regular statements all hints (except QB_NAME)
are printed into the topmost SELECT with attachment of target
query block names. If some query blocks do not have explicit
names set with QB_NAME hints, implicit names select#1, select#2
are attached.

This approach does not work views, because view definitions do not
have such implicit numbering of query blocks. The solution is
to print hints related to a particular query block right
into this query block instead of printing them into
the topmost SELECT.

Refine the logic of storing hints inside view definitions.

The same procedures are used for printing hints in both
EXPLAIN EXTENDED of regular statements and when storing view
definitions. For regular statements all hints (except QB_NAME)
are printed into the topmost SELECT with attachment of target
query block names. If some query blocks do not have explicit
names set with QB_NAME hints, implicit names `select#1`, `select#2`
are attached.

This approach does not work views, because view definitions do not
have such implicit numbering of query blocks. The solution is
to print hints related to a particular query block right
into this query block instead of printing them into
the topmost SELECT.
@Olernov Olernov force-pushed the 12.3-MDEV-39349-hints-views branch from 61cceb6 to d240499 Compare April 27, 2026 14:14
There two variants of syntax supported for join order hints:
- hint([@qb_name] t1, t2, t3,...)
- hint (t1[@qb1], t2[@qb2],...)

However, when creating views, internal procedures may produce
a mixed variant of both:
- hint (@qb_name t1, t2@qb1, t3...)

This commit introduces support for this third variant of
syntax for join order hints, so it is supported for both
regular statements and for views
@Olernov Olernov force-pushed the 12.3-MDEV-39349-hints-views branch from d240499 to e51441f Compare April 27, 2026 14:18
Copy link
Copy Markdown
Member

@DaveGosselin-MariaDB DaveGosselin-MariaDB left a comment

Choose a reason for hiding this comment

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

Looks good!

Consider making this change in sql_select.cc, to remove the duplicate conditional branches:

--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -32620,13 +32620,9 @@ void st_select_lex::print_hints(THD *thd, String *str)
       */
       parent_lex->opt_hints_global->print(thd, str);
     }
-    else if (opt_hints_qb)
-    {
-      // For views print hints related to a QB right into the QB they target
-      opt_hints_qb->print(thd, str);
-    }
   }
-  else if (is_view && opt_hints_qb)
+
+  if (is_view && opt_hints_qb)
   {
     // For views print hints related to a QB right into the QB they target
     opt_hints_qb->print(thd, str);

# End Examples from QA
# End of 12.2 tests
#
# NO_INDEX hint using QB_NAME doesn't work inside VIEWs
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a little confusing because the tests below show that it does work, see next comment...

CREATE VIEW v1 AS SELECT /*+ QB_NAME(foo) NO_INDEX(t1@foo) */ a, b FROM t1 WHERE a<3 LIMIT 10;
SHOW CREATE VIEW v1;
View Create View character_set_client collation_connection
v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select /*+ QB_NAME(`foo`) NO_INDEX(`t1`@`foo`) */ `t1`.`a` AS `a`,`t1`.`b` AS `b` from `t1` where `t1`.`a` < 3 limit 10 utf8mb3 utf8mb3_uca1400_ai_ci
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

... here we see the hint is in the SHOW CREATE VIEW output and the query plan shows full scan ...

1 PRIMARY <derived2> ALL NULL NULL NULL NULL 10 100.00
2 DERIVED t1 ALL NULL NULL NULL NULL 1000 100.00 Using where
Warnings:
Note 1003 /* select#1 */ select `v1`.`a` AS `a`,`v1`.`b` AS `b` from `test`.`v1`
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

... so is the issue just that the hint doesn't appear here? Is it because the VIEW was merged and so we can't show the hint anymore (even though it applied)?

(If yes, then consider adding a "Limitations" section to the git commit message to document this detail)

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.

2 participants