Re: inconsistent/weird index usage - Mailing list pgsql-performance

From Tom Lane
Subject Re: inconsistent/weird index usage
Date
Msg-id 9345.1096641526@sss.pgh.pa.us
Whole thread Raw
In response to inconsistent/weird index usage  (Dustin Sallings <dustin@spy.net>)
Responses Re: inconsistent/weird index usage
List pgsql-performance
Dustin Sallings <dustin@spy.net> writes:
>     The following view creates the illusion of the old ``single-table''
> model:

> create view samples as
>      select * from samples_1999
>   union  select * from samples_2000
>   union  select * from samples_2001
>   union  select * from samples_2002
>   union  select * from samples_2003
>   union  select * from samples_2004

You really, really, really want to use UNION ALL not UNION here.

>     OK, now that that's over with, I have this one particular query that I
> attempt to run for a report from my phone that no longer works because
> it tries to do a table scan on *some* of the tables.  Why it chooses
> this table scan, I can't imagine.

Most of the problem here comes from the fact that "current_date - 7"
isn't reducible to a constant and so the planner is making bad guesses
about how much of each table will be scanned.  If possible, do the date
arithmetic on the client side and send over a simple literal constant.
If that's not practical you can fake it with a mislabeled IMMUTABLE
function --- see the list archives for previous discussions of the
same issue.

            regards, tom lane

pgsql-performance by date:

Previous
From: John Meinel
Date:
Subject: Re: inconsistent/weird index usage
Next
From: Richard Huxton
Date:
Subject: Re: inconsistent/weird index usage