Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 5660B7CD25 for ; Mon, 7 Nov 2011 19:19:42 -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 76887-04 for ; Mon, 7 Nov 2011 23:19:35 +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 874F67CD22 for ; Mon, 7 Nov 2011 19:19:35 -0400 (AST) Received: by vcbf1 with SMTP id f1so2617053vcb.19 for ; Mon, 07 Nov 2011 15:19:35 -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=0qWblaYBClQOsJT4i4xjageVPikKsDFU+iD4SXU/Fz8=; b=HKhk1FzGg51VRXS25TdcxKY+2cTp+r7X6uLB+YLT9jsOMpcPtA7oIABRfrGTCQB3fP 1IhWrKT+ynGsYO3FRuVE2Rnp/Rgt2TcfQInBc3ar5ZayKyTNLLKbkLkSBj2zOuE5xENp KR9kPcgJaO4C0RAy+v23ryg895wj6uv7/Bcb8= Received: by 10.52.112.74 with SMTP id io10mr28644402vdb.116.1320707975071; Mon, 07 Nov 2011 15:19:35 -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 bl9sm20026472vdb.15.2011.11.07.15.19.34 (version=SSLv3 cipher=OTHER); Mon, 07 Nov 2011 15:19:34 -0800 (PST) Message-ID: <4EB86784.7080209@gmail.com> Date: Mon, 07 Nov 2011 18:19:32 -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> In-Reply-To: <4EB80CAA0200002500042B3B@gw.wicourts.gov> 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/104 X-Sequence-Number: 45615 Kevin Grittner wrote: > "Kevin Grittner" wrote: > >> If I had made the scores table wider, it might have gone from the >> user table to scores on the index. > > Bah. I just forgot to put an index on scores.user_id. With that > index available it did what you were probably expecting -- seq scan > on questions, nested loop index scan on users, nested loop index > scan on scores. > > You weren't running you test with just a few rows in each table and > expecting the same plan to be generated as for tables with a lot of > rows, were you? No, we're a startup - we only have 2,000 users and 17,000 scores! We don't need test databases yet... But I just realized something I'd completely forgot (or blocked) - scores is a view. And views don't have indexes. The underlying tables are ultimately indexed by user_id, but I can believe that Postgres doesn't think that's a cheap way to do it - especially since we're still using stock tuning settings (I know) so its costs are all screwed up. 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. I've been meaning to persist that view anyway, so that's what I'll do. Thanks for the push in the right direction.. Jay