Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id D5EFD1336473 for ; Sun, 31 Oct 2010 04:01:20 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 64185-03 for ; Sun, 31 Oct 2010 07:01:12 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from eagle.jhcloos.com (eagle.jhcloos.com [207.210.242.212]) by mail.postgresql.org (Postfix) with ESMTP id 4480E133616B for ; Sun, 31 Oct 2010 04:01:12 -0300 (ADT) Received: by eagle.jhcloos.com (Postfix, from userid 10) id A1C2240180; Sun, 31 Oct 2010 07:00:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jhcloos.com; s=eagle; t=1288508470; bh=xqTau3npIj8x1zdiF2HrPiFaf48nZG55xeWV7LGl0BA=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type; b=lroflPiAaPplz7u455Ve2SI3gY1hTP0lvhl0H57sb2XMe33a2MCuh063jaLFi2H7d yCQIA53/7rxLEOrcZ2NYHtSo2JRgeUAmpnKjiaNlmS6aR07VP7S4c3mvgc352ThFMu qQo46ooOCO2NZLW9xmI7AQPifuI6mSKOL0PZKEkI= Received: from carbon (localhost [127.0.0.1]) by carbon.jhcloos.org (Postfix) with ESMTP id DEA771E9B12 for ; Sun, 31 Oct 2010 06:59:20 +0000 (UTC) From: James Cloos To: pgsql-sql@postgresql.org Subject: A more efficient way? User-Agent: Gnus/5.110011 (No Gnus v0.11) Emacs/24.0.50 (gnu/linux) Face: iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAI1J REFUOE+lU9ESgCAIg64P1y+ngUdxhl5H8wFbbM0OmUiEhKkCYaZThXCo6KE5sCbA1DDX3genvO4d eBQgEMaM5qy6uWk4SfBYfdu9jvBN9nSVDOKRtwb+I3epboOsOX5pZbJNsBJFvmQQ05YMfieIBnYX FK2N6dOawd97r/e8RjkTLzmMsiVgrAoEugtviCM3v2WzjgAAAABJRU5ErkJggg== Copyright: Copyright 2009 James Cloos OpenPGP: ED7DAEA6; url=http://jhcloos.com/public_key/0xED7DAEA6.asc OpenPGP-Fingerprint: E9E9 F828 61A4 6EA9 0F2B 63E7 997A 9F17 ED7D AEA6 Date: Sun, 31 Oct 2010 02:59:20 -0400 Message-ID: Lines: 42 MIME-Version: 1.0 Content-Type: text/plain X-Hashcash: 1:30:101031:pgsql-sql@postgresql.org::FyojlQK8eUBe8xGR:000000000000000000000000000000000000QA0cW X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201010/79 X-Sequence-Number: 34730 I've a third-party app which is slower than it ought to be. It does one operation in particular by doing a query to get a list of rows, and then iterates though them to confirm whether it actually wants that row. As you can imagine that is very slow. This query gets the correct data set in one go, but is also slow: select p from m where s = 7 and p not in (select p from m where s != 7); The p column is not unique; the goal is the set of p for which *every* row with a matching p also has s=7. (s and p are both integer columns, if that matters.) That takes about 38 seconds with this (explain analyze) plan: QUERY PLAN ------------------------------------------------------------------------------------------------------------------------ Index Scan using m_5 on m (cost=8141.99..11519.73 rows=1 width=4) (actual time=564.689..37964.163 rows=243 loops=1) Index Cond: (s = 7) Filter: (NOT (SubPlan 1)) SubPlan 1 -> Materialize (cost=8141.99..13586.24 rows=523955 width=4) (actual time=0.003..80.734 rows=523948 loops=243) -> Seq Scan on m (cost=0.00..7413.34 rows=523955 width=4) (actual time=0.023..259.901 rows=523948 loops=1) Filter: (s <> 7) Total runtime: 38121.781 ms The index m_5 is btree (s). Can that be done is a way which requires only 1 loop? My understanding is that: select p from m where s = 7 except select p from m where s != 7; will return the same results as a simple 'select p from m where s = 7', yes? Is there a way to do it with a self join which requires just a single loop? -JimC -- James Cloos OpenPGP: 1024D/ED7DAEA6