mysql 5.7 é muito mais lento que o mysql 5.6 no sql médio
Estamos atualizando para o mysql 5.7 e apenas descobrimos que é muito mais lento que a sua parte 5.6. Enquanto ambos têm uma configuração quase idêntica, a versão 5.6 executa a maioria dos sqls em milissegundos, enquanto a outra leva cerca de 1 segundo ou mais para um sql de complexo intermediário como o exemplo abaixo.
-- Getting most recent users that are email-verified and not banned
SELECT
`u`.*
FROM
`user` AS `u`
INNER JOIN `user` user_table_alias ON user_table_alias.`id` = `u`.`id`
LEFT JOIN `user_suspend` user_suspend_table_alias ON user_suspend_table_alias.`userId` = `user_table_alias`.`id`
WHERE
(
`user_suspend_table_alias`.`id` IS NULL
)
AND
`user_table_alias`.`emailVerify` = 1
ORDER BY
`u`.`joinStamp` DESC
LIMIT 1, 18
Ambas as tabelas são bem simples e bem indexadas:
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(128) NOT NULL DEFAULT '',
`username` varchar(32) NOT NULL DEFAULT '',
`password` varchar(64) NOT NULL DEFAULT '',
`joinStamp` int(11) NOT NULL DEFAULT '0',
`activityStamp` int(11) NOT NULL DEFAULT '0',
`accountType` varchar(32) NOT NULL DEFAULT '',
`emailVerify` tinyint(2) NOT NULL DEFAULT '0',
`joinIp` int(11) unsigned NOT NULL,
`locationId` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `username` (`username`),
KEY `accountType` (`accountType`),
KEY `joinStamp` (`joinStamp`),
KEY `activityStamp` (`activityStamp`)
) ENGINE=MyISAM AUTO_INCREMENT=89747 DEFAULT CHARSET=utf8 COMMENT='utf8_general_ci';
-- ----------------------------
-- Table structure for user_suspend
-- ----------------------------
DROP TABLE IF EXISTS `user_suspend`;
CREATE TABLE `user_suspend` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userId` int(11) DEFAULT NULL,
`timestamp` int(11) DEFAULT NULL,
`message` text NOT NULL,
`expire` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `userId` (`userId`)
) ENGINE=MyISAM AUTO_INCREMENT=513 DEFAULT CHARSET=utf8;
As tabelas têm cerca de 100K e 1K linhas, respectivamente. Notei dois comportamentos interessantes que gostaria de "corrigir":
A remoção de ORDER BY eleva o tempo de execução de ~ 1,2 segundos para 0,0015 segundos !!O sql não é armazenado em cache pelo mysql 5.7Nota: temos consulta de cache:
MOSTRAR STATUS COMO 'Qcache%'
Qcache_free_blocks 19408
Qcache_free_memory 61782816
Qcache_hits 31437169
Qcache_inserts 2406719
Qcache_lowmem_prunes 133483
Qcache_not_cached 43555
Qcache_queries_in_cache 41691
Qcache_total_blocks 103951
Pesquisei no Google e descobri muitos problemas relatados no 5.7, mas não entendo por que esses comportamentos estranhos nesse sql (ainda existem muitos outros sqls que ficam muito mais lentos no 5.7).
Aqui está o EXPLAIN, sugerido por Neville K:
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE user_table_alias NULL ALL PRIMARY NULL NULL NULL 104801 10.00 Using where; Usingtemporary; Usingfilesort
1 SIMPLE u NULL eq_ref PRIMARY PRIMARY 4 knn.base_user_table_alias.id 1 100.00 NULL
1 SIMPLE user_suspend_table_alias NULL ref userId userId 5 knn.base_user_table_alias.id 1 10.00 Using where;