Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id CCAFE6FED3 for ; Tue, 19 Jun 2012 18:35:14 -0300 (ADT) Received: from mail-gg0-f174.google.com ([209.85.161.174]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Sh64u-0002Gy-2b for pgsql-performance@postgresql.org; Tue, 19 Jun 2012 21:35:14 +0000 Received: by gglu4 with SMTP id u4so5122937ggl.19 for ; Tue, 19 Jun 2012 14:34:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:content-type:content-transfer-encoding:subject:date :message-id:to:mime-version:x-mailer; bh=HFn79OCatx+APBYgZ5UkdUrWE2MM1PsSsteaaV/VhhY=; b=yySadwt9cP+STaL3alpELT8Gq9jszWQE0T/mOsP3EVs87RJpXtNqcRrCspCby9XcD4 QgK1m1SnFbbccguy3xhFZLQI60xDoKFkZRpCx32oxhzzorYX0nrRqAfh4PWhziDWgwBK +PEz7ucbpR1vsF8B7JWJuMfb0T6S11g5QfL2BDwJQ2aGdKp6zzEJGkPjfuS9ugbuZE9z DchS9AduMJCHarJTBarcnmAjDUYnDFmWMyQ1+15is6C3PYHqno6Y06/Cfu6p2uZoKK0r TeYztCTcC1lvDh6bSIOtLl072wRyM5H6K+Hotft37+jpV7JfGyZQ4/big4ov5EmvNmY0 l0vg== Received: by 10.236.170.36 with SMTP id o24mr13346820yhl.40.1340141698746; Tue, 19 Jun 2012 14:34:58 -0700 (PDT) Received: from garin.med.unc.edu (garin.med.unc.edu. [152.19.81.191]) by mx.google.com with ESMTPS id v61sm84357174yhi.17.2012.06.19.14.34.56 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 19 Jun 2012 14:34:57 -0700 (PDT) From: Tim Jacobs Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Subject: Why is a hash join being used? Date: Tue, 19 Jun 2012 17:34:56 -0400 Message-Id: <9D1AF2AE-9605-41B5-8BCC-177B5EF6F1A5@gmail.com> To: pgsql-performance@postgresql.org Mime-Version: 1.0 (Apple Message framework v1278) X-Mailer: Apple Mail (2.1278) X-Pg-Spam-Score: -2.3 (--) X-Archive-Number: 201206/105 X-Sequence-Number: 47131 I am running the following query: SELECT res1.x, res1.y, res1.z FROM test t JOIN residue_atom_coords res1 ON t.struct_id_1 =3D res1.struct_id AND res1.atomno IN (1,2,3,4) AND=20 (res1.seqpos BETWEEN t.pair_1_helix_1_begin AND = t.pair_1_helix_1_end) WHERE t.compare_id BETWEEN 1 AND 10000; The 'test' table is very large (~270 million rows) as is the = residue_atom_coords table (~540 million rows). The number of compare_ids I select in the 'WHERE' clause determines the = join type in the following way: t.compare_id BETWEEN 1 AND 5000; Nested Loop (cost=3D766.52..15996963.12 rows=3D3316307 width=3D24) -> Index Scan using test_pkey on test t (cost=3D0.00..317.20 = rows=3D5372 width=3D24) Index Cond: ((compare_id >=3D 1) AND (compare_id <=3D 5000)) -> Bitmap Heap Scan on residue_atom_coords res1 = (cost=3D766.52..2966.84 rows=3D625 width=3D44) Recheck Cond: ((struct_id =3D t.struct_id_1) AND (seqpos >=3D = t.pair_1_helix_1_begin) AND (seqpos <=3D t.pair_1_helix_1_end) AND = (atomno =3D ANY ('{1,2,3,4}'::integer[]))) -> Bitmap Index Scan on residue_atom_coords_pkey = (cost=3D0.00..766.36 rows=3D625 width=3D0) Index Cond: ((struct_id =3D t.struct_id_1) AND (seqpos >=3D= t.pair_1_helix_1_begin) AND (seqpos <=3D t.pair_1_helix_1_end) AND = (atomno =3D ANY ('{1,2,3,4}'::integer[]))) t.compare_id BETWEEN 1 AND 10000; Hash Join (cost=3D16024139.91..20940899.94 rows=3D6633849 width=3D24) Hash Cond: (t.struct_id_1 =3D res1.struct_id) Join Filter: ((res1.seqpos >=3D t.pair_1_helix_1_begin) AND = (res1.seqpos <=3D t.pair_1_helix_1_end)) -> Index Scan using test_pkey on test t (cost=3D0.00..603.68 = rows=3D10746 width=3D24) Index Cond: ((compare_id >=3D 1) AND (compare_id <=3D 10000)) -> Hash (cost=3D13357564.16..13357564.16 rows=3D125255660 width=3D44)= -> Seq Scan on residue_atom_coords res1 = (cost=3D0.00..13357564.16 rows=3D125255660 width=3D44) Filter: (atomno =3D ANY ('{1,2,3,4}'::integer[])) The nested loop join performs very quickly, whereas the hash join is = incredibly slow. If I disable the hash join temporarily then a nested = loop join is used in the second case and is the query runs much more = quickly. How can I change my configuration to favor the nested join in = this case? Is this a bad idea? Alternatively, since I will be doing = selections like this many times, what indexes can be put in place to = expedite the query without mucking with the query optimizer? I've = already created an index on the struct_id field of residue_atom_coords = (each unique struct_id should only have a small number of rows for the = residue_atom_coords table). Thanks in advance, Tim