February 23, 2009

wrong distinct due to index

Distinct glitch

select distinct foo from table where bar=N can return no rows found where select foo
from table where bar=N returns rows.

http://bugs.mysql.com/bug.php?id=41610

There is something seriously gone wrong in the following explain plan:

mysql> explain select distinct c from distinct_fail where d=4\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: distinct_fail
type: range
possible_keys: NULL
key: foo
key_len: 4
ref: NULL
rows: 13
Extra: Using where; Using index for group-by
1 row in set (0.00 sec)

The index "foo" looks so harmless but when used in a wrong way, can create havoc:

CREATE TABLE `distinct_fail` (
`a` int NOT NULL,
`b` int NOT NULL,
`c` int NOT NULL,
`d` int NOT NULL,
KEY `foo` (`c`,`d`,`a`,`b`),
KEY `bar` (`c`,`a`,`b`,`d`)
) ENGINE=InnoDB;

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.