views:

23

answers:

1

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 :)

+2  A: 

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

nos
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