[MDEV-8787] Return Impossible WHERE instead of Full table scan on some admittedly false field=const expressions Created: 2015-09-10  Updated: 2023-11-30

Status: Open
Project: MariaDB Server
Component/s: Character Sets, Optimizer
Fix Version/s: None

Type: New Feature Priority: Major
Reporter: Alexander Barkov Assignee: Alexander Barkov
Resolution: Unresolved Votes: 0
Labels: performance

Issue Links:
PartOf
includes MDEV-28531 update on out of range criteria shoul... Open
Relates
relates to MDEV-12432 Range optimizer for ENUM and SET does... Closed
relates to MDEV-15758 Split Item_bool_func::get_mm_leaf() i... Closed
relates to MDEV-15759 Expect "Impossible WHERE" for indexed... Closed
relates to MDEV-16784 Range search does not work well for d... Open
relates to MDEV-18898 SELECT using wrong index when using o... Open

 Description   

The majority of Field_xxx classes could detect "Impossible WHERE" by catching out-of-domain constants that appear in WHERE condition in the form field=const. It could avoid full table scan in such cases.

Examples:

a DATE column compared to a DATETIME constant

{code}
DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (a DATE);
INSERT INTO t1 VALUES ('2001-01-01'),('2001-01-02');
EXPLAIN SELECT * FROM t1 WHERE a=TIMESTAMP'2001-01-01 10:20:30';
{code}
returns:

+------+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id   | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+------+-------------+-------+------+---------------+------+---------+------+------+-------------+
|    1 | SIMPLE      | t1    | ALL  | NULL          | NULL | NULL    | NULL |    2 | Using where |
+------+-------------+-------+------+---------------+------+---------+------+------+-------------+

It's to do the full table scan, through the condition is known to return FALSE for all records in the table.

INT compared to a constant that is out of range

{code}
DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (a TINYINT);
INSERT INTO t1 VALUES (10),(20),(30);
EXPLAIN SELECT * FROM t1 WHERE a=300;
{code}

INT compared to a constant with fractional digits

{code}
DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (10),(20),(30);
EXPLAIN SELECT * FROM t1 WHERE a=10.1
{code}

String column compared to a long constant

{code}
DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (a VARCHAR(5));
INSERT INTO t1 VALUES ('a'),('b'),('c');
EXPLAIN SELECT * FROM t1 WHERE a=REPEAT('a',100);
{code}



 Comments   
Comment by Ralf Gebhardt [ 2021-05-11 ]

Added the estimate based on a comment from Bar.

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