Bug #70112 "expr like '%'" is not optimized away when expr is a NOT NULL column
Submitted: 21 Aug 2013 20:00 Modified: 22 Aug 2013 14:29
Reporter: Justin Swanhart Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S4 (Feature request)
Version:5.6,5.5,5.1,5.0 OS:Any
Assigned to: CPU Architecture:Any

[21 Aug 2013 20:00] Justin Swanhart
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 '%'
[22 Aug 2013 14:29] MySQL Verification Team
This behavior is tested and was verified. However, I do not consider it a bug, but a feature request. A feature request that should be definitely scheduled.