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

Missed optimization opportunities of "TRUE OR any_expr" in SELECT clause

    XMLWordPrintable

Details

    • Bug
    • Status: Open (View Workflow)
    • Minor
    • Resolution: Unresolved
    • 12.0.2
    • None
    • Optimizer
    • None
    • ubuntu 22.04

    Description

      Hi, MariaDB developers.

      Thanks for reading my report.

      Since "TRUE OR any_expr" is TRUE. MariaDB can apply short-circuit evaluation to it. When the true expression is "1", "TRUE", or "2>1", etc., the optimizer can rewrite it to "TRUE" directly. In another scenario, the executor can short-circuit when determining that one expression is true after scanning one table, and skip scanning another table.

      You can reproduce as follows:

      -- Create table t1 with 1 row and t2 with 10,000,000 rows
      CREATE TABLE t1(c0 INT8);
      CREATE TABLE t2(c0 INT8);
       
      INSERT INTO t1 VALUES(1);
      INSERT INTO t2 SELECT seq FROM seq_1_to_10000000;
       
       
      -- The optimizer can rewrite such a SQL statement to "SELECT TRUE" directly
      SELECT true or (SELECT MIN(c0) FROM t2)<0;
      +------------------------------------+
      | true or (SELECT MIN(c0) FROM t2)<0 |
      +------------------------------------+
      |                                  1 |
      +------------------------------------+
      1 row in set (0.000 sec)
       
      SELECT (SELECT MIN(c0) FROM t2)<0 OR true;
      +------------------------------------+
      | (SELECT MIN(c0) FROM t2)<0 OR true |
      +------------------------------------+
      |                                  1 |
      +------------------------------------+
      1 row in set (1.674 sec)
       
      -- The executor can short-circuit when determining that one expression is true after scanning one table, and skip scanning another table.
      SELECT (SELECT MIN(c0) FROM t1)>0 OR (SELECT MIN(c0) FROM t2)<0;
      +----------------------------------------------------------+
      | (SELECT MIN(c0) FROM t1)>0 OR (SELECT MIN(c0) FROM t2)<0 |
      +----------------------------------------------------------+
      |                                                        1 |
      +----------------------------------------------------------+
      1 row in set (0.016 sec)
       
      SELECT (SELECT MIN(c0) FROM t2)<0 OR (SELECT MIN(c0) FROM t1)>0;
      +----------------------------------------------------------+
      | (SELECT MIN(c0) FROM t2)<0 OR (SELECT MIN(c0) FROM t1)>0 |
      +----------------------------------------------------------+
      |                                                        1 |
      +----------------------------------------------------------+
      1 row in set (1.652 sec)
      

      Best regards,
      Jinhui Lai

      Attachments

        Issue Links

          Activity

            People

              psergei Sergei Petrunia
              jinhui lai jinhui lai
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:

                Git Integration

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