[MDEV-13744] Force index for group by is not always honored Created: 2017-09-06 Updated: 2020-08-25 Resolved: 2018-11-02 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Optimizer |
| Affects Version/s: | 10.1, 10.2, 10.3 |
| Fix Version/s: | N/A |
| Type: | Bug | Priority: | Critical |
| Reporter: | Richard Stracke | Assignee: | Sergei Petrunia |
| Resolution: | Not a Bug | Votes: | 2 |
| Labels: | None | ||
| Environment: |
tested on docker container |
||
| Description |
|
MariaDB optimizer does not seem to honor the "force index for group by" index hint : To reproduce:
analyze format = json select id, max(ts) from t force index for group by (primary) group by
Cardinality in show indexes is 66343 for column id and for column ts. select count(distinct id) from t; # --> 32770 Related Oracle bug reports https://bugs.mysql.com/bug.php?id=60023 |
| Comments |
| Comment by Elena Stepanova [ 2017-09-07 ] | ||||||||||||||||||||||||||
|
MySQL 5.7 shows Using index for group-by with or without the hint:
| ||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2018-11-02 ] | ||||||||||||||||||||||||||
|
It actually does use index for GROUP BY. Reading data through index "PRIMARY" allows to read one GROUP BY group after another, and the optimizer is taking advantage of that. Compare:
Changing the GROUP BY clause so the optimizer doesn't know the index is usable:
| ||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2018-11-02 ] | ||||||||||||||||||||||||||
|
Maybe, the actual question of this bug report was, why is this query not using Loose Scan to resolve the GROUP BY. This is a valid question. I suspect the cost calculations, but let me check that. | ||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2018-11-02 ] | ||||||||||||||||||||||||||
|
The report says
Well, if there are 32K GROUP BY groups with an average of 2 (TWO) rows per group, Loose Scan will not provide any speedup. | ||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2018-11-02 ] | ||||||||||||||||||||||||||
|
And btw if I get the GROUP BY group larger, it will pick Loose Scan:
| ||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2018-11-02 ] | ||||||||||||||||||||||||||
|
Closing as Not a bug. |