Postgres Optimizer not using index scan

Posted on 2004-09-17
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 30 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Best database to use for Maps is PostgreSQL. This is an open source database. Comes as a package with most Linux OS. For more info visit the following site: ( This requires some add-o…
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…
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.
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

710 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