Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 2A1221337B68 for ; Tue, 21 Sep 2010 15:22:02 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 34556-06 for ; Tue, 21 Sep 2010 18:21:55 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mxout-07.mxes.net (mxout-07.mxes.net [216.86.168.182]) by mail.postgresql.org (Postfix) with ESMTP id 3A6981337B59 for ; Tue, 21 Sep 2010 15:21:54 -0300 (ADT) Received: from [192.168.1.104] (unknown [173.165.13.38]) by smtp.mxes.net (Postfix) with ESMTPA id AF50222E1F3; Tue, 21 Sep 2010 14:21:53 -0400 (EDT) Subject: Re: Query much faster with enable_seqscan=0 Mime-Version: 1.0 (Apple Message framework v1081) Content-Type: text/plain; charset=us-ascii From: Ogden In-Reply-To: <20100921180618.GI17152@aart.is.rice.edu> Date: Tue, 21 Sep 2010 13:21:52 -0500 Cc: pgsql-performance@postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: References: <0D5B9ED0-00C3-4EEB-8336-0B2F8A884620@darkstatic.com> <20100921180618.GI17152@aart.is.rice.edu> To: Kenneth Marshall X-Mailer: Apple Mail (2.1081) 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: 201009/126 X-Sequence-Number: 40329 I assume you mean random_page_cost? It is currently set to 4.0 - is it = better to increase or decrease this value? Thank you=20 Ogden On Sep 21, 2010, at 1:06 PM, Kenneth Marshall wrote: > You DB is more than likely cached. You should adjust your > page costs to better reflect reality and then the planner > can make more accurate estimates and then choose the proper > plan. >=20 > Cheers, > Ken >=20 > On Tue, Sep 21, 2010 at 12:32:01PM -0500, Ogden wrote: >> Hello, >>=20 >> I have received some help from the IRC channel, however, the problem = still exists. When running the following query with enable_seqscan set = to 0, it takes less than a second, whereas with it set to 1, the query = returns in 14 seconds. The machine itself has 8GB Ram and is running = PostgreSQL 9.0 on Debian Lenny. The database size is about 7GB.=20 >>=20 >>=20 >> Query: >> SELECT tr.id, tr.sid >> FROM >> test_registration tr, >> INNER JOIN test_registration_result r on (tr.id =3D = r.test_registration_id) >> WHERE. >> = tr.test_administration_id=3D'32a22b12-aa21-11df-a606-96551e8f4e4c'::uuid >> GROUP BY tr.id, tr.sid >>=20 >>=20 >>=20 >> demo=3D# \d test_registration >> Table "public.test_registration" >> Column | Type | = Modifiers =20 >> = ------------------------+-----------------------------+-------------------= ----- >> id | uuid | not null >> sid | character varying(36) | not null >> created_date | timestamp without time zone | not null = default now() >> modified_date | timestamp without time zone | not null >> test_administration_id | uuid | not null >> teacher_number | character varying(15) |=20 >> test_version_id | uuid |=20 >> Indexes: >> "test_registration_pkey" PRIMARY KEY, btree (id) >> "test_registration_sid_key" UNIQUE, btree (sid, = test_administration_id) >> "test_registration_teacher" btree (teacher_number) >> "test_registration_test_id" btree (test_administration_id) >>=20 >> demo=3D# \d test_registration_result >> Table "public.test_registration_result" >> Column | Type | Modifiers=20 >> ----------------------+-----------------------+----------- >> answer | character varying(15) |=20 >> question_id | uuid | not null >> score | double precision |=20 >> test_registration_id | uuid | not null >> Indexes: >> "test_registration_result_pkey" PRIMARY KEY, btree (question_id, = test_registration_id) >> "test_registration_result_answer" btree (test_registration_id, = answer, score) >> "test_registration_result_test" btree (test_registration_id) >>=20 >>=20 >> Explain Analyze: >>=20 >>=20 >> = --------------------------------------------------------------------------= --------------------------------------------------------------------------= ---------------------- >> HashAggregate (cost=3D951169.97..951198.37 rows=3D2840 width=3D25) = (actual time=3D14669.039..14669.843 rows=3D2972 loops=3D1) >> -> Hash Join (cost=3D2988.07..939924.85 rows=3D2249024 width=3D25) = (actual time=3D551.464..14400.061 rows=3D638980 loops=3D1) >> Hash Cond: (r.test_registration_id =3D tr.id) >> -> Seq Scan on test_registration_result r = (cost=3D0.00..681946.72 rows=3D37199972 width=3D16) (actual = time=3D0.015..6073.101 rows=3D37198734 loops=3D1) >> -> Hash (cost=3D2952.57..2952.57 rows=3D2840 width=3D25) = (actual time=3D2.516..2.516 rows=3D2972 loops=3D1) >> Buckets: 1024 Batches: 1 Memory Usage: 160kB >> -> Bitmap Heap Scan on test_registration tr = (cost=3D44.29..2952.57 rows=3D2840 width=3D25) (actual time=3D0.528..1.458= rows=3D2972 loops=3D1) >> Recheck Cond: (test_administration_id =3D = 'e26a165a-c19f-11df-be2f-778af560e5a2'::uuid) >> -> Bitmap Index Scan on = test_registration_test_administration_id (cost=3D0.00..43.58 rows=3D2840 = width=3D0) (actual time=3D0.507..0.507 rows=3D2972 loops=3D1) >> Index Cond: (test_administration_id =3D = 'e26a165a-c19f-11df-be2f-778af560e5a2'::uuid) >> Total runtime: 14670.337 ms >> (11 rows) >>=20 >>=20 >> real 0m14.698s >> user 0m0.000s >> sys 0m0.008s >>=20 >>=20 >> With "set enable_seqscan=3D0;" >>=20 >>=20 >> SET >> = QUERY PLAN = =20 >> = --------------------------------------------------------------------------= --------------------------------------------------------------------------= --------------------------------- >> HashAggregate (cost=3D1225400.19..1225428.59 rows=3D2840 width=3D25) = (actual time=3D748.397..749.160 rows=3D2972 loops=3D1) >> -> Nested Loop (cost=3D0.00..1214155.07 rows=3D2249024 width=3D25) = (actual time=3D0.107..465.165 rows=3D638980 loops=3D1) >> -> Index Scan using test_registration_test_administration_id = on test_registration tr (cost=3D0.00..4413.96 rows=3D2840 width=3D25) = (actual time=3D0.050..1.610 rows=3D2972 loops=3D1) >> Index Cond: (test_administration_id =3D = 'e26a165a-c19f-11df-be2f-778af560e5a2'::uuid) >> -> Index Scan using test_registration_result_answer on = test_registration_result r (cost=3D0.00..416.07 rows=3D792 width=3D16) = (actual time=3D0.019..0.106 rows=3D215 loops=3D2972) >> Index Cond: (r.test_registration_id =3D tr.id) >> Total runtime: 749.745 ms >> (7 rows) >>=20 >>=20 >> real 0m0.759s >> user 0m0.008s >> sys 0m0.000s >>=20 >>=20 >> The following parameters are changed in postgresql.conf and I have = routinely vacuum analyzed the tables and database: >>=20 >> shared_buffers =3D 2048MB=20 >> work_mem =3D 8MB >> maintenance_work_mem =3D 256MB=20 >> wal_buffers =3D 640kB >> random_page_cost =3D 4.0 =20 >> effective_cache_size =3D 7000MB >> default_statistics_target =3D 200=20 >>=20 >>=20 >> free -m: >> total used free shared buffers = cached >> Mem: 8003 7849 153 0 25 = 7555 >> -/+ buffers/cache: 268 7735 >> Swap: 7640 0 7639 >>=20 >>=20 >> Any help would be appreciated. Thank you very much.=20 >>=20 >> Ogden >=20 > --=20 > Sent via pgsql-performance mailing list = (pgsql-performance@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-performance