Bug #46217 | Wrong result on aggregate + JOIN + ORDER BY + LIMIT | ||
---|---|---|---|
Submitted: | 16 Jul 2009 11:16 | ||
Reporter: | Philip Stoev | Email Updates: | |
Status: | Verified | Impact on me: | |
Category: | MySQL Server: Optimizer | Severity: | S3 (Non-critical) |
Version: | 5.0,5.1,5.4 | OS: | Any |
Assigned to: | CPU Architecture: | Any |
[16 Jul 2009 11:16]
Philip Stoev
[16 Jul 2013 10:55]
Hartmut Holzgraefe
Still reproducible with 5.6.12, requires that the date_time values in the test case are moved into the future due to the change in how TIME<->DATETIME comparisons work (by now using the current date as DATE part for TIME) introduced with 5.6.4 This is also only reproducible with an index on the TIME column and with the 'right' join order: DROP TABLE IF EXISTS C; CREATE TABLE `C` ( `pk` int(11) NOT NULL AUTO_INCREMENT, `time_key` time NOT NULL, `datetime_nokey` datetime NOT NULL, PRIMARY KEY (`pk`), KEY `time_key` (`time_key`) ); INSERT INTO `C` VALUES ( 1,'15:59:14','2020-10-03 15:17:43'); INSERT INTO `C` VALUES ( 2,'04:43:51','2029-04-25 16:10:46'); INSERT INTO `C` VALUES ( 3,'08:22:36','2022-02-13 21:59:10'); INSERT INTO `C` VALUES ( 4,'09:22:36','2022-02-14 21:59:10'); -- see that we get 16 result rows SELECT OUTR .`datetime_nokey` FROM C OUTR2 JOIN C OUTR ON OUTR2 .`time_key` <= OUTR .`datetime_nokey` ; -- now use count -> still 16 SELECT COUNT( OUTR .`datetime_nokey` ) FROM C OUTR2 JOIN C OUTR ON OUTR2 .`time_key` <= OUTR .`datetime_nokey` ; -- now add redundant ORDER BY and LIMIT -> 15 SELECT COUNT( OUTR .`datetime_nokey` ) FROM C OUTR2 JOIN C OUTR ON OUTR2 .`time_key` <= OUTR .`datetime_nokey` ORDER BY OUTR .`pk` LIMIT 1 ; -- the problem is with the join order, if we -- force it either way we get either 15 or 16 SELECT COUNT( OUTR .`datetime_nokey` ) FROM C OUTR2 STRAIGHT_JOIN C OUTR ON OUTR2 .`time_key` <= OUTR .`datetime_nokey` ORDER BY OUTR .`pk` LIMIT 1 ; SELECT COUNT( OUTR .`datetime_nokey` ) FROM C OUTR STRAIGHT_JOIN C OUTR2 ON OUTR2 .`time_key` <= OUTR .`datetime_nokey` ORDER BY OUTR .`pk` LIMIT 1 ; -- now lets drop the index on time_key ALTER TABLE C DROP KEY time_key; -- we will now get a count of 16 in both cases SELECT COUNT( OUTR .`datetime_nokey` ) FROM C OUTR2 STRAIGHT_JOIN C OUTR ON OUTR2 .`time_key` <= OUTR .`datetime_nokey` ORDER BY OUTR .`pk` LIMIT 1 ; SELECT COUNT( OUTR .`datetime_nokey` ) FROM C OUTR STRAIGHT_JOIN C OUTR2 ON OUTR2 .`time_key` <= OUTR .`datetime_nokey` ORDER BY OUTR .`pk` LIMIT 1 ;
[16 Jul 2013 11:00]
Hartmut Holzgraefe
Hmno, i minimized it too much, right now it works on 5.6.12 due to different execution plan chosen ... will redo the test case once more ...
[16 Jul 2013 11:10]
Hartmut Holzgraefe
Ok, ignore me, it is reproducible on 5.1 and 5.5 but not on 5.6.12 anymore ... sorry for the noise ...