--disable_warnings
DROP TABLE /*! IF EXISTS */ CC;
DROP TABLE /*! IF EXISTS */ C;
--enable_warnings
CREATE TABLE `CC` (
`pk` int(11) NOT NULL AUTO_INCREMENT,
`col_int_nokey` int(11) DEFAULT NULL,
`col_int_key` int(11) DEFAULT NULL,
`col_time_key` time DEFAULT NULL,
`col_varchar_key` varchar(1) DEFAULT NULL,
`col_varchar_nokey` varchar(1) DEFAULT NULL,
PRIMARY KEY (`pk`),
KEY `col_int_key` (`col_int_key`),
KEY `col_time_key` (`col_time_key`),
KEY `col_varchar_key` (`col_varchar_key`,`col_int_key`)
) ENGINE=MyISAM AUTO_INCREMENT=30 DEFAULT CHARSET=latin1;
INSERT INTO `CC` VALUES (10,7,8,'01:27:35','v','v');
INSERT INTO `CC` VALUES (11,1,9,'19:48:31','r','r');
INSERT INTO `CC` VALUES (12,5,9,'00:00:00','a','a');
INSERT INTO `CC` VALUES (13,3,186,'19:53:05','m','m');
INSERT INTO `CC` VALUES (14,6,NULL,'19:18:56','y','y');
INSERT INTO `CC` VALUES (15,92,2,'10:55:12','j','j');
INSERT INTO `CC` VALUES (16,7,3,'00:25:00','d','d');
INSERT INTO `CC` VALUES (17,NULL,0,'12:35:47','z','z');
INSERT INTO `CC` VALUES (18,3,133,'19:53:03','e','e');
INSERT INTO `CC` VALUES (19,5,1,'17:53:30','h','h');
INSERT INTO `CC` VALUES (20,1,8,'11:35:49','b','b');
INSERT INTO `CC` VALUES (21,2,5,NULL,'s','s');
INSERT INTO `CC` VALUES (22,NULL,5,'06:01:40','e','e');
INSERT INTO `CC` VALUES (23,1,8,'05:45:11','j','j');
INSERT INTO `CC` VALUES (24,0,6,'00:00:00','e','e');
INSERT INTO `CC` VALUES (25,210,51,'00:00:00','f','f');
INSERT INTO `CC` VALUES (26,8,4,'06:11:01','v','v');
INSERT INTO `CC` VALUES (27,7,7,'13:02:46','x','x');
INSERT INTO `CC` VALUES (28,5,6,'21:44:25','m','m');
INSERT INTO `CC` VALUES (29,NULL,4,'22:43:58','c','c');
CREATE TABLE `C` (
`pk` int(11) NOT NULL AUTO_INCREMENT,
`col_int_nokey` int(11) DEFAULT NULL,
`col_int_key` int(11) DEFAULT NULL,
`col_time_key` time DEFAULT NULL,
`col_varchar_key` varchar(1) DEFAULT NULL,
`col_varchar_nokey` varchar(1) DEFAULT NULL,
PRIMARY KEY (`pk`),
KEY `col_int_key` (`col_int_key`),
KEY `col_time_key` (`col_time_key`),
KEY `col_varchar_key` (`col_varchar_key`,`col_int_key`)
) ENGINE=MyISAM AUTO_INCREMENT=21 DEFAULT CHARSET=latin1;
INSERT INTO `C` VALUES (1,NULL,2,'11:28:45','w','w');
INSERT INTO `C` VALUES (2,7,9,'20:25:14','m','m');
INSERT INTO `C` VALUES (3,9,3,'13:47:24','m','m');
INSERT INTO `C` VALUES (4,7,9,'19:24:11','k','k');
INSERT INTO `C` VALUES (5,4,NULL,'15:59:13','r','r');
INSERT INTO `C` VALUES (6,2,9,'00:00:00','t','t');
INSERT INTO `C` VALUES (7,6,3,'15:15:04','j','j');
INSERT INTO `C` VALUES (8,8,8,'11:32:06','u','u');
INSERT INTO `C` VALUES (9,NULL,8,'18:32:33','h','h');
INSERT INTO `C` VALUES (10,5,53,'15:19:25','o','o');
INSERT INTO `C` VALUES (11,NULL,0,'19:03:19',NULL,NULL);
INSERT INTO `C` VALUES (12,6,5,'00:39:46','k','k');
INSERT INTO `C` VALUES (13,188,166,NULL,'e','e');
INSERT INTO `C` VALUES (14,2,3,'00:00:00','n','n');
INSERT INTO `C` VALUES (15,1,0,'13:12:11','t','t');
INSERT INTO `C` VALUES (16,1,1,'04:56:48','c','c');
INSERT INTO `C` VALUES (17,0,9,'19:56:05','m','m');
INSERT INTO `C` VALUES (18,9,5,'19:35:19','y','y');
INSERT INTO `C` VALUES (19,NULL,6,'05:03:03','f','f');
INSERT INTO `C` VALUES (20,4,2,'18:38:59','d','d');
set @@optimizer_switch='subquery_cache=off';
/* cache is off */ SELECT (
SELECT 4
FROM DUAL ) AS field1 , SUM( DISTINCT table1 . `pk` ) AS field2 , (
SELECT MAX( SUBQUERY2_t1 . `col_int_nokey` ) AS SUBQUERY2_field1
FROM ( C AS SUBQUERY2_t1 INNER JOIN C AS SUBQUERY2_t2 ON (SUBQUERY2_t2 . `col_int_key` = SUBQUERY2_t1 . `pk` ) )
WHERE SUBQUERY2_t2 . `col_varchar_nokey` <= table1 . `col_varchar_key` OR SUBQUERY2_t1 . `col_int_nokey` < table1 . `pk` ) AS field3 , table1 . `col_time_key` AS field4 , table1 . `col_int_key` AS field5 , CONCAT ( table2 . `col_varchar_nokey` , table1 . `col_varchar_key` ) AS field6
FROM ( C AS table1 INNER JOIN ( ( C AS table2 LEFT JOIN CC AS table3 ON (table3 . `col_varchar_key` = table2 . `col_varchar_key` ) ) ) ON (table3 . `col_varchar_key` = table2 . `col_varchar_nokey` ) )
WHERE ( table2 . `col_varchar_nokey` NOT IN (
SELECT 'd' UNION
SELECT 'u' ) ) OR table3 . `col_varchar_nokey` <= table1 . `col_varchar_key`
GROUP BY field1, field3, field4, field5, field6
ORDER BY table1 . `col_int_key` , field1, field2, field3, field4, field5, field6
;
set @@optimizer_switch='subquery_cache=on';
/* cache is on */ SELECT (
SELECT 4
FROM DUAL ) AS field1 , SUM( DISTINCT table1 . `pk` ) AS field2 , (
SELECT MAX( SUBQUERY2_t1 . `col_int_nokey` ) AS SUBQUERY2_field1
FROM ( C AS SUBQUERY2_t1 INNER JOIN C AS SUBQUERY2_t2 ON (SUBQUERY2_t2 . `col_int_key` = SUBQUERY2_t1 . `pk` ) )
WHERE SUBQUERY2_t2 . `col_varchar_nokey` <= table1 . `col_varchar_key` OR SUBQUERY2_t1 . `col_int_nokey` < table1 . `pk` ) AS field3 , table1 . `col_time_key` AS field4 , table1 . `col_int_key` AS field5 , CONCAT ( table2 . `col_varchar_nokey` , table1 . `col_varchar_key` ) AS field6
FROM ( C AS table1 INNER JOIN ( ( C AS table2 LEFT JOIN CC AS table3 ON (table3 . `col_varchar_key` = table2 . `col_varchar_key` ) ) ) ON (table3 . `col_varchar_key` = table2 . `col_varchar_nokey` ) )
WHERE ( table2 . `col_varchar_nokey` NOT IN (
SELECT 'd' UNION
SELECT 'u' ) ) OR table3 . `col_varchar_nokey` <= table1 . `col_varchar_key`
GROUP BY field1, field3, field4, field5, field6
ORDER BY table1 . `col_int_key` , field1, field2, field3, field4, field5, field6
;
Test case:
--disable_warnings
DROP TABLE /*! IF EXISTS */ CC;
DROP TABLE /*! IF EXISTS */ C;
--enable_warnings
CREATE TABLE `CC` ( varchar_ nokey` varchar(1) DEFAULT NULL, key`,`col_ int_key` ) '01:27: 35','v' ,'v'); '19:48: 31','r' ,'r'); '00:00: 00','a' ,'a'); '19:53: 05','m' ,'m'); '19:18: 56','y' ,'y'); 2,'10:55: 12','j' ,'j'); '00:25: 00','d' ,'d'); 0,'12:35: 47','z' ,'z'); '19:53: 03','e' ,'e'); '17:53: 30','h' ,'h'); '11:35: 49','b' ,'b'); NULL,'s' ,'s'); 5,'06:01: 40','e' ,'e'); '05:45: 11','j' ,'j'); '00:00: 00','e' ,'e'); 51,'00: 00:00', 'f','f' ); '06:11: 01','v' ,'v'); '13:02: 46','x' ,'x'); '21:44: 25','m' ,'m'); 4,'22:43: 58','c' ,'c'); varchar_ nokey` varchar(1) DEFAULT NULL, key`,`col_ int_key` ) 2,'11:28: 45','w' ,'w'); '20:25: 14','m' ,'m'); '13:47: 24','m' ,'m'); '19:24: 11','k' ,'k'); '15:59: 13','r' ,'r'); '00:00: 00','t' ,'t'); '15:15: 04','j' ,'j'); '11:32: 06','u' ,'u'); 8,'18:32: 33','h' ,'h'); '15:19: 25','o' ,'o'); 0,'19:03: 19',NULL, NULL); '00:39: 46','k' ,'k'); 166,NULL, 'e','e' ); '00:00: 00','n' ,'n'); '13:12: 11','t' ,'t'); '04:56: 48','c' ,'c'); '19:56: 05','m' ,'m'); '19:35: 19','y' ,'y'); 6,'05:03: 03','f' ,'f'); '18:38: 59','d' ,'d');
`pk` int(11) NOT NULL AUTO_INCREMENT,
`col_int_nokey` int(11) DEFAULT NULL,
`col_int_key` int(11) DEFAULT NULL,
`col_time_key` time DEFAULT NULL,
`col_varchar_key` varchar(1) DEFAULT NULL,
`col_
PRIMARY KEY (`pk`),
KEY `col_int_key` (`col_int_key`),
KEY `col_time_key` (`col_time_key`),
KEY `col_varchar_key` (`col_varchar_
) ENGINE=MyISAM AUTO_INCREMENT=30 DEFAULT CHARSET=latin1;
INSERT INTO `CC` VALUES (10,7,8,
INSERT INTO `CC` VALUES (11,1,9,
INSERT INTO `CC` VALUES (12,5,9,
INSERT INTO `CC` VALUES (13,3,186,
INSERT INTO `CC` VALUES (14,6,NULL,
INSERT INTO `CC` VALUES (15,92,
INSERT INTO `CC` VALUES (16,7,3,
INSERT INTO `CC` VALUES (17,NULL,
INSERT INTO `CC` VALUES (18,3,133,
INSERT INTO `CC` VALUES (19,5,1,
INSERT INTO `CC` VALUES (20,1,8,
INSERT INTO `CC` VALUES (21,2,5,
INSERT INTO `CC` VALUES (22,NULL,
INSERT INTO `CC` VALUES (23,1,8,
INSERT INTO `CC` VALUES (24,0,6,
INSERT INTO `CC` VALUES (25,210,
INSERT INTO `CC` VALUES (26,8,4,
INSERT INTO `CC` VALUES (27,7,7,
INSERT INTO `CC` VALUES (28,5,6,
INSERT INTO `CC` VALUES (29,NULL,
CREATE TABLE `C` (
`pk` int(11) NOT NULL AUTO_INCREMENT,
`col_int_nokey` int(11) DEFAULT NULL,
`col_int_key` int(11) DEFAULT NULL,
`col_time_key` time DEFAULT NULL,
`col_varchar_key` varchar(1) DEFAULT NULL,
`col_
PRIMARY KEY (`pk`),
KEY `col_int_key` (`col_int_key`),
KEY `col_time_key` (`col_time_key`),
KEY `col_varchar_key` (`col_varchar_
) ENGINE=MyISAM AUTO_INCREMENT=21 DEFAULT CHARSET=latin1;
INSERT INTO `C` VALUES (1,NULL,
INSERT INTO `C` VALUES (2,7,9,
INSERT INTO `C` VALUES (3,9,3,
INSERT INTO `C` VALUES (4,7,9,
INSERT INTO `C` VALUES (5,4,NULL,
INSERT INTO `C` VALUES (6,2,9,
INSERT INTO `C` VALUES (7,6,3,
INSERT INTO `C` VALUES (8,8,8,
INSERT INTO `C` VALUES (9,NULL,
INSERT INTO `C` VALUES (10,5,53,
INSERT INTO `C` VALUES (11,NULL,
INSERT INTO `C` VALUES (12,6,5,
INSERT INTO `C` VALUES (13,188,
INSERT INTO `C` VALUES (14,2,3,
INSERT INTO `C` VALUES (15,1,0,
INSERT INTO `C` VALUES (16,1,1,
INSERT INTO `C` VALUES (17,0,9,
INSERT INTO `C` VALUES (18,9,5,
INSERT INTO `C` VALUES (19,NULL,
INSERT INTO `C` VALUES (20,4,2,
set @@optimizer_ switch= 'subquery_ cache=off' ;
/* cache is off */ SELECT (
SELECT 4
FROM DUAL ) AS field1 , SUM( DISTINCT table1 . `pk` ) AS field2 , (
SELECT MAX( SUBQUERY2_t1 . `col_int_nokey` ) AS SUBQUERY2_field1
FROM ( C AS SUBQUERY2_t1 INNER JOIN C AS SUBQUERY2_t2 ON (SUBQUERY2_t2 . `col_int_key` = SUBQUERY2_t1 . `pk` ) )
WHERE SUBQUERY2_t2 . `col_varchar_nokey` <= table1 . `col_varchar_key` OR SUBQUERY2_t1 . `col_int_nokey` < table1 . `pk` ) AS field3 , table1 . `col_time_key` AS field4 , table1 . `col_int_key` AS field5 , CONCAT ( table2 . `col_varchar_nokey` , table1 . `col_varchar_key` ) AS field6
FROM ( C AS table1 INNER JOIN ( ( C AS table2 LEFT JOIN CC AS table3 ON (table3 . `col_varchar_key` = table2 . `col_varchar_key` ) ) ) ON (table3 . `col_varchar_key` = table2 . `col_varchar_nokey` ) )
WHERE ( table2 . `col_varchar_nokey` NOT IN (
SELECT 'd' UNION
SELECT 'u' ) ) OR table3 . `col_varchar_nokey` <= table1 . `col_varchar_key`
GROUP BY field1, field3, field4, field5, field6
ORDER BY table1 . `col_int_key` , field1, field2, field3, field4, field5, field6
;
set @@optimizer_ switch= 'subquery_ cache=on' ;
/* cache is on */ SELECT (
SELECT 4
FROM DUAL ) AS field1 , SUM( DISTINCT table1 . `pk` ) AS field2 , (
SELECT MAX( SUBQUERY2_t1 . `col_int_nokey` ) AS SUBQUERY2_field1
FROM ( C AS SUBQUERY2_t1 INNER JOIN C AS SUBQUERY2_t2 ON (SUBQUERY2_t2 . `col_int_key` = SUBQUERY2_t1 . `pk` ) )
WHERE SUBQUERY2_t2 . `col_varchar_nokey` <= table1 . `col_varchar_key` OR SUBQUERY2_t1 . `col_int_nokey` < table1 . `pk` ) AS field3 , table1 . `col_time_key` AS field4 , table1 . `col_int_key` AS field5 , CONCAT ( table2 . `col_varchar_nokey` , table1 . `col_varchar_key` ) AS field6
FROM ( C AS table1 INNER JOIN ( ( C AS table2 LEFT JOIN CC AS table3 ON (table3 . `col_varchar_key` = table2 . `col_varchar_key` ) ) ) ON (table3 . `col_varchar_key` = table2 . `col_varchar_nokey` ) )
WHERE ( table2 . `col_varchar_nokey` NOT IN (
SELECT 'd' UNION
SELECT 'u' ) ) OR table3 . `col_varchar_nokey` <= table1 . `col_varchar_key`
GROUP BY field1, field3, field4, field5, field6
ORDER BY table1 . `col_int_key` , field1, field2, field3, field4, field5, field6
;