Does MySQL work with more than one index?

0

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 'empresa_id' column and the other in 'datacad' .

Will MySQL use only one of the two, or is it able to use both? Would it be better to create an index 'empresa_id, datacad' ?

    
asked by anonymous 10.03.2018 / 18:46

1 answer

0

You can add EXPLAIN to the beginning of your query . You will be able to see what the possible keys that the query can use and which one you are using, in your case you are only using índice related to empresa_id , if you want to change you can force the use of índice putting the USE INDEX (nome_do_indice) statement after the table name. To use a% composite% you must create a reference for the two columns. for example:

CREATE INDEX 'empresa_id_data_cad_idx' ON clientes(empresa_id, data_cad);
    
13.03.2018 / 16:11