I am trying to use covering index (with a functional index) in MySQL 8.0.28 to speed up a group by query on a large InnoDB table.
Covering index created:
ALTER TABLE `sp_files`
ADD INDEX `ix_site_header_to_cleaned_text2` (`sp_site_foreign_key`, (length(`cleaned_text`)));
Using only the fields from the covering index I would expect that MySQL uses the index instead of the underlying table - so I would expect the Extra column of the output of the EXPLAIN to show Using index:
EXPLAIN
SELECT sp_site_foreign_key,
count(case when length(`cleaned_text`) > 100 then 1 else NULL END) as num1,
count(1) as num2
FROM sp_files USE INDEX (`ix_site_header_to_cleaned_text2`)
GROUP by sp_site_foreign_key;
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | SIMPLE | sp_files | index | ix_site_header_to_cleaned_text2 | ix_site_header_to_cleaned_text2 | 208 | 105249 | 100 |
Any idea why it is not using the index?
All fields of the query are contained in the ix_site_header_to_cleaned_text2 index, so I would expect the explain to show Using index in the Extra column.