Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 643F72501B7 for ; Mon, 7 Nov 2011 21:56:51 -0400 (AST) 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 71983-01 for ; Tue, 8 Nov 2011 01:56:44 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from mail-vx0-f174.google.com (mail-vx0-f174.google.com [209.85.220.174]) by mail.postgresql.org (Postfix) with ESMTP id A744B2501B6 for ; Mon, 7 Nov 2011 21:56:44 -0400 (AST) Received: by vcbf1 with SMTP id f1so5056vcb.19 for ; Mon, 07 Nov 2011 17:56:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=GD+m8vpdLnQ2UfJm4MziuT/MeeGun7PUrIYj4HUYuco=; b=YSY+4SS8p885Um8RzPCSKwDepH0mJFBb+64ug0SyWuHfIRYp55EfOdsTd3YOHwSkLc SnXIEfKpTRaSsZM5zuR14NkuHzYom+YUGDkEngI6yBbcmQiCUqGG+xGQ/dJqtJcygiDY RwMo65SubmcN06bxFlzOirHJ3BRck0mwSEoRQ= Received: by 10.52.91.237 with SMTP id ch13mr29186993vdb.129.1320717404660; Mon, 07 Nov 2011 17:56:44 -0800 (PST) Received: from jay.local (c-24-63-250-125.hsd1.ma.comcast.net. [24.63.250.125]) by mx.google.com with ESMTPS id hn2sm19616324vdb.14.2011.11.07.17.56.43 (version=SSLv3 cipher=OTHER); Mon, 07 Nov 2011 17:56:44 -0800 (PST) Message-ID: <4EB88C5A.2020704@gmail.com> Date: Mon, 07 Nov 2011 20:56:42 -0500 From: Jay Levitt User-Agent: Postbox 3.0.1 (Macintosh/20111102) MIME-Version: 1.0 To: Kevin Grittner CC: pgsql-performance@postgresql.org Subject: Re: Subquery in a JOIN not getting restricted? References: <4EB84CB4.2010102@gmail.com> <4EB7FEFD0200002500042B1C@gw.wicourts.gov> <4EB80CAA0200002500042B3B@gw.wicourts.gov> <4EB86784.7080209@gmail.com> In-Reply-To: <4EB86784.7080209@gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.899 tagged_above=-10 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001 X-Spam-Level: X-Archive-Number: 201111/105 X-Sequence-Number: 45616 Jay Levitt wrote: > And yep! When I do a CREATE TABLE AS from that view, and add an index on > user_id, it works just as I'd like. Or not. Feel free to kick me back over to pgsql-novice, but I don't get why the GROUP BY in this subquery forces it to scan the entire users table (seq scan here, index scan on a larger table) when there's only one row in users that can match: create table questions ( id int not null primary key, user_id int not null ); insert into questions select generate_series(1,1100), (random()*2000)::int; create table users ( id int not null primary key ); insert into users select generate_series(1, 2000); vacuum freeze analyze; explain analyze select questions.id from questions join ( select u.id from users as u group by u.id ) as s on s.id = questions.user_id where questions.id = 1; Hash Join (cost=42.28..89.80 rows=2 width=4) (actual time=0.857..1.208 rows=1 loops=1) Hash Cond: (u.id = questions.user_id) -> HashAggregate (cost=34.00..54.00 rows=2000 width=4) (actual time=0.763..1.005 rows=2000 loops=1) -> Seq Scan on users u (cost=0.00..29.00 rows=2000 width=4) (actual time=0.003..0.160 rows=2000 loops=1) -> Hash (cost=8.27..8.27 rows=1 width=8) (actual time=0.015..0.015 rows=1 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 1kB -> Index Scan using questions_pkey on questions (cost=0.00..8.27 rows=1 width=8) (actual time=0.012..0.013 rows=1 loops=1) Index Cond: (id = 1) Total runtime: 1.262 ms This is on patched 9.0.5 built earlier today. The real query has aggregates, so it really does need GROUP BY.. I think.. Jay