I have a table of books :
CREATE TABLE `books` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`nameOfBook` VARCHAR(32),
`releaseDate` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `Index 2` (`releaseDate`, `id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=33029692;
I compared two SQL requests to do a pagiation with sort on releaseDate. Both of theses request return the same result.
(simple one)
select SQL_NO_CACHE id,name, releaseDate
from books
where releaseDate <= '2016-11-07'
AND (releaseDate<'2016-11-07' OR id < 3338191)
ORDER by releaseDate DESC, id DESC limit 50;
and
(tuple comparison or row comparaison)
select SQL_NO_CACHE id,name, releaseDate
from books
where (releaseDate ,id) < ('2016-11-07',3338191)
ORDER by releaseDate DESC, id DESC limit 50;
When I do the explain of the request i got this
simple one :
"id";"select_type";"table";"type";"possible_keys";"key";"key_len";"ref";"rows";"Extra"
"1";"SIMPLE";"books";"range";"PRIMARY,Index 2";"Index 2";"9";"";"1015876";"Using where; Using index"
We can see it is parsing "1015876" of rows
The explain for the tuple comparison :
"id";"select_type";"table";"type";"possible_keys";"key";"key_len";"ref";"rows";"Extra"
"1";"SIMPLE";"books";"index";"";"Index 2";"13";"";"50";"Using where; Using index"
We can see it is parsing "50" of rows.
But if I checked the exectution time the simple one :
/* Affected rows: 0 Lignes trouvées: 50 Avertissements: 0 Durée pour 1 query: 0,031 sec. */
and the tuple one :
/* Affected rows: 0 Lignes trouvées: 50 Avertissements: 0 Durée pour 1 query: 3,682 sec. */
I don't understant why according to the explain the tuple comparison is better but the execution time is badly worse?
(releaseDate, id)is effectively the same as an index on justreleaseDatein this case. See Docs, "In InnoDB, each record in a secondary index contains the primary key columns for the row, as well as the columns specified for the secondary index."