pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alexey Klyukin <alexk@commandprompt.com>
To: David Jarvis <thangalin@gmail.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Kevin Grittner <Kevin.Grittner@wicourts.gov>
Cc: pgsql-performance@postgresql.org
Subject: Re: Random Page Cost and Planner
Date: Wed, 26 May 2010 12:00:18 +0300
Message-ID: <1B94E0EC-D665-4059-83DC-5D1B1E2B1009@commandprompt.com> (raw)
In-Reply-To: <AANLkTikpsmzgv6R7yv7DPTlTEmFhmkJhTwOdB3W5tPh1@mail.gmail.com>
References: <AANLkTikS_QvlkYfv5_hbsXFH4SsTh1nP_WoTwRqpUUcz@mail.gmail.com>
	<4BFBD0890200002500031A6E@gw.wicourts.gov>
	<AANLkTin-j4SACaMeJCH4r5ekEWrtuiFqU56gVM1_lIyV@mail.gmail.com>
	<2008.1274833463@sss.pgh.pa.us>
	<AANLkTikpsmzgv6R7yv7DPTlTEmFhmkJhTwOdB3W5tPh1@mail.gmail.com>


On May 26, 2010, at 6:50 AM, David Jarvis wrote:
> 
> That said, when using the following condition, the query is fast (1 second):
> 
>     extract(YEAR FROM sc.taken_start) >= 1963 AND
>     extract(YEAR FROM sc.taken_end) <= 2009 AND 
> 
> "                    ->  Index Scan using measurement_013_stc_idx on measurement_013 m  (cost=0.00..511.00 rows=511 width=15) (actual time=0.018..3.601 rows=3356 loops=104)"
> "                          Index Cond: ((m.station_id = sc.station_id) AND (m.taken >= sc.taken_start) AND (m.taken <= sc.taken_end) AND (m.category_id = 7))"
> 
> This condition makes it slow (13 seconds on first run, 8 seconds thereafter):
> 
>     extract(YEAR FROM sc.taken_start) >= 1900 AND
>     extract(YEAR FROM sc.taken_end) <= 2009 AND 
> 
> "                          Filter: (category_id = 7)"
> "                    ->  Seq Scan on measurement_013 m  (cost=0.00..359704.80 rows=18118464 width=15) (actual time=0.008..4025.692 rows=18118395 loops=1)"
> 
> At this point, I'm tempted to write a stored procedure that iterates over each station category for all the years of each station. My guess is that the planner's estimate for the number of rows that will be returned by extract(YEAR FROM sc.taken_start) >= 1900 is incorrect and so it chooses a full table scan for all rows. 

Nope, it appears that the planner estimate is correct (it estimates 18118464 vs 18118464 real rows). I think what's happening there is  that 18M rows is large enough part of the total table rows that it makes sense to scan it sequentially (eliminating random access costs).  Try SET enable_seqsan = false and repeat the query - there is a chance that the index scan would be even slower.

> The part I am having trouble with is convincing PG to use the index for the station ID and the date range for when the station was active. Each station has a unique ID; the data in the measurement table is ordered by measurement date then by station.
> 
> Should I add a clustered index by station then by date?
> 
> Any other suggestions are very much appreciated.

Is it necessary to get the data as far as 1900 all the time ? Maybe there is a possibility to aggregate results
from the past years if they are constant. 

Regards,
--
Alexey Klyukin <alexk@commandprompt.com>
The PostgreSQL Company - Command Prompt, Inc.
 




view thread (22+ messages)  latest in thread

Message-ID: <1B94E0EC-D665-4059-83DC-5D1B1E2B1009@commandprompt.com>
Permalink:  ../1B94E0EC-D665-4059-83DC-5D1B1E2B1009@commandprompt.com/
Also on:    postgresql.org/message-id/1B94E0EC-D665-4059-83DC-5D1B1E2B1009@commandprompt.com

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-performance@postgresql.org
  Cc: alexk@commandprompt.com, thangalin@gmail.com, tgl@sss.pgh.pa.us, Kevin.Grittner@wicourts.gov
  Subject: Re: Random Page Cost and Planner
  In-Reply-To: <1B94E0EC-D665-4059-83DC-5D1B1E2B1009@commandprompt.com>
  References: <AANLkTikS_QvlkYfv5_hbsXFH4SsTh1nP_WoTwRqpUUcz@mail.gmail.com> <4BFBD0890200002500031A6E@gw.wicourts.gov> <AANLkTin-j4SACaMeJCH4r5ekEWrtuiFqU56gVM1_lIyV@mail.gmail.com> <2008.1274833463@sss.pgh.pa.us> <AANLkTikpsmzgv6R7yv7DPTlTEmFhmkJhTwOdB3W5tPh1@mail.gmail.com> <1B94E0EC-D665-4059-83DC-5D1B1E2B1009@commandprompt.com>

* 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