Solved

Postgres Optimizer not using index scan

Posted on 2004-09-17
3
1,223 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
[X]
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
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

636 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