Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 9656F9FB696 for ; Mon, 5 Mar 2007 21:27:18 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-new, port 10024) with ESMTP id 70972-05 for ; Mon, 5 Mar 2007 21:27:09 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from wx-out-0506.google.com (wx-out-0506.google.com [66.249.82.234]) by postgresql.org (Postfix) with ESMTP id A259D9FB8CA for ; Mon, 5 Mar 2007 21:27:14 -0400 (AST) Received: by wx-out-0506.google.com with SMTP id i28so1841951wxd for ; Mon, 05 Mar 2007 17:27:13 -0800 (PST) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:mime-version:content-transfer-encoding:message-id:content-type:to:from:subject:date:x-mailer; b=BB90B7x0ClnDPmFLSReQJbrzyCeywuVaWdvHymNm1+I58CiSi27GBPfnLHEdC+jCNa0lHKAYQRubH7hvuYPPIiyKS5wf+Xbm4TiYDqBOsWChg6pBZ2deXgLBy2852thSNSLaEsz6ymYUGkciAdSpIZuUievVIpSCOIX2RpLTyt0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:mime-version:content-transfer-encoding:message-id:content-type:to:from:subject:date:x-mailer; b=PdWtOmyEnxMkIqSD3A6Pq38XEefvmgJGcwyGgDU/5egm8csJfML7NUEmZ09KuGGmRgCLQuZ2wN0z9CwRiUm7I0cRjZBEru+PrprtG5O9WF6YngiLgEIUM9wt8nMrAprKPWX87H8So+Mf0dwMz7QFf5Qy3UrCTiQP1x5fwzoQnCM= Received: by 10.70.32.6 with SMTP id f6mr9315991wxf.1173144433518; Mon, 05 Mar 2007 17:27:13 -0800 (PST) Received: from ?10.0.1.2? ( [65.96.174.157]) by mx.google.com with ESMTP id 7sm10095956wrh.2007.03.05.17.27.12; Mon, 05 Mar 2007 17:27:12 -0800 (PST) Mime-Version: 1.0 (Apple Message framework v752.3) Content-Transfer-Encoding: 7bit Message-Id: <2C84559B-8A4C-4AF7-9137-C8F508FF4E98@gmail.com> Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed To: pgsql-performance@postgresql.org From: Jeff Cole Subject: query slows down after vacuum analyze Date: Mon, 5 Mar 2007 20:27:10 -0500 X-Mailer: Apple Mail (2.752.3) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200703/110 X-Sequence-Number: 23515 Hi, I'm new to tuning PostgreSQL and I have a query that gets slower after I run a vacuum analyze. I believe it uses a Hash Join before the analyze and a Nested Loop IN Join after. It seems the Nested Loop IN Join estimates the correct number of rows, but underestimates the amount of time required. I am curious why the vacuum analyze makes it slower and if that gives any clues as too which parameter I should be tuning. BTW, I know the query could be re-structured more cleanly to remove the sub-selects, but that doesn't seem to impact the performance. thanks, Jeff Welcome to psql 8.1.5, the PostgreSQL interactive terminal. Type: \copyright for distribution terms \h for help with SQL commands \? for help with psql commands \g or terminate with semicolon to execute query \q to quit plm_demo=# explain analyze SELECT count(*) AS count_all FROM symptoms WHERE ( 1=1 and symptoms.id in (select symptom_id from symptom_reports sr where 1=1 and sr.user_id in (select id from users where disease_id=1))) ; QUERY PLAN ------------------------------------------------------------------------ ------------------------------------------------------------------------ ----------------------- Aggregate (cost=366.47..366.48 rows=1 width=0) (actual time=125.093..125.095 rows=1 loops=1) -> Hash Join (cost=362.41..366.38 rows=36 width=0) (actual time=124.162..124.859 rows=106 loops=1) Hash Cond: ("outer".id = "inner".symptom_id) -> Seq Scan on symptoms (cost=0.00..3.07 rows=107 width=4) (actual time=0.032..0.295 rows=108 loops=1) -> Hash (cost=362.25..362.25 rows=67 width=4) (actual time=124.101..124.101 rows=106 loops=1) -> HashAggregate (cost=361.58..362.25 rows=67 width=4) (actual time=123.628..123.854 rows=106 loops=1) -> Hash IN Join (cost=35.26..361.41 rows=67 width=4) (actual time=9.767..96.372 rows=13074 loops=1) Hash Cond: ("outer".user_id = "inner".id) -> Seq Scan on symptom_reports sr (cost=0.00..259.65 rows=13165 width=8) (actual time=0.029..33.359 rows=13074 loops=1) -> Hash (cost=35.24..35.24 rows=11 width=4) (actual time=9.696..9.696 rows=1470 loops=1) -> Bitmap Heap Scan on users (cost=2.04..35.24 rows=11 width=4) (actual time=0.711..6.347 rows=1470 loops=1) Recheck Cond: (disease_id = 1) -> Bitmap Index Scan on users_disease_id_index (cost=0.00..2.04 rows=11 width=0) (actual time=0.644..0.644 rows=2378 loops=1) Index Cond: (disease_id = 1) Total runtime: 134.045 ms (15 rows) plm_demo=# vacuum analyze; VACUUM plm_demo=# analyze; ANALYZE plm_demo=# explain analyze SELECT count(*) AS count_all FROM symptoms WHERE ( 1=1 and symptoms.id in (select symptom_id from symptom_reports sr where 1=1 and sr.user_id in (select id from users where disease_id=1))) ; QUERY PLAN ------------------------------------------------------------------------ ----------------------------------------------------------------- Aggregate (cost=586.47..586.48 rows=1 width=0) (actual time=3441.385..3441.386 rows=1 loops=1) -> Nested Loop IN Join (cost=149.05..586.26 rows=85 width=0) (actual time=54.517..3441.115 rows=106 loops=1) Join Filter: ("outer".id = "inner".symptom_id) -> Seq Scan on symptoms (cost=0.00..3.08 rows=108 width=4) (actual time=0.007..0.273 rows=108 loops=1) -> Hash IN Join (cost=149.05..603.90 rows=13074 width=4) (actual time=0.078..24.503 rows=3773 loops=108) Hash Cond: ("outer".user_id = "inner".id) -> Seq Scan on symptom_reports sr (cost=0.00..258.74 rows=13074 width=8) (actual time=0.003..9.044 rows=3773 loops=108) -> Hash (cost=145.38..145.38 rows=1470 width=4) (actual time=7.608..7.608 rows=1470 loops=1) -> Seq Scan on users (cost=0.00..145.38 rows=1470 width=4) (actual time=0.006..4.353 rows=1470 loops=1) Filter: (disease_id = 1) Total runtime: 3441.452 ms (11 rows)