Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-41320

EXPLAIN lists a subquery that constant folding removed from WHERE

    XMLWordPrintable

Details

    • Bug
    • Status: In Progress (View Workflow)
    • Major
    • Resolution: Unresolved
    • 11.4
    • 11.4
    • Optimizer
    • None
    • Can result in unexpected behaviour

    Description

      EXPLAIN reports a subquery that the optimizer removed and that can never run; ANALYZE FORMAT=JSON shows r_loops 0 for the subquery's table.

      create table t1 (t text character set utf8mb4);
      insert into t1 values ('a'),('b');
      explain select * from t1 where t = 'b' and (1 = 1 or t = (select t from t1 limit 1));
      explain select * from t1 where t = 'b' and 1 = 0 and t = (select t from t1 limit 1);
      drop table t1;
      

      id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
      1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
      2	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2
      id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
      1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
      2	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2
      

      The first WHERE reduces to t = 'b' and the second to Impossible WHERE, yet both plans list select#2. A constant IN shows the same thing, for example 'b' in ('b', (select t from t1 limit 1)) and t = 'b'.

      The column is utf8mb4, the default character set in 11.8, so no multiple equality is built for t = (select ...). With latin1, the default in 11.4, the multiple equality evaluates the subquery once during optimization (Item_equal::add_const()), before the WHERE is folded. ANALYZE then shows r_loops 1 for the first two queries, on 11.4 and on 11.8 alike. EXPLAIN lists select#2 regardless of the character set. For a table created without a character set, 11.4 lists a subquery that ran once yet is no longer part of the plan, while 11.8 lists a subquery that never runs (r_loops 0). The constant IN example never runs the subquery under either character set.

      Attachments

        Issue Links

          Activity

            People

              Gosselin Dave Gosselin
              Gosselin Dave Gosselin
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:

                Time Tracking

                  Estimated:
                  Original Estimate - 0d
                  0d
                  Remaining:
                  Time Spent - 1h 20m Remaining Estimate - 1d
                  1d
                  Logged:
                  Time Spent - 1h 20m Remaining Estimate - 1d
                  1h 20m

                  Git Integration

                    Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.