Hello,
I have a table with about 600000 records and two indexes:
CREATE TABLE transactions (
type INT2,
order_id INT4,
account_id INT4,
market_id INT4,
qty INT4,
price INT2,
timestamp INT8,
memo varchar(30));
CREATE INDEX trans_aid_idx ON transactions USING HASH (account_id);
CREATE INDEX trans_oid_idx ON transactions USING HASH (order_id);
When I make a query on an account_id it uses the trans_aid_idx index,
but when I make a query on an order_id it does a sequential scan instead
of using the trans_oid_idx index:
nf=# explain select * from transactions where account_id = 12345;
NOTICE: QUERY PLAN:
Index Scan using trans_aid_idx on transactions (cost=0.00..496.99
rows=139 width=40)
EXPLAIN
nf=# explain select * from transactions where order_id = 12345;
NOTICE: QUERY PLAN:
Seq Scan on transactions (cost=0.00..11490.45 rows=5061 width=40)
EXPLAIN
I have vacuum analyzed the entire DB and just the transactions table, I
have
dropped and recreated the table and the indexes. But it does seem to help.
The weird thing is that this used to work (i.e. trans_oid_idx was used)
when there was about 200000 records (about 1/3 of now). Also, there is
about
9000 distinct values of account_id, but about 300000 values of order_id.
Is there a problem when the number of distinct values grows too large?
(I am running Postgresql 7.1.3 on Redhat 7.1)
Thanks for any help/advice.
--Maurice