[MDEV-18964] Non-optimal execution of WHERE num_field=const AND num_field=const Created: 2019-03-19  Updated: 2019-03-19

Status: Open
Project: MariaDB Server
Component/s: Data types, Optimizer
Affects Version/s: 10.1, 10.2, 10.3, 10.4
Fix Version/s: 10.4

Type: Bug Priority: Major
Reporter: Alexander Barkov Assignee: Alexander Barkov
Resolution: Unresolved Votes: 0
Labels: None

Issue Links:
Blocks
blocks MDEV-18898 SELECT using wrong index when using o... Open
is blocked by MDEV-18968 Both (WHERE 0.1) and (WHERE NOT 0.1) ... Closed

 Description   

Usually, WHERE condition gets optimized by removing reundant parts. But in some cases optimization does not work.

Good example: int_field=int_const1 AND int_field=int_const1

CREATE OR REPLACE TABLE t1 (a INT);
INSERT INTO t1 VALUES (1),(2),(3);
EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=1 AND a=1;
SHOW WARNINGS;

+-------+------+--------------------------------------------------------------------------+
| Level | Code | Message                                                                  |
+-------+------+--------------------------------------------------------------------------+
| Note  | 1003 | select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` = 1 |
+-------+------+--------------------------------------------------------------------------+

The redundant part was correctly removed.

Good example: int_field=int_const1 AND int_field=int_const2

CREATE OR REPLACE TABLE t1 (a INT);
INSERT INTO t1 VALUES (1),(2),(3);
EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=1 AND a=2;
SHOW WARNINGS;

+-------+------+--------------------------------------------------------+
| Level | Code | Message                                                |
+-------+------+--------------------------------------------------------+
| Note  | 1003 | select `test`.`t1`.`a` AS `a` from `test`.`t1` where 0 |
+-------+------+--------------------------------------------------------+

The condition was correctly detected as impossible.

Good example: decimal_field=decimal_const1 AND decimal_field=int_const1

CREATE OR REPLACE TABLE t1 (a DECIMAL(10,3));
INSERT INTO t1 VALUES (1),(2),(3);
EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=1.0 AND a=1;
SHOW WARNINGS;

+-------+------+----------------------------------------------------------------------------+
| Level | Code | Message                                                                    |
+-------+------+----------------------------------------------------------------------------+
| Note  | 1003 | select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` = 1.0 |
+-------+------+----------------------------------------------------------------------------+

The redundant part was correctly removed.

Good example: decimal_field=decimal_const1 AND decimal_field=int_const2

CREATE OR REPLACE TABLE t1 (a DECIMAL(10,3));
INSERT INTO t1 VALUES (1),(2),(3);
EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=1.0 AND a=2;
SHOW WARNINGS;

+-------+------+--------------------------------------------------------+
| Level | Code | Message                                                |
+-------+------+--------------------------------------------------------+
| Note  | 1003 | select `test`.`t1`.`a` AS `a` from `test`.`t1` where 0 |
+-------+------+--------------------------------------------------------+

The condition was correctly detected as impossible.

Bad example: int_field=int_const1 AND int_field=decimal_const1

CREATE OR REPLACE TABLE t1 (a INT);
INSERT INTO t1 VALUES (1),(2),(3);
EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=1 AND a=1.0;
SHOW WARNINGS;

+-------+------+----------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                            |
+-------+------+----------------------------------------------------------------------------------------------------+
| Note  | 1003 | select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` = 1 and `test`.`t1`.`a` = 1.0 |
+-------+------+----------------------------------------------------------------------------------------------------+

The redundant part was not removed.

Bad example: int_field=int_const1 AND int_field=decimal_const2

CREATE OR REPLACE TABLE t1 (a INT);
INSERT INTO t1 VALUES (1),(2),(3);
EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=1 AND a=2.0;
SHOW WARNINGS;

+-------+------+----------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                            |
+-------+------+----------------------------------------------------------------------------------------------------+
| Note  | 1003 | select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` = 1 and `test`.`t1`.`a` = 2.0 |
+-------+------+----------------------------------------------------------------------------------------------------+

The condition was not detected as impossible.

CREATE OR REPLACE TABLE t1 (a INT);
INSERT INTO t1 VALUES (1),(2),(3);
EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=1 AND a=1.1;
SHOW WARNINGS;

+-------+------+----------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                            |
+-------+------+----------------------------------------------------------------------------------------------------+
| Note  | 1003 | select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` = 1 and `test`.`t1`.`a` = 1.1 |
+-------+------+----------------------------------------------------------------------------------------------------+

The condition was not detected as impossible.


Generated at Thu Feb 08 08:48:04 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.