Bug #110746 invalid NO_INDEX_USED=1 metric when using UNION in queries.

Submitted: 20 Apr 2023 8:43 Modified: 28 Apr 2023 7:22
Reporter: Denis Subbota Email Updates:
Status: Duplicate Impact on me:
None 
Category:MySQL Server: Performance Schema Severity:S3 (Non-critical)
Version:5.7.39-42-log OS:CentOS (CentOS Linux 7 (Core))
Assigned to: CPU Architecture:x86 (Linux 3.10.0-1160.49.1.el7.x86_64)

[20 Apr 2023 8:43] Denis Subbota
Description:
I have the following table

mysql> show create table sbtest1\G
*************************** 1. row ***************************
       Table: sbtest1
Create Table: CREATE TABLE `sbtest1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `k` int(11) NOT NULL DEFAULT '0',
  `c` char(120) NOT NULL DEFAULT '',
  `pad` char(60) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `k_1` (`k`)
) ENGINE=InnoDB AUTO_INCREMENT=5000001 DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

As you can see it has a primary key in column "id" and an index on column "k". I executed queries against both queries and as it was expected those were not reported as NO_INDEX_USED. However when I did a union between both tables I got it flagged as NO_INDEX_USED

mysql> select t.processlist_id,esh.thread_id,esh.event_name,left(esh.sql_text,80) as sql_text,esh.rows_affected,esh.rows_sent,esh.rows_examined,esh.NO_INDEX_USED  from performance_schema.events_statements_history esh inner join performance_schema.threads t on esh.thread_id = t.thread_id where t.processlist_id=2 order by esh.event_id;
+----------------+-----------+---------------------------------+----------------------------------------------------------------------------------+---------------+-----------+---------------+---------------+
| processlist_id | thread_id | event_name                      | sql_text                                                                         | rows_affected | rows_sent | rows_examined | NO_INDEX_USED |
+----------------+-----------+---------------------------------+----------------------------------------------------------------------------------+---------------+-----------+---------------+---------------+
|              2 |        39 | statement/sql/select            | select id,k from sbtest1 where id = 1                                            |             0 |         1 |             1 |             0 |
|              2 |        39 | statement/sql/select            | select id,k from sbtest1 where k = 2521302                                       |             0 |        95 |            95 |             0 |
|              2 |        39 | statement/sql/select            | select id,k from sbtest1 where id = 1 union select id,k from sbtest1 where k = 2 |             0 |        96 |           192 |             1 |
+----------------+-----------+---------------------------------+----------------------------------------------------------------------------------+---------------+-----------+---------------+---------------+
10 rows in set (0.00 sec)

If we look at the explain plan we can see that the select_type "UNION RESULT" is considered as not using index.

mysql> explain
    -> select id,k from sbtest1 where id = 1 union select id,k from sbtest1 where k = 2521302;
+----+--------------+------------+------------+-------+---------------+---------+---------+-------+------+----------+-----------------+
| id | select_type  | table      | partitions | type  | possible_keys | key     | key_len | ref   | rows | filtered | Extra           |
+----+--------------+------------+------------+-------+---------------+---------+---------+-------+------+----------+-----------------+
|  1 | PRIMARY      | sbtest1    | NULL       | const | PRIMARY       | PRIMARY | 4       | const |    1 |   100.00 | NULL            |
|  2 | UNION        | sbtest1    | NULL       | ref   | k_1           | k_1     | 4       | const |   95 |   100.00 | Using index     |
| NULL | UNION RESULT | <union1,2> | NULL       | ALL   | NULL          | NULL    | NULL    | NULL  | NULL |     NULL | Using temporary |
+----+--------------+------------+------------+-------+---------------+---------+---------+-------+------+----------+-----------------+

Having said that, I think it is fair to assume that your query is using indexes and that it is flagged as NO_INDEX_USED for the UNION, which should be a bug.

How to repeat:
You may use the same steps mentioned above to reproduce. It should be quite simple to reproduce. 

Create table for test:

CREATE TABLE `sbtest1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `k` int(11) NOT NULL DEFAULT '0',
  `c` char(120) NOT NULL DEFAULT '',
  `pad` char(60) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `k_1` (`k`)
) 

Populate some data inside of the table,and run next queries:
select id,k from sbtest1 where id = 1 ;
select id,k from sbtest1 where k = 2521302;
select id,k from sbtest1 where id = 1 union select id,k from sbtest1 where k = 2;

Verify odd behavior with: 

 select t.processlist_id,esh.thread_id,esh.event_name,left(esh.sql_text,80) as sql_text,esh.rows_affected,esh.rows_sent,esh.rows_examined,esh.NO_INDEX_USED  from performance_schema.events_statements_history esh inner join performance_schema.threads t on esh.thread_id = t.thread_id where t.processlist_id=2 order by esh.event_id;
[20 Apr 2023 12:04] MySQL Verification Team
Hi Mr. Subbota,

Thank you for your bug report.

However, 5.7 is receiving only hard bug fixes and this is not a hard bug.

So, first , please check if the same bug exists in 8.0. Next, do send us a test case for 8.0 with INSERT statements that will show the behaviour in 8.0.

We are waiting on your feedback.
[28 Apr 2023 5:57] Denis Subbota
Hello team, 

I have verified that this bug is still presented in 8 versions of MySQL 
and created the same bug there https://bugs.mysql.com/bug.php?id=110857&thanks=4.

Regards,
Denis Subbota.
[28 Apr 2023 7:22] MySQL Verification Team
Duplicate of Bug #110857(ideally, this should have been the base but Bug #110857 verified hence marking it).

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.041 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.