Quantcast
Viewing all articles
Browse latest Browse all 7

MySQL Indexes – Multi-column indexes and order of columns

The problem: Many a times people find that they create index but the query is still slow or the index is not being used by MySQL to fetch the result-set. Mostly the reason is that the index is not created properly, either not the right columns being indexed or the order of columns in the index does not match how its being used in the query. The order of index! What’s that. Well that’s what we will be discussing today. How does the order of column in the index matter? The order of columns in the index matters a lot, an index won’t be used my MySQL if the order of columns in index don’t match with the order in which they are used in the query. Consider the following example: Suppose there is an orders table consisting of order_id and product_id columns and you want to select the product_id for specific order_id(s). The query is as follows: select product_id from orders where order_id in (123, 312, 223, 132, 224); Now have a quick look at what the query is asking mysql to do, its asking MySQL to search by order_id(s) and return the corresponding product_id(s) so the order of

The post MySQL Indexes – Multi-column indexes and order of columns appeared first on ovais.tariq.


Viewing all articles
Browse latest Browse all 7

Trending Articles