MySQL Master e Slave com planos de execução muito diferentes
-
19-09-2019 - |
Pergunta
Eu tenho uma consulta complexa do MySQL que se junta a três mesas e se auto-joa uma tabela para si mesma.
Há um mestre e um escravo que possuem dados e índices idênticos. O mestre é uma caixa poderosa em comparação com o escravo, mas a consulta funciona 10x mais rápida no escravo (durante um período de carga leve para o mestre).
Os planos de execução são muito diferentes.
Master execution plan
1, 'SIMPLE', 'table3_', 'const', 'PRIMARY', 'PRIMARY', '12', 'const', 1, 100.00, 'Using temporary; Using filesort'
1, 'SIMPLE', 'table2_', 'ref', 'PRIMARY,FK376E02E910238FCA', 'FK376E02E910238FCA', '13', 'const', 105, 100.00, 'Using where'
1, 'SIMPLE', 'table0_', 'ref', 'FK57012F937DD0DC02,FK57012F9398CD28D0', 'FK57012F9398CD28D0', '13', 'table2_.ID', 1515, 100.00, 'Using where'
1, 'SIMPLE', 'table1_', 'eq_ref', 'PRIMARY,FKE7E81F1ED170D4C9', 'PRIMARY', '8', 'table0_.FK_ID', 1, 100.00, 'Using where'
Slave execution plan
1, 'SIMPLE', 'table3_', 'const', 'PRIMARY', 'PRIMARY', '12', 'const', 1, 100.00, 'Using filesort'
1, 'SIMPLE', 'table1_', 'ref', 'PRIMARY,FKE7E81F1ED170D4C9', 'FKE7E81F1ED170D4C9', '9', 'const', 187398, 100.00, 'Using where'
1, 'SIMPLE', 'table0_', 'ref', 'FK57012F937DD0DC02,FK57012F9398CD28D0', 'FK57012F937DD0DC02', '9', 'table1_.ID', 1, 100.00, 'Using where'
1, 'SIMPLE', 'table2_', 'eq_ref', 'PRIMARY,FK376E02E910238FCA', 'PRIMARY', '12', 'table0_.FK_ID', 1, 100.00, 'Using where'
As tabelas são processadas em pedidos diferentes e o DB mestre usa uma tabela temporária e uma sequência de arquivos, enquanto o escravo usa apenas uma forma de arquivo.
Que fatores poderiam causar planos diferentes com tempos de execução tão diferentes?
ATUALIZAR:
É possível que isso tenha a ver com estatísticas de índice? Planejo executar uma tabela de análise no mestre durante um período de baixo volume. O Index Show mostra cardinalidade muito diferente para algumas das chaves entre mestre e escravo.
Solução
MySQL optimizes queries based upon collected statistics.
Looking at your output you see that they are using different keys, you might have to add key hints or even force keys
FROM table2_ JOIN
should become
FROM table2_ USE KEY('FK376E02E910238FCA') JOIN
Or FORCE KEY
Outras dicas
This looks like a bug in the query optimizer to me. I would report it.
Are both servers on the same version of MySQL?