0

I got question on index strategies for mysql - mainly when to use a composite index

I have a fairly common relational db scenario, heres my table set up:

Maintable - table consisting of "products" including brandid, merchantid 

So I create a table to store the brands and merchants

Brandtable - brandname, brandid
Merchanttable - merchantname, merchantid

When I query the Maintable I sometimes query for brandid, sometimes for merchantid and sometimes both. What is best to use in this scenario, a single index on each column, or composite index made up of both?

Also if I wanted to include brandname and merchantname in a fulltext search, how would I achieve this?

Cheers :)

1 Answer 1

2

In this case you should use a separate index for each column. MySQL can only use a composite index if your queries involve the leftmost columns in that composite index.

e.g., if you have a composite index of (brandid,merchantid), that index will be considered if you query on brandid or brandid and merchantid. Not if you only query on merchantid

1
  • This is really clear. Im just getting used to the world of indeces and its quite complex to get the head around at first. Cheers for the concise answer :)
    – Ke.
    Commented Feb 15, 2010 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.