-- Reproduced with 5.5.17, but not with 5.5.28 DROP TABLE IF EXISTS countbug; CREATE TABLE countbug ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `ts1` timestamp NULL DEFAULT NULL, `ts2` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`), KEY `ts1` (`ts1`), KEY `ts2` (`ts2`) ) ENGINE=InnoDB; DELIMITER // DROP PROCEDURE IF EXISTS countbug_fill // CREATE PROCEDURE countbug_fill(IN rowCount INT UNSIGNED) BEGIN SET @i = 0; START TRANSACTION; REPEAT INSERT INTO countbug (ts1) VALUES (NULL); SET @i = @i + 1; UNTIL @i >= rowCount END REPEAT; COMMIT; END // DELIMITER ; -- Incorrect composite index behaviour only appears with enough rows CALL countbug_fill(300000); SELECT SQL_NO_CACHE COUNT(*) FROM countbug WHERE ts1 IS NULL OR ts2 IS NULL; -- Adding index "on the fly" causes composite index to return invalid value ALTER TABLE countbug ADD KEY `ts1ts2` (`ts1`,`ts2`); SELECT SQL_NO_CACHE COUNT(*) FROM countbug WHERE ts1 IS NULL OR ts2 IS NULL; SELECT SQL_NO_CACHE COUNT(*) FROM countbug IGNORE INDEX (`ts1ts2`) WHERE ts1 IS NULL OR ts2 IS NULL; -- Rebuilding table causes problem too disappear OPTIMIZE TABLE countbug; SELECT SQL_NO_CACHE COUNT(*) FROM countbug WHERE ts1 IS NULL OR ts2 IS NULL;