Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 963949FB224 for ; Mon, 5 Mar 2007 18:05:50 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-new, port 10024) with ESMTP id 86626-08 for ; Mon, 5 Mar 2007 18:05:47 -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.230]) by postgresql.org (Postfix) with ESMTP id 3DD059FB37C for ; Mon, 5 Mar 2007 18:05:46 -0400 (AST) Received: by wx-out-0506.google.com with SMTP id i28so1793946wxd for ; Mon, 05 Mar 2007 14:05:45 -0800 (PST) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:mime-version:content-type:message-id:content-transfer-encoding:x-mailer:from:subject:date:to; b=Aw4I+VSowlLEUr4IydPQ9bWWhJc7RSEoE8YTnXxLUCSEcpLv6eWjv7j17wwDxRhkMvecgFg6pwA8RxCklAnKbqhYZDs0Qh5Rjk458rmro2FJ0sdFybaB+g94Er8ZU9Clp4KnglrI6lMe8nRBc3geZ9bltyD2xDVm7+gKi7kGZ/8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:mime-version:content-type:message-id:content-transfer-encoding:x-mailer:from:subject:date:to; b=F+IGxb6cwW9yBkCpvKoz4/Ak0gnnl4W0ZHVN2TviVmxonO+9wMgKim3m57ishjwU9/vHQmQ0ySm4kuguASqC37nKUtvenLssJnhPVqsFAYLVajUwMHTN4WvzB++fy6y8c5UTEXx1Zzh2Gj23tgeHj/WJ5NOLeFK6GsKzC0EJm3E= Received: by 10.70.73.12 with SMTP id v12mr6661018wxa.1173132344952; Mon, 05 Mar 2007 14:05:44 -0800 (PST) Received: from ?192.168.1.45? ( [71.243.113.55]) by mx.google.com with ESMTP id i38sm9152283wxd.2007.03.05.14.05.42; Mon, 05 Mar 2007 14:05:42 -0800 (PST) Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <9BAE31C9-93A6-48AE-AFED-3BB1947D13F1@gmail.com> Content-Transfer-Encoding: 7bit X-Mailer: Apple Mail (2.752.3) From: Jeff Cole Subject: Date: Mon, 5 Mar 2007 17:05:41 -0500 To: pgsql-performance@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.705 tagged_above=0 required=5 tests=BAYES_05, MISSING_SUBJECT, SPF_PASS X-Spam-Level: X-Archive-Number: 200703/109 X-Sequence-Number: 23514 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 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)