Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id B648D9FA45F for ; Tue, 6 Mar 2007 11:40:32 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-new, port 10024) with ESMTP id 23180-02 for ; Tue, 6 Mar 2007 11:40:25 -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.235]) by postgresql.org (Postfix) with ESMTP id CBF0C9FB28A for ; Tue, 6 Mar 2007 11:40:25 -0400 (AST) Received: by wx-out-0506.google.com with SMTP id i28so2031617wxd for ; Tue, 06 Mar 2007 07:40:24 -0800 (PST) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:mime-version:in-reply-to:references:content-type:message-id:content-transfer-encoding:from:subject:date:to:x-mailer; b=i0JM5szyJIiqRKcDmWXCLVnqECFQ/gGnLhcnXsH0yD9WhocslpRpCZE+JzU30i07oiZX6DQGZkbLB0ptlSt7qSm1QgjHFRb56BCBenl7WWR+zDPt1qRvfnAPvS4KztiJPJWcdF9v1SPJPLk5C9lw/rIXu1KFpDeBTAt54oO37F4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:mime-version:in-reply-to:references:content-type:message-id:content-transfer-encoding:from:subject:date:to:x-mailer; b=LXOl8bc4RHjqSo/RRppi5uFYLF9TREzibkhmVfrYz9OUf19r8ajqB0gqbPSkJxLa+uzPyGrsEfLyaV06l33qIWwj8sTDXsfoQH2lrPYbmWO27smIF4V/3VPS9h8lJANdEOSMgz8DM1LBw6DkgLmU04DIjeXv4i5tal0oYN9b368= Received: by 10.70.69.11 with SMTP id r11mr12010724wxa.1173195623357; Tue, 06 Mar 2007 07:40:23 -0800 (PST) Received: from ?192.168.1.45? ( [71.243.113.55]) by mx.google.com with ESMTP id h36sm10519574wxd.2007.03.06.07.40.21; Tue, 06 Mar 2007 07:40:22 -0800 (PST) Mime-Version: 1.0 (Apple Message framework v752.3) In-Reply-To: <25081.1173146092@sss.pgh.pa.us> References: <9BAE31C9-93A6-48AE-AFED-3BB1947D13F1@gmail.com> <25081.1173146092@sss.pgh.pa.us> Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <43756010-6EF6-4F91-B84E-E7D29599C3E8@gmail.com> Content-Transfer-Encoding: 7bit From: Jeff Cole Subject: Re: Date: Tue, 6 Mar 2007 10:40:11 -0500 To: pgsql-performance@postgresql.org X-Mailer: Apple Mail (2.752.3) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200703/132 X-Sequence-Number: 23537 On Mar 5, 2007, at 8:54 PM, Tom Lane wrote: > Hm, the cost for the upper nestloop is way less than you would expect > given that the HASH IN join is going to have to be repeated 100+ > times. > I think this must be due to a very low "join_in_selectivity" estimate > but I'm not sure why you are getting that, especially seeing that the > rowcount estimates aren't far off. Can you show us the pg_stats > rows for symptoms.id and symptom_reports.symptom_id? > Hi Tom, thanks for the response. Here are the pg_stats. I think I understand what the stats say, but I don't know what to conclude from them. plm_stage=# select * from pg_stats where tablename = 'symptoms' and attname = 'id'; schemaname | tablename | attname | null_frac | avg_width | n_distinct | most_common_vals | most_common_freqs | histogram_bounds | correlation ------------+-----------+---------+-----------+----------- +------------+------------------+------------------- +-------------------------------------+------------- public | symptoms | id | 0 | 4 | -1 | | | {1,11,24,34,46,57,71,85,95,106,117} | 0.451606 plm_stage=# select * from pg_stats where tablename = 'symptom_reports' and attname = 'symptom_id'; schemaname | tablename | attname | null_frac | avg_width | n_distinct | most_common_vals | most_common_freqs | histogram_bounds | correlation ------------+-----------------+------------+-----------+----------- +------------+------------------------ +----------------------------------------------------------------------- ---------------+-------------------------------------+------------- public | symptom_reports | symptom_id | 0 | 4 | 80 | {3,2,4,1,5,8,9,7,10,6} | {0.094,0.0933333,0.0933333,0.092,0.0913333,0.0903333,0.0866667,0.0843333 ,0.084,0.08} | {12,18,24,30,38,44,51,57,91,91,114} | 0.0955925 And Ismo, I followed your suggestion to re-write the SQL more cleanly, and you are right it was faster, so that is certainly a solution. Although I am still curious why my original query slowed down after the vacuum analyze. In any case, here is the explain analyze from the new query. Compare that to the 3441.452 ms of the old query after the analyze (and 134.045 ms before the analyze): plm_stage=# explain analyze SELECT count(distinct s.id) AS count_all FROM symptoms s ,symptom_reports sr,users u WHERE s.id=sr.symptom_id and sr.user_id=u.id and u.disease_id in (1); QUERY PLAN ------------------------------------------------------------------------ ----------------------------------------------------------------- Aggregate (cost=770.05..770.06 rows=1 width=4) (actual time=176.749..176.751 rows=1 loops=1) -> Hash Join (cost=89.43..737.50 rows=13020 width=4) (actual time=7.762..142.063 rows=13038 loops=1) Hash Cond: ("outer".symptom_id = "inner".id) -> Hash Join (cost=86.09..538.86 rows=13020 width=4) (actual time=7.277..89.293 rows=13038 loops=1) Hash Cond: ("outer".user_id = "inner".id) -> Seq Scan on symptom_reports sr (cost=0.00..257.38 rows=13038 width=8) (actual time=0.003..30.499 rows=13038 loops=1) -> Hash (cost=82.41..82.41 rows=1471 width=4) (actual time=7.261..7.261 rows=1471 loops=1) -> Seq Scan on users u (cost=0.00..82.41 rows=1471 width=4) (actual time=0.006..4.133 rows=1471 loops=1) Filter: (disease_id = 1) -> Hash (cost=3.07..3.07 rows=107 width=4) (actual time=0.469..0.469 rows=107 loops=1) -> Seq Scan on symptoms s (cost=0.00..3.07 rows=107 width=4) (actual time=0.007..0.247 rows=107 loops=1) Total runtime: 176.842 ms (12 rows)