Re: inconsistent/weird index usage

From: Richard Huxton <dev(at)archonet(dot)com>
To: Dustin Sallings <dustin(at)spy(dot)net>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: inconsistent/weird index usage
Date: 2004-10-01 14:43:05
Message-ID: 415D6CF9.60909@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Dustin Sallings wrote:
> 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

Try this with UNION ALL (you know there won't be any duplicates) and
possibly with some limits too:

SELECT * FROM samples_1999 WHERE ts BETWEEN '1999-01-01 00:00:00+00' AND
'1999-12-31 11:59:59+00'
UNION ALL ...

> select
> s.serial as serial_num,
> s.name as name,
> date(ts) as day,
> min(sample) as min_temp,
> avg(sample) as avg_temp,
> stddev(sample) as stddev_temp,
> max(sample) as max_temp
> from
> samples inner join sensors s using (sensor_id)
> where
> ts > current_date - 7
> group by
> serial_num, name, day
> order by
> serial_num, day desc

Try restricting the timestamp too

WHERE
ts BETWEEN (current_date -7) AND current_timestamp

Hopefully that will give the planner enough smarts to know it can skip
most of the sample_200x tables.

--
Richard Huxton
Archonet Ltd

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Jim C. Nasby 2004-10-01 15:13:03 Re: Caching of Queries
Previous Message Tom Lane 2004-10-01 14:38:46 Re: inconsistent/weird index usage