pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Dustin Sallings <dustin@spy.net>
Cc: pgsql-performance@postgresql.org
Subject: Re: inconsistent/weird index usage
Date: Fri, 01 Oct 2004 10:38:46 -0400
Message-ID: <9345.1096641526@sss.pgh.pa.us> (raw)
In-Reply-To: <6FA9AB99-1373-11D9-A87D-000A957659CC@spy.net>
References: <6FA9AB99-1373-11D9-A87D-000A957659CC@spy.net>

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



view thread (8+ messages)  latest in thread

Message-ID: <9345.1096641526@sss.pgh.pa.us>
Permalink:  ../9345.1096641526@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/9345.1096641526@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: tgl@sss.pgh.pa.us, dustin@spy.net
  Subject: Re: inconsistent/weird index usage
  In-Reply-To: <9345.1096641526@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox