I have a composite index on two columns in a table. However, the
index is not used in a query that restricts the 2nd column to a
constant. If both columns are linked with columns in other join
tables, the index will be used.
To illustrate it with an example, I have a query like this:
select s.ticker, p.quantity
from stock s, positions p
where s.type_id = 4
and s.stock_id = p.stock_id
There is an index on stock: (stock_id, type_id). In the table stocks,
there are about 100 different type_ids evenly distributed, and ten of
thousand different stock_ids (each stock_id could map to every one of
the 100 type_ids).
From the plan, the above query does not use the index, but the
following query does use the index:
select s.ticker, p.quantity
from stock s, positions p
where s.type_id = p.pos_type_id
and s.stock_id = p.stock_id
The only difference is that in the first query, type_id is constant 4,
while in the second it is linked with another column in the second
table.
The stats are good. Is there anything else that might have caused the
above?
index is not used in a query that restricts the 2nd column to a
constant. If both columns are linked with columns in other join
tables, the index will be used.
To illustrate it with an example, I have a query like this:
select s.ticker, p.quantity
from stock s, positions p
where s.type_id = 4
and s.stock_id = p.stock_id
There is an index on stock: (stock_id, type_id). In the table stocks,
there are about 100 different type_ids evenly distributed, and ten of
thousand different stock_ids (each stock_id could map to every one of
the 100 type_ids).
From the plan, the above query does not use the index, but the
following query does use the index:
select s.ticker, p.quantity
from stock s, positions p
where s.type_id = p.pos_type_id
and s.stock_id = p.stock_id
The only difference is that in the first query, type_id is constant 4,
while in the second it is linked with another column in the second
table.
The stats are good. Is there anything else that might have caused the
above?
Comment