Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 30E2D650E7D for ; Wed, 2 Jul 2008 16:07:56 -0300 (ADT) Received: from postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 13057-04 for ; Wed, 2 Jul 2008 16:07:47 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from qb-out-0506.google.com (qb-out-0506.google.com [72.14.204.226]) by postgresql.org (Postfix) with ESMTP id C65AC650EA9 for ; Wed, 2 Jul 2008 16:07:48 -0300 (ADT) Received: by qb-out-0506.google.com with SMTP id c8so1556904qbc.39 for ; Wed, 02 Jul 2008 12:07:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:cc:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references; bh=5OhZEVj2xqVt8xxht+SCNO+q8+yPDjHyzRk/Rnw3mEk=; b=mQtaDwrNGp5LetCzR8WBugbb0VHnBDKr8CUSv4FM+VP7zTCWatRtNwbpOok+YboWTs 9iuknhklIw18aAqVeFSFaFNGoRM+1uNGIpbfD6onv5l6FesvyIidlMbKB5OVO/MS/ndk X3iWTWN6LGHx6eh/opdrBA8V466SkhOBMCw/4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=J2cE4VdaoYaF9LY4gV/vrsj0nwg/nq97fLDTCoRfxVo7bzLT59xXywBnpqlJvL8jHT D27+ZYVugoctwAfjAB3Z1Qoj8DnFz8IeC4LHmvLmmUHiS35RhsW0mod00QEQ8+mecami ZMyYPr2kI4ZZLvxSJYkI18m2lx5Pvc7ExTpaA= Received: by 10.115.78.1 with SMTP id f1mr7348263wal.150.1215025665465; Wed, 02 Jul 2008 12:07:45 -0700 (PDT) Received: by 10.114.180.17 with HTTP; Wed, 2 Jul 2008 12:07:45 -0700 (PDT) Message-ID: Date: Wed, 2 Jul 2008 13:07:45 -0600 From: "Scott Marlowe" To: "samantha mahindrakar" Subject: Re: Select running slow on Postgres Cc: pgsql-performance@postgresql.org In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200807/35 X-Sequence-Number: 30635 On Wed, Jul 2, 2008 at 1:01 PM, samantha mahindrakar wrote: > I ran the explain analyze.Here is what i got: > > > "Group (cost=112266.37..112266.40 rows=1 width=56) (actual > time=5583.399..5615.476 rows=13373 loops=1)" > " -> Sort (cost=112266.37..112266.38 rows=1 width=56) (actual > time=5583.382..5590.890 rows=13373 loops=1)" > " Sort Key: lane_data_07_08.lane_id, > lane_data_07_08.measurement_start, lane_data_07_08.measurement_end, > lane_data_07_08.speed, lane_data_07_08.volume, lane_data_07_08.occupancy, > lane_data_07_08.quality, lane_data_07_08.effective_date" > " -> Nested Loop IN Join (cost=0.00..112266.36 rows=1 width=56) > (actual time=1100.307..5547.768 rows=13373 loops=1)" > " -> Seq Scan on lane_data_07_08 (cost=0.00..112241.52 rows=3 > width=56) (actual time=1087.666..5341.662 rows=20581 loops=1)" You can see here that the seq scan on lane_data is what's eating up all your time. Also, since the row estimate is WAY off, it then chose a nested loop thinking it would be joining up only 1 row and actually running across 20k rows. > " Filter: (((volume = 255::double precision) OR (speed = > 255::double precision) OR (occupancy = 255::double precision) OR (occupancy >>= 100::double precision) OR (volume > 52::double precision) OR (volume < > 0::double precision) OR (speed > 120::double precision) OR (speed < > 0::double precision)) AND (date_part('hour'::text, measurement_start) >= > 5::double precision) AND (date_part('hour'::text, measurement_start) <= > 23::double precision) AND (date_part('day'::text, measurement_start) = > 1::double precision))" > " -> Index Scan using lane_info_pk on > lane_info (cost=0.00..8.27 rows=1 width=4) (actual time=0.007..0.007 rows=1 > loops=20581)" > " Index Cond: (lane_data_07_08.lane_id = > lane_info.lane_id)" > " Filter: (inactive IS NULL)" > "Total runtime: 5621.409 ms" > > > Well instaed of creating extra indexes (since they eat up lot of space) i > made use of the whole measurement_start field, so thet it uses the index > proeprty and makes the search faster. > So i changed the query to include the measuerment start as follows: > > SELECT lane_id,measurement_start, > measurement_end,speed,volume,occupancy,quality,effective_date > FROM tss.lane_data_06_08 > WHERE lane_id in(select lane_id from lane_info where inactive is null ) > AND measurement_start between '2008-06-30 05:00:00-04' AND '2008-06-30 > 23:00:00-04' > GROUP BY > lane_id,measurement_start,measurement_end,speed,volume,occupancy,quality,effective_date > ORDER BY lane_id, measurement_start Yeah, anytime you can just compare date / timestamp on an indexed field you'll do better. If you find yourself needing to use the other syntax, so you can, for instance, grab the data for 5 days in a row from 5am to 11am or something, then the method I mentioned of making indexes on date_part are a good choice. Note that you need regular timestamp, not timstamptz to create indexes.