Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 2B516632712 for ; Wed, 26 May 2010 06:00:48 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 27040-07 for ; Wed, 26 May 2010 09:00:40 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from spinlock.commandprompt.com (host-215.commandprompt.net [207.173.203.215]) by mail.postgresql.org (Postfix) with ESMTP id C56BC6325EF for ; Wed, 26 May 2010 06:00:40 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by spinlock.commandprompt.com (Postfix) with ESMTP id 4C7E6348035; Wed, 26 May 2010 02:00:39 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at commandprompt.com Received: from spinlock.commandprompt.com ([127.0.0.1]) by localhost (spinlock.commandprompt.com.commandprompt.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Zu2RlwWlBjEk; Wed, 26 May 2010 02:00:33 -0700 (PDT) Received: from 92-49-211-42.dynamic.peoplenet.ua (92-49-211-42.dynamic.peoplenet.ua [92.49.211.42]) by spinlock.commandprompt.com (Postfix) with ESMTPSA id 777A1348002; Wed, 26 May 2010 02:00:31 -0700 (PDT) Subject: Re: Random Page Cost and Planner Mime-Version: 1.0 (Apple Message framework v1078) Content-Type: text/plain; charset=us-ascii From: Alexey Klyukin In-Reply-To: Date: Wed, 26 May 2010 12:00:18 +0300 Cc: Tom Lane , Kevin Grittner , pgsql-performance@postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: <1B94E0EC-D665-4059-83DC-5D1B1E2B1009@commandprompt.com> References: <4BFBD0890200002500031A6E@gw.wicourts.gov> <2008.1274833463@sss.pgh.pa.us> To: David Jarvis X-Mailer: Apple Mail (2.1078) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201005/194 X-Sequence-Number: 38778 On May 26, 2010, at 6:50 AM, David Jarvis wrote: >=20 > That said, when using the following condition, the query is fast (1 = second): >=20 > extract(YEAR FROM sc.taken_start) >=3D 1963 AND > extract(YEAR FROM sc.taken_end) <=3D 2009 AND=20 >=20 > " -> Index Scan using measurement_013_stc_idx on = measurement_013 m (cost=3D0.00..511.00 rows=3D511 width=3D15) (actual = time=3D0.018..3.601 rows=3D3356 loops=3D104)" > " Index Cond: ((m.station_id =3D = sc.station_id) AND (m.taken >=3D sc.taken_start) AND (m.taken <=3D = sc.taken_end) AND (m.category_id =3D 7))" >=20 > This condition makes it slow (13 seconds on first run, 8 seconds = thereafter): >=20 > extract(YEAR FROM sc.taken_start) >=3D 1900 AND > extract(YEAR FROM sc.taken_end) <=3D 2009 AND=20 >=20 > " Filter: (category_id =3D 7)" > " -> Seq Scan on measurement_013 m = (cost=3D0.00..359704.80 rows=3D18118464 width=3D15) (actual = time=3D0.008..4025.692 rows=3D18118395 loops=3D1)" >=20 > 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) >=3D 1900 is incorrect and = so it chooses a full table scan for all rows.=20 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 =3D 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. >=20 > Should I add a clustered index by station then by date? >=20 > 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.=20 Regards, -- Alexey Klyukin The PostgreSQL Company - Command Prompt, Inc. =20