Is a composite index also good for queries on the second field?

Bhaskar :

We are using db as mysql and have a table with 6 fields , out of which we have created composite index on two fields let say (A,B). At times when there is query on only field B, the query comes into slow query radar. Are queries on only field B be fully optimized by composite index?

Or We need to create a separate index on field B for better optimization.

Neville Kuyt :

You can see what the query optimizer is doing by using EXPLAIN.

To answer your question - if your index is on (A, B), and your query does not include A in the where clause, the index won't be used.

To understand why, imagine you're building a paper catalogue of your movie collection. Each movie goes onto an index card, and you organise the catalogue by year, and then by film name. This makes it really easy to find all movies that came out in 2019, but finding the movie called "Joker" will require you to go through all the index cards in turn.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=19486&siteId=1