Details
-
Bug
-
Status: Open (View Workflow)
-
Minor
-
Resolution: Unresolved
-
12.0.2
-
None
-
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
- relates to
-
MDEV-37714 Missed optimization opportunities of "FALSE and any_expr"
-
- Open
-