Bug #70112 "expr like '%'" is not optimized away when expr is a NOT NULL column
Description:
mysql> CREATE TABLE `t` (
-> `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
-> `txt` char(50) NOT NULL,
-> PRIMARY KEY (`id`),
-> KEY `idx` (`txt`)
-> ) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8 ;
Query OK, 0 rows affected (0.22 sec)
mysql> explain extended select id from t where txt like '%';
+----+-------------+-------+-------+---------------+------+---------+------+------+----------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+-------+---------------+------+---------+------+------+----------+--------------------------+
| 1 | SIMPLE | t | index | NULL | idx | 150 | NULL | 1 | 100.00 | Using where; Using index |
+----+-------------+-------+-------+---------------+------+---------+------+------+----------+--------------------------+
1 row in set, 1 warning (0.03 sec)
mysql> show warnings;
+-------+------+----------------------------------------------------------------------------------+
| Level | Code | Message |
+-------+------+----------------------------------------------------------------------------------+
| Note | 1003 | select `test`.`t`.`id` AS `id` from `test`.`t` where (`test`.`t`.`txt` like '%') |
+-------+------+----------------------------------------------------------------------------------+
1 row in set (0.00 sec)
How to repeat:
see above
Suggested fix:
For NOT NULL columns, the expression "where NOT_NULL_COL like '%'" should be optimized away like 1=1. This expression will always evaluate to true, because even empty string will match a like '%'
Portions of this website are copyright © 2001, 2002 The PHP Group
Page generated in 0.038 sec. using MySQL 8.4.7-u6-cloud
Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the
respective copyright holders.
It is not reviewed in advance
by Oracle and does not necessarily represent the opinion of
Oracle or any other party.
Description: mysql> CREATE TABLE `t` ( -> `id` int(10) unsigned NOT NULL AUTO_INCREMENT, -> `txt` char(50) NOT NULL, -> PRIMARY KEY (`id`), -> KEY `idx` (`txt`) -> ) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8 ; Query OK, 0 rows affected (0.22 sec) mysql> explain extended select id from t where txt like '%'; +----+-------------+-------+-------+---------------+------+---------+------+------+----------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+-------+---------------+------+---------+------+------+----------+--------------------------+ | 1 | SIMPLE | t | index | NULL | idx | 150 | NULL | 1 | 100.00 | Using where; Using index | +----+-------------+-------+-------+---------------+------+---------+------+------+----------+--------------------------+ 1 row in set, 1 warning (0.03 sec) mysql> show warnings; +-------+------+----------------------------------------------------------------------------------+ | Level | Code | Message | +-------+------+----------------------------------------------------------------------------------+ | Note | 1003 | select `test`.`t`.`id` AS `id` from `test`.`t` where (`test`.`t`.`txt` like '%') | +-------+------+----------------------------------------------------------------------------------+ 1 row in set (0.00 sec) How to repeat: see above Suggested fix: For NOT NULL columns, the expression "where NOT_NULL_COL like '%'" should be optimized away like 1=1. This expression will always evaluate to true, because even empty string will match a like '%'