Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

292
Visualizações
Mysql - how to design indexes for a query with WHERE and GROUP BY

I work with MySQL (MyISAM) DB.

I have a query like this:

select c1, c2, c3, MAX(c5) from T where c4=#string_value# group by c1, c2, c3

- c[1-4] are varchar(255)
- c5 is an integer

I've tried two indexes:

  • "c1_c2_c3_idx" for (c1, c2, c3) - that makes query work 3 times slower
  • "c4_idx" for (c4) - that makes query work 5 times faster

Maybe there is a way to create even more fast index?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I see the following index being optimal:

CREATE INDEX idx ON T (c4, c1, c2, c3, c5);

The index begins with c4, which appears in the WHERE clause, which is the first part of the query execution. Next, we add c1, c2, and c3, to cover the GROUP BY clause. After this, we add c5, which will make it much faster for MySQL to find MAX(c5).

Note that the above index can be said to cover the entire query, because MySQL can use the index alone to satisfy the entire query plan, without needing to seek back to the clustered index (table).

over 4 years ago · Santiago Trujillo Relatório

0

The best index should bd based on where condition ...

 create index  idx1 on T ( c4) 

for avoid the access to data table and use only info contained in index you can also try using a redundant index adding the column involved in others clauses

create index  idx1 on T ( c4, c1,c2,c3,c5) 

leaving the where column left most

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda