Solved

Postgres Optimizer not using index scan

Posted on 2004-09-17
3
1,200 Views
Last Modified: 2010-10-05
Postgres sql does not use the index created on the table

select count(cust) from tableA
where
   (col1 = 1)
    and
    (col2 = 2)
    and
    (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?
0
Comment
Question by:aswam1975
  • 2
3 Comments
 
LVL 9

Expert Comment

by:rjkimble
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
where
   (col1 = 1)
    and
    (col2 = 2)
    and
    (col3 > = 0 and col3 <= 100)

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

Expert Comment

by:ctcampbell
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.
0
 
LVL 3

Accepted Solution

by:
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.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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: http://www.postgresql.org/ (http://www.postgresql.org/) 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.
This video discusses moving either the default database or any database to a new volume.

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now