Bug #6274 | SELECT DISTINCT on a column with few distinct values isn't optimized | ||
---|---|---|---|
Submitted: | 27 Oct 2004 3:49 | Modified: | 8 Feb 2006 12:41 |
Reporter: | Matthias Urlichs | Email Updates: | |
Status: | Closed | Impact on me: | |
Category: | MySQL Server | Severity: | S4 (Feature request) |
Version: | - | OS: | - |
Assigned to: | CPU Architecture: | Any |
[27 Oct 2004 3:49]
Matthias Urlichs
[8 Feb 2006 12:41]
Valeriy Kravchuk
Looks like the appropriate optimization is already implemented in 5.0.x: mysql> explain select distinct(foo) from test\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: test type: range possible_keys: NULL key: foo key_len: 20 ref: NULL rows: 10 Extra: Using index for group-by 1 row in set (0.01 sec) Please, read the manual (http://dev.mysql.com/doc/refman/5.0/en/explain.html): "- Using index for group-by Similar to the Using index way of accessing a table, Using index for group-by indicates that MySQL found an index that can be used to retrieve all columns of a GROUP BY or DISTINCT query without any extra disk access to the actual table. Additionally, the index is used in the most efficient way so that for each group, only a few index entries are read. For details, see Section 7.2.13, “GROUP BY Optimization”."