Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 6F4031DF83C0 for ; Wed, 9 Nov 2011 16:39:24 -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 49624-02 for ; Wed, 9 Nov 2011 20:39:18 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from mail-vw0-f46.google.com (mail-vw0-f46.google.com [209.85.212.46]) by mail.postgresql.org (Postfix) with ESMTP id 0B40A1DF83BF for ; Wed, 9 Nov 2011 16:39:17 -0400 (AST) Received: by vws1 with SMTP id 1so1770020vws.19 for ; Wed, 09 Nov 2011 12:39:17 -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=64OCaQ5q05dYsu9E80dxFKCHi87wkfBJesP00xdhJ5o=; b=JlMAZTJRTLlv26n5xqVcGIW4xTZgBWABVUW7noRR5JqP9yN6rSSBmdUdV4+1/hTftX JriUhwpku0DR3UgEBh48RsaybRk0tgb7ECzTeVCT6+M7k86feEyYfvwE/CGixLNw+sHY /XjxLd5URCju1EDUkZSNwSPSsTqh68aMzU1k8= Received: by 10.52.88.231 with SMTP id bj7mr7338858vdb.81.1320871157312; Wed, 09 Nov 2011 12:39:17 -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 f7sm8813045vdj.13.2011.11.09.12.39.15 (version=SSLv3 cipher=OTHER); Wed, 09 Nov 2011 12:39:16 -0800 (PST) Message-ID: <4EBAE4F2.1090403@gmail.com> Date: Wed, 09 Nov 2011 15:39:14 -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> <4EB88C5A.2020704@gmail.com> <4EBA44B70200002500042BC8@gw.wicourts.gov> In-Reply-To: <4EBA44B70200002500042BC8@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/127 X-Sequence-Number: 45638 Kevin Grittner wrote: > Jay Levitt wrote: > >> 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: > Are you sure there's a plan significantly faster than 1.3 ms? Yep! Watch this: drop schema if exists jaytest cascade; create schema jaytest; set search_path to jaytest; create table questions ( id int not null primary key, user_id int not null ); insert into questions select generate_series(1,1100), (random()*2000000)::int; create table users ( id int not null primary key ); insert into users select generate_series(1, 2000000); 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; ----------------------- Merge Join (cost=8.28..90833.02 rows=1818 width=4) (actual time=888.787..888.790 rows=1 loops=1) Merge Cond: (u.id = questions.user_id) -> Group (cost=0.00..65797.47 rows=2000000 width=4) (actual time=0.017..735.509 rows=1747305 loops=1) -> Index Scan using users_pkey on users u (cost=0.00..60797.47 rows=2000000 width=4) (actual time=0.015..331.990 rows=1747305 loops=1) -> Materialize (cost=8.28..8.29 rows=1 width=8) (actual time=0.013..0.015 rows=1 loops=1) -> Sort (cost=8.28..8.28 rows=1 width=8) (actual time=0.012..0.013 rows=1 loops=1) Sort Key: questions.user_id Sort Method: quicksort Memory: 25kB -> Index Scan using questions_pkey on questions (cost=0.00..8.27 rows=1 width=8) (actual time=0.006..0.006 rows=1 loops=1) Index Cond: (id = 1) Total runtime: 888.832 ms (11 rows) explain analyze select questions.id from questions join ( select u.id from users as u ) as s on s.id = questions.user_id where questions.id = 1; ----------------------- Nested Loop (cost=0.00..16.77 rows=1 width=4) (actual time=0.019..0.021 rows=1 loops=1) -> Index Scan using questions_pkey on questions (cost=0.00..8.27 rows=1 width=8) (actual time=0.008..0.009 rows=1 loops=1) Index Cond: (id = 1) -> Index Scan using users_pkey on users u (cost=0.00..8.49 rows=1 width=4) (actual time=0.007..0.007 rows=1 loops=1) Index Cond: (u.id = questions.user_id) Total runtime: 0.045 ms (6 rows) > That said, there might be some room for an optimization which pushes > that test into the query with the "group by" clause. I don't know > if there's a problem with that which I'm missing, the construct was > judged to be too rare to be worth the cost of testing for it, or > it's just that nobody has yet gotten to it. Anyone have more insights on whether this is hard to optimize or simply not-yet-optimized? And if the latter, where might I start looking? (Not that you -really- want me to submit a patch; my C has regressed to the "try an ampersand. OK, try an asterisk." level...) Jay