2

I have index on a column, let's say ID (bigint). If I have a query with something like this:

SELECT * 
  FROM table 
 WHERE id = 12345

...it will use index. But when I'm using query like...

SELECT * 
  FROM table 
 WHERE id >= 12345 
   AND id <= 12366

It use sequential scan, which is very slow. Can I force using the ID index?

5
  • possible duplicate of How do I force Postgres to use a particular index? Commented Jul 14, 2011 at 16:07
  • what type of index are you using, btree? Commented Jul 14, 2011 at 16:08
  • Hi Alexius, You are correct. I used hash instead of BTree. Once I change into BTree, it is now using the index. Thanks. Commented Jul 14, 2011 at 16:29
  • What does EXPLAIN ANALYZE yourqueryhere have to say? Commented Jul 14, 2011 at 17:36
  • The explain analyze say it is using seq index. Commented Jul 16, 2011 at 2:10

2 Answers 2

2

It should use the index if the index type is btree and select doesn't fetch more then 30% of all record count (is it true in postgresql as well?) @scott-marlowe says that "..for PostgreSQL the switchover point comes much earlier, somewhere in the 1 to 10% range where it's cheaper to do a sequential scan..".

Try calling REINDEX action maybe?

Sign up to request clarification or add additional context in comments.

2 Comments

No, in PostgreSQL indexes are not "covering" which means that you always have to go to the table whether you use the index or not. So, for PostgreSQL the switchover point comes much earlier, somewhere in the 1 to 10% range where it's cheaper to do a sequential scan.
@ScottMarlowe Is the covering still true with the INCLUDE() keyword? Or am I mistaken on the meaning of covering here?
0

I don't use postgresql, but what you need to do is.

  • Look at the query plan to confirm it's not using any index.
  • In sybase, you can force a query to use a certain index using an "index hint"

Looks like this question gives you exactly what you want. How do I force Postgres to use a particular index?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.