X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 277C952801 for ; Tue, 28 Jun 2005 13:26:19 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 67641-04 for ; Tue, 28 Jun 2005 16:26:09 +0000 (GMT) Received: from sccmmhc91.asp.att.net (sccmmhc91.asp.att.net [204.127.203.211]) by svr1.postgresql.org (Postfix) with ESMTP id 13373529E4 for ; Tue, 28 Jun 2005 13:26:05 -0300 (ADT) Received: from juju.arbash-meinel.com ([12.214.18.81]) by sccmmhc91.asp.att.net (sccmmhc91) with ESMTP id <20050628162602m9100ngemie>; Tue, 28 Jun 2005 16:26:03 +0000 Received: by juju.arbash-meinel.com (Postfix, from userid 505) id E9FB55606A; Tue, 28 Jun 2005 11:26:01 -0500 (CDT) Received: from [192.168.1.11] (mail [192.168.1.1]) by juju.arbash-meinel.com (Postfix) with ESMTP id 69E4755FB6; Tue, 28 Jun 2005 11:25:57 -0500 (CDT) Message-ID: <42C17A14.3040100@arbash-meinel.com> Date: Tue, 28 Jun 2005 11:25:56 -0500 From: John A Meinel User-Agent: Mozilla Thunderbird 1.0.2 (Windows/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Merlin Moncure Cc: Postgresql Performance , elein Subject: Re: tricky query References: <6EE64EF3AB31D5448D0007DD34EEB3415C2BFF@Herge.rcsinc.local> In-Reply-To: <6EE64EF3AB31D5448D0007DD34EEB3415C2BFF@Herge.rcsinc.local> X-Enigmail-Version: 0.92.0.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enigCEEFCB2A271606AE3B38C834" X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.051 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200506/568 X-Sequence-Number: 13205 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enigCEEFCB2A271606AE3B38C834 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Merlin Moncure wrote: >John Meinel wrote: > > >>See my follow up post, which enables an index scan. On my system with >>90k rows, it takes no apparent time. >>(0.000ms) >>John >>=:-> >> >> > >Confirmed. Hats off to you, the above some really wicked querying. >IIRC I posted the same question several months ago with no response and >had given up on it. I think your solution (smallest X1 not in X) is a >good candidate for general bits, so I'm passing this to varlena for >review :) > >SELECT t1.id+1 as id_new FROM id_test t1 > WHERE NOT EXISTS > (SELECT t2.id FROM id_test t2 WHERE t2.id = t1.id+1) > ORDER BY t1.id LIMIT 1; > >Merlin > > Just be aware that as your table fills it's holes, this query gets slower and slower. I've been doing some testing. And it starts at 0.00 when the first entry is something like 3, but when you start getting to 16k it starts taking more like 200 ms. So it kind of depends how your table fills (and empties I suppose). The earlier query was slower overall (since it took 460ms to read in the whole table). I filled up the table such that 63713 is the first empty space, and it takes 969ms to run. So actually if your table is mostly full, the first form is better. But if you are going to have 100k rows, with basically random distribution of empties, then the NOT EXISTS works quite well. Just be aware of the tradeoff. I'm pretty sure the WHERE NOT EXISTS will always use a looping structure, and go through the index in order. John =:-> --------------enigCEEFCB2A271606AE3B38C834 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (Cygwin) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFCwXoUJdeBCYSNAAMRAkjLAKDEQ2oVyUsg5sIGaRNZKMNRqfb0SQCdF0Xo JH6KkY4FxhHyCJCYsDm1bZk= =WkpU -----END PGP SIGNATURE----- --------------enigCEEFCB2A271606AE3B38C834--