Bug #67753 index_merge of 2 rows looses to full table scan
Submitted: 29 Nov 2012 9:22 Modified: 29 Nov 2012 17:05
Reporter: Ruslan Zakirov Email Updates:
Status: Can't repeat Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S2 (Serious)
Version:5.5 OS:Any
Assigned to: CPU Architecture:Any
Tags: index_merge

[29 Nov 2012 9:22] Ruslan Zakirov
Description:
Hi,

The following two statements use very different plans:

mysql> EXPLAIN SELECT * FROM t WHERE (name LIKE 'adfgjlskdm%' OR email LIKE 'adfgjlskdm%');
+----+-------------+-------+-------------+---------------+-------+---------+------+------+--------------------------------------+
| id | select_type | table | type        | possible_keys | key   | key_len | ref  | rows | Extra                                |
+----+-------------+-------+-------------+---------------+-------+---------+------+------+--------------------------------------+
|  1 | SIMPLE      | t     | index_merge | t1,t2         | t1,t2 | 767,768 | NULL |    2 | Using sort_union(t1,t2); Using where |
+----+-------------+-------+-------------+---------------+-------+---------+------+------+--------------------------------------+
1 row in set (0.00 sec)

mysql> EXPLAIN SELECT * FROM t WHERE (name LIKE 'adfgjlskdm%' OR email LIKE 'adfgjlskdm%')
    ->     AND email != '' AND email IS NOT NULL;
+----+-------------+-------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows   | Extra       |
+----+-------------+-------+------+---------------+------+---------+------+--------+-------------+
|  1 | SIMPLE      | t     | ALL  | t1,t2         | NULL | NULL    | NULL | 100234 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+--------+-------------+
1 row in set (0.00 sec)

How to repeat:
Attached script to create data.

Suggested fix:
index_merge scan should be used in both cases.
[29 Nov 2012 9:23] Ruslan Zakirov
perl script to create data

Attachment: create_data.pl (text/x-perl-script), 775 bytes.

[29 Nov 2012 17:05] Sveta Smirnova
Thank you for the report.

This is fixed in version 5.6. Please upgrade.