Indexes are bound to the fields that they index in the order in which they are defined in the index. As long as you use the fields in the index, in their left → in the right order, the index can be used for your query. If you skip fields, the index cannot be used. for example, given the following index:
CREATE INDEX ind1 ON foo (bar, baz, qux)
then in cases where offers will be able to use the index:
WHERE bar=1 WHERE bar=1 AND baz=2 WHERE baz=2 AND bar=1 <--same as before WHERE bar=1 AND baz=2 AND qux=3
The order in which indexed fields are used in the query does not matter, you just use them. However, the order that they are defined in the index is crucial. In the following sentences do NOT use an index:
WHERE baz=2 <-- 'bar' not being used WHERE baz=2 AND qux=3 <-- 'bar' again not being used WHERE bar=1 AND qux=3 <-- the index can be partially used to find `bar`, but not qux.
In your two cases, there is nothing wrong with how they are indexed, but it would be slightly more efficient to index as follows:
(STORE_ID, DIST_ID) (DIST_ID)
It makes no sense to index store_id in the second index, because the DBMS can use the first index to process store_id requests. This is not a serious gain, but still ... maintaining the overhead indices for the database, and reducing overhead is always good.
source share