Does Mysql work with more than one index?

Asked

Viewed 449 times

1

If I have a query in a table:

SELECT * FROM clientes WHERE empresa_id=10 AND datacad='2017-01-01'

And two indexes, one in the column 'empresa_id' and another in 'datacad'.

Mysql will use only one of the two, or is able to use both?
It would be better to create an index 'empresa_id, datacad' ?

  • Only empresa_id is index, Where clause does not only work with index

1 answer

1

You can add the EXPLAIN at the beginning of his query. You’ll be able to see what’s possible keys which query you can use and which one you are using, in your case you are using only the índice related to empresa_id, if you want to trade you can force the use of a índiceplacing after the table name the instruction USE INDEX (nome_do_indice). To use a índicecompound you must create a reference to the two columns. for example:

CREATE INDEX `empresa_id_data_cad_idx` ON clientes(empresa_id, data_cad);
  • Thank you very much Arllon, perfect explanation!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.