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 72E5932A518 for ; Mon, 11 Oct 2004 15:14:31 +0100 (BST) 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 31047-03 for ; Mon, 11 Oct 2004 14:14:22 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 7B81C32AEB4 for ; Mon, 11 Oct 2004 15:14:25 +0100 (BST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id i9BEEIxB004866; Mon, 11 Oct 2004 10:14:19 -0400 (EDT) To: Dawid Kuroczko Cc: pgsql-performance@postgresql.org Subject: Re: Views, joins and LIMIT In-reply-to: <758d5e7f04101102543eab8dca@mail.gmail.com> References: <758d5e7f04101102543eab8dca@mail.gmail.com> Comments: In-reply-to Dawid Kuroczko message dated "Mon, 11 Oct 2004 11:54:41 +0200" Date: Mon, 11 Oct 2004 10:14:18 -0400 Message-ID: <4865.1097504058@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=1.1 tagged_above=0.0 required=5.0 tests=MAILTO_TO_SPAM_ADDR X-Spam-Level: * X-Archive-Number: 200410/135 X-Sequence-Number: 8611 Dawid Kuroczko writes: > This I guess would be quite benefitial for VIEWs. :) Have you tried it? regression-# SELECT entry_id,message FROM entries NATURAL JOIN messages ORDER BY entry_id DESC LIMIT 10; QUERY PLAN ----------------------------------------------------------------------------------------------------- Limit (cost=0.00..48.88 rows=10 width=36) -> Nested Loop (cost=0.00..4887.52 rows=1000 width=36) -> Index Scan Backward using entries_pkey on entries (cost=0.00..52.00 rows=1000 width=8) -> Index Scan using messages_pkey on messages (cost=0.00..4.82 rows=1 width=36) Index Cond: ("outer".message_id = messages.message_id) (5 rows) > Other thing that would be, I guess, benefitial for views would be > special handling of lines like this: > SELECT entry_id,message_id FROM entries NATURAL JOIN messages; > Here there is no reason to perform JOIN at all -- the data will not be used. > As above, since entries.message_id IS NOT NULL REFERENCES messages > and messages is UNIQUE (PRIMARY KEY) we are sure there will be one-to-one(*) > mapping between two tables. And since these keys are not used, no need to > waste time and perform JOIN. The bang-for-the-buck ratio on that seems much too low. regards, tom lane