Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 3D7C09FA179 for ; Mon, 1 Oct 2007 16:23:08 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 49422-05 for ; Mon, 1 Oct 2007 16:23:04 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from an-out-0708.google.com (an-out-0708.google.com [209.85.132.249]) by postgresql.org (Postfix) with ESMTP id 1473A9FA20B for ; Mon, 1 Oct 2007 16:23:03 -0300 (ADT) Received: by an-out-0708.google.com with SMTP id c36so501558ana for ; Mon, 01 Oct 2007 12:23:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; bh=0KAMHwd2mDQe1cksnSCNLnE40VdUVAemq89xvBxF53M=; b=GpZeMHL2J51O+LWfJnkAsQhJDQZLKWV5OuajnmIXIigTJ0Dtqs8mIinf0mPkEul0QJjIJZC6B9O1/R9bq7FGTlD+p8bwK8ZQq9VuLlHljW6VqMXoFxUGyitk0oL0tda87zGU35uVI50VARThyt0D772qVXb1q5epATYMwNgb0OA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=QfQ0s5F8kENohBeztC0uDwpLIhjR1S/aNcM9a2LbGLFPCtr1ETcdZt/Tjqo+xni2cnjkVF0w+ef18Qv4KqnLb2tVco371aJc0AAAMhlA0Spouq2l1EHtYa1OyZl9WDQXUU+nrHQo3MbdcQSTyPpPU507PxWqJQ4LCqBnHqA+L1I= Received: by 10.114.13.1 with SMTP id 1mr5311122wam.1191266582407; Mon, 01 Oct 2007 12:23:02 -0700 (PDT) Received: by 10.114.197.19 with HTTP; Mon, 1 Oct 2007 12:23:02 -0700 (PDT) Message-ID: <4544e0330710011223q7676fd34mca7e82393510b2ec@mail.gmail.com> Date: Mon, 1 Oct 2007 14:23:02 -0500 From: "Peter Koczan" To: "Tom Lane" Subject: Re: sequence query performance issues Cc: "Richard Huxton" , pgsql-performance In-Reply-To: <4544e0330709281033s5a8c72ccj979f1c02a43188a0@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <4544e0330709271504g364fd042u161966fa448934f6@mail.gmail.com> <46FCB5F3.4080600@archonet.com> <886.1190988979@sss.pgh.pa.us> <4544e0330709281033s5a8c72ccj979f1c02a43188a0@mail.gmail.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200710/1 X-Sequence-Number: 27023 > *light bulb* Ahhhhhhh, that's it. So, I guess the solution is either > to cast the column or wait for 8.3 (which isn't a problem since the > port won't be done until 8.3 is released anyway). Just a quick bit of follow-up: This query works and is equivalent to what I was trying to do (minus the randomization and limiting): => select a.uid from generate_series(1000, 32000) as a(uid) where a.uid::smallint not in (select uid from people where uid is not null); It turns out that this and using coalesce are a wash in terms of performance, usually coming within 10 ms of each other no matter what limit and ordering constraints you put on the queries. Peter => explain analyze select a.uid from generate_series(1000, 32767) as a(uid) where a.uid not in (select coalesce(uid, 0) from people); QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------- Function Scan on generate_series a (cost=718.41..733.41 rows=500 width=4) (actual time=68.742..186.340 rows=26808 loops=1) Filter: (NOT (hashed subplan)) SubPlan -> Seq Scan on people (cost=0.00..702.68 rows=6294 width=2) (actual time=0.025..28.368 rows=6294 loops=1) Total runtime: 286.311 ms (5 rows) => explain analyze select a.uid from generate_series(1000, 32767) as a(uid) where a.uid::smallint not in (select uid from people where uid is not null); QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------- Function Scan on generate_series a (cost=699.34..716.84 rows=500 width=4) (actual time=58.508..177.683 rows=26808 loops=1) Filter: (NOT (hashed subplan)) SubPlan -> Seq Scan on people (cost=0.00..686.94 rows=4958 width=2) (actual time=0.017..23.123 rows=4971 loops=1) Filter: (uid IS NOT NULL) Total runtime: 277.699 ms (6 rows)