Bug #111198 Using index for skip scan optimization produces wrong result
Submitted: 30 May 2023 10:42 Modified: 30 May 2023 12:11
Reporter: Adithya K S Email Updates:
Status: Can't repeat Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:8.0.31 OS:Any
Assigned to: CPU Architecture:Any

[30 May 2023 10:42] Adithya K S
Description:
MySQL produces wrong result when select is run on the first column (index order) in a table with composite key and indexes created automatically on composite key.
The same select query works fine when run as a select * query

Running EXPLAIN <query> produces the following under EXTRA column:
Using where; Using index for skip scan; Using filesort

Please check the query details attached to get the complete details of the issue.

How to repeat:
1.Consider the following table:

CREATE TABLE `group_info` (
`UpdateTime` datetime NOT NULL,
`GroupName` varchar(128) NOT NULL,
`username` varchar(128) NOT NULL,
`DisplayName` varchar(256) NOT NULL,
KEY `date_user` (`UpdateTime`, `GroupName`, `username`),
KEY `date_group` (`UpdateTime`, `GroupName`)
); 

ENGINE=InnoDB DEFAULT CHARSET=latin1

2. Index Created is of the form:
mysql> SHOW INDEXES FROM group_info;
+----------------+------------+------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table          | Non_unique | Key_name   | Seq_in_index | Column_name   | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+----------------+------------+------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| group_info|          1 | date_user  |            1 | UpdateTime    | A         |           1 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
| group_info|          1 | date_user  |            2 | GroupName| A         |           1 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
| group_info|          1 | date_user  |            3 | username      | A         |           1 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
| group_info|          1 | date_group |            1 | UpdateTime    | A         |           1 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
| group_info|          1 | date_group |            2 | GroupName| A         |           1 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
+----------------+------------+------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+

3. When select is run on this table, the result is missing many records.
Sample Query run: 

select UpdateTime from group_info where username='test' and GroupName = 'testgroup1' order by UpdateTime desc limit 1;

EXPLAIN on this select Query:

id: 1
select_type: SIMPLE
table: group_info
partitions: NULL
type: RANGE
possible_keys: date_user
key: date_user
key_len: 135
ref: NULL
rows: 7075102
filtered: 10.00
Extra: Using where; Using index for skip scan; Using filesort

4. Alternate select * query which produces the correct result:

select * from group_info where username='test' and GroupName = 'testgroup1' order by UpdateTime desc limit 1;
[30 May 2023 12:11] MySQL Verification Team
Hi Mr. Adithya,

Thank you for your bug report.

However, we are not able to repeat your results.

First of all, we do not see any results at all. 

Second, we do not see a single row entered into that table, so how can we repeat your test case ??? Is your table totally empty ???

Third your EXPLAIN queries should be made  for both queries. Next those  should be printed in tabular form with EXTENDED and ANALYSE forms as well.

Fourth, we are using latest releases, so in this case it is 8.0.33.

Can't repeat.