Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Postgres Optimizer not using index scan

Posted on 2004-09-17
Medium Priority
Last Modified: 2010-10-05
Postgres sql does not use the index created on the table

select count(cust) from tableA
   (col1 = 1)
    (col2 = 2)
    (col3 betwee 0 and 100)

Index created on col1,col2,col2 and(col1,col2,col3) in that order

Table A has over a million records

The explain plan shows that a Sequential Scan is being done on tableA
Why are the indexes not being used?
Question by:aswam1975
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Expert Comment

ID: 12090532
First, make sure that your indexes are up to date by running the following commands:

vacuum full analyze tableA;
reindex tableA;

If that doesn't fix things, try this query instead:

select count(cust) from tableA
   (col1 = 1)
    (col2 = 2)
    (col3 > = 0 and col3 <= 100)

If that uses the index, then I'd say that the PostgreSQL optimizer needs work.

Expert Comment

ID: 12528436
There could be a couple of reasons for that.

First, it appears that PostgreSQL can only use one index per table.  This is different from MS SQL Server which can actually join two or more indexes to speed processing.  What this means is that you must have a single index that matches (or partially matches) your WHERE conditions.  If you have an index on (col1, col2, col3) then you've got that covered.

The second possibility is index selectivity.  If col1 is not very selective (i.e. only contains a small percentage of distinct values), then the optimizer will not use the index.  This is because an index scan is more intensive, per row, than a table scan (postgres has to read the index page and then read the data pointed to by the index, like a bookmark lookup in SQL Server, I think).  If the index selective enough, then the extra time is offset by reading (much) fewer rows.  Thus, create your index with the most selective (i.e. most unique) column first.

Accepted Solution

ctcampbell earned 90 total points
ID: 12528479
Slight correction...  if the first index column is not selective for the WHERE clause, the optimizer MAY or MAY NOT use the index.  It depends on the actual criteria.  The more selective your index, the more likely it is to be used.  There is no guarantee your index will be used at all, if the optimizer doesn't think it will help.  This is normally more true for small tables that fit entirely in a single data page (or just a few), since the whole pages are read anyway.

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : All lightning effects with instructions : http://www.mediaf…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question