Bug #99995 Histogram is not used for filtering estimate when index is disabled
Submitted: 26 Jun 2020 7:57 Modified: 26 Jun 2020 12:08
Reporter: Øystein Grøvlen Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:8.0 OS:Any
Assigned to: CPU Architecture:Any

[26 Jun 2020 7:57] Øystein Grøvlen
Description:
For the following query the filtering estimate is not based on histogram when index is not available:

mysql> explain select /*+ NO_INDEX(city) */ avg(population) from city where countrycode = 'CHN' or countrycode = 'IND';
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | city  | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 4188 |     0.86 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

For the equivalent query using IN expression, the filtering estimate is based on histogram:

mysql> explain select /*+ NO_INDEX(city) */ avg(population) from city where countrycode IN ('CHN', 'IND');
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | city  | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 4188 |    17.26 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

How to repeat:
Using the world database:

analyze table city update histogram on countrycode;
explain select /*+ NO_INDEX(city) */ avg(population) from city where countrycode = 'CHN' or countrycode = 'IND';
explain select /*+ NO_INDEX(city) */ avg(population) from city where countrycode IN ('CHN', 'IND');
[26 Jun 2020 12:08] MySQL Verification Team
Hi Mr. Øystein Grøvlen,

Thank you for your bug report.

I have managed to repeat this optimiser deficiency.

Verified as reported.