Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 5B4CD1337BED for ; Sun, 20 Mar 2011 03:47:30 -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 59516-09 for ; Sun, 20 Mar 2011 06:47:20 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-qy0-f181.google.com (mail-qy0-f181.google.com [209.85.216.181]) by mail.postgresql.org (Postfix) with ESMTP id A949D1337BE9 for ; Sun, 20 Mar 2011 03:47:19 -0300 (ADT) Received: by qyg14 with SMTP id 14so4366228qyg.19 for ; Sat, 19 Mar 2011 23:47:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:content-type:content-transfer-encoding :subject:date:message-id:to:mime-version:x-mailer; bh=1t1CN431l5E4Le8dzaPpbnoxI8193XgeFxcxHV3JYC0=; b=MRSgWO5onTBX+b/mwSYl8ncuUQlPzyQSk9Cj7sFsyGQV4MK0t3EPPXPcKftFvk0fHP Kr/uMnZ9mbclbJeqwspC/Yk6zXO4SXk7x69RC/CbfvxZ7thlUoUvx1y7cQ4hIJrG8maY 0MIWCvV2oTmfdf42Nktp4NFPlpswhX/kRuNxA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:content-type:content-transfer-encoding:subject:date:message-id :to:mime-version:x-mailer; b=tGLVCi4TQtrH50VgqmGJV7YfO1gYioDus+KlhWubrKFYT1a3r7oImGHsxr2ukQiYvb vsoF7/nqY8IIEJbEaBAZXXWsV2/SmcBvB+07tzlKLPOGT3RCuj4GuShMlsr/o/WTMRAa FmErTPLIrPaML1pgDjzchU+PWSyl//pPvqwj0= Received: by 10.224.47.149 with SMTP id n21mr2248416qaf.289.1300603638977; Sat, 19 Mar 2011 23:47:18 -0700 (PDT) Received: from [192.168.1.6] (146-115-76-62.c3-0.nat-ubr5.sbo-nat.ma.cable.rcn.com [146.115.76.62]) by mx.google.com with ESMTPS id e29sm3429462qck.27.2011.03.19.23.47.16 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 19 Mar 2011 23:47:17 -0700 (PDT) From: Adam Tistler Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Subject: Select in subselect vs select = any array Date: Sun, 20 Mar 2011 02:47:15 -0400 Message-Id: <75F42287-42D3-4FD5-AFFA-64B1CE3C0195@gmail.com> To: pgsql-performance@postgresql.org Mime-Version: 1.0 (Apple Message framework v1082) X-Mailer: Apple Mail (2.1082) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.889 tagged_above=-10 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, T_TO_NO_BRKTS_FREEMAIL=0.01 X-Spam-Level: X-Archive-Number: 201103/234 X-Sequence-Number: 42913 I have noticed that SELECT ... =3D ANY(ARRAY(...)) is about twice as = fast as SELECT IN ( ... ). Can anyone explain a reason for this? Results are the bottom and are = reproducible. I can test with other versions if that is necessary. ./configure --prefix=3D/usr/local/pgsql84 --with-openssl --with-perl CentOS release 5.4 (Final) psql (PostgreSQL) 8.4.1 prompt2=3D# select count(*) from nodes; count =20 -------- 754734 (1 row) prompt2=3D# \d nodes Table "public.nodes" Column | Type | = Modifiers =20 = --------------+--------------------------+--------------------------------= --------------------------- node_id | integer | not null default = nextval(('node_id_seq'::text)::regclass) node_type_id | integer | not null template_id | integer | not null timestamp | timestamp with time zone | default = ('now'::text)::timestamp(6) with time zone Indexes: "nodes_pkey" PRIMARY KEY, btree (node_id) "n_node_id_index" btree (node_id) "n_node_type_id_index" btree (node_type_id) "n_template_id_index" btree (template_id) prompt2=3D# select count(*) from nodes where node_id =3D any( = Array(select node_id from nodes limit 100000) ); count =20 -------- 100000 (1 row) Time: 404.530 ms prompt2=3D# select count(*) from nodes where node_id =3D any( = Array(select node_id from nodes limit 100000) ); count =20 -------- 100000 (1 row) Time: 407.316 ms prompt2=3D# select count(*) from nodes where node_id =3D any( = Array(select node_id from nodes limit 100000) ); count =20 -------- 100000 (1 row) Time: 408.728 ms prompt2=3D# select count(*) from nodes where node_id in (select node_id = from nodes limit 100000 ); count =20 -------- 100000 (1 row) Time: 793.840 ms prompt2=3D# select count(*) from nodes where node_id in (select node_id = from nodes limit 100000 ); count =20 -------- 100000 (1 row) Time: 779.137 ms prompt2=3D# select count(*) from nodes where node_id in (select node_id = from nodes limit 100000 ); count =20 -------- 100000 (1 row) Time: 781.820 ms