MySQL Forums
Forum List  »  InnoDB

Re: what is the difference between index (a,b) and (b,a)
Posted by: Benoit St-Jean
Date: March 11, 2017 02:09PM

There is no "one answer fits all" to this. Normally, the column that is the most selective should appear first. But, there are cases where you'd go the other way around to "reuse" the same index (and avoid creating another one). For instance :

Query 1 : SELECT * FROM tablename WHERE a=2 AND b=17
If column A is way more selective than column B, and index on (a, b) makes sense.

But let's say columns A and B more or less have the same selectivity and let's say you have a second query that is excuted often

Query 2: SELECT * FROM tablename WHERE b = 145.

You could add an index on B for this second query. So you'd have 2 indexes, (a,b) and (b). But indexes take place, they take time to update, etc. So in this case, perhaps you'd be better off with a single index that could be used by BOTH queries, namely an index on (b, a).

Options: ReplyQuote

Written By
Re: what is the difference between index (a,b) and (b,a)
March 11, 2017 02:09PM

Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.