[MDEV-8599] "WHERE varchar_field LIKE temporal_const" does not use range optimizer Created: 2015-08-11  Updated: 2016-01-11  Resolved: 2015-08-12

Status: Closed
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: 10.1
Fix Version/s: 10.1.7

Type: Bug Priority: Major
Reporter: Alexander Barkov Assignee: Alexander Barkov
Resolution: Fixed Votes: 0
Labels: performance


 Description   

This script:

DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (a VARCHAR(10) CHARACTER SET latin1, KEY(a)) ENGINE=MyISAM;
INSERT INTO t1 VALUES ('00:00:00');
INSERT INTO t1 VALUES ('00:00:01');
INSERT INTO t1 VALUES ('00:00:02');
INSERT INTO t1 VALUES ('00:00:03');
INSERT INTO t1 VALUES ('00:00:04');
INSERT INTO t1 VALUES ('00:00:05');
INSERT INTO t1 VALUES ('00:00:06');
INSERT INTO t1 VALUES ('00:00:07');
EXPLAIN SELECT * FROM t1 WHERE a LIKE '00:00:00';

returns

+------+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+
| id   | select_type | table | type  | possible_keys | key  | key_len | ref  | rows | Extra                    |
+------+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+
|    1 | SIMPLE      | t1    | range | a             | a    | 13      | NULL |    1 | Using where; Using index |
+------+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+

The index is used to optimize the query.

Now if I change the EXPLAIN SELECT query to:

EXPLAIN SELECT * FROM t1 WHERE a LIKE TIME'00:00:00';

it does not use the index any longer:

+------+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+
| id   | select_type | table | type  | possible_keys | key  | key_len | ref  | rows | Extra                    |
+------+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+
|    1 | SIMPLE      | t1    | index | a             | a    | 13      | NULL |    8 | Using where; Using index |
+------+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+

It happens because of this wrong condition in get_mm_leaf:

  if (value->cmp_type() == TIME_RESULT && field->cmp_type() != TIME_RESULT)
    goto end;

This condition is valid for the comparison operators (<,>,=, etc), but should not be checked for LIKE.


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