Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 1C9339FB1F4 for ; Mon, 8 Jan 2007 00:58:07 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-new, port 10024) with ESMTP id 45751-01 for ; Mon, 8 Jan 2007 00:58:00 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from smtp113.sbc.mail.re2.yahoo.com (smtp113.sbc.mail.re2.yahoo.com [68.142.229.92]) by postgresql.org (Postfix) with SMTP id D574C9FB1E2 for ; Mon, 8 Jan 2007 00:58:00 -0400 (AST) Received: (qmail 60447 invoked from network); 8 Jan 2007 04:58:00 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=sbcglobal.net; h=Received:X-YMail-OSG:From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Priority:X-MSMail-Priority:X-Mailer:X-MimeOLE:In-Reply-To:Importance; b=Zm8i1iMGvZCKvUufBv3pR9bFKTaiKIu0KKVrcBRNikUpZMiyCVRKN8eMsGFnPa3Ul4qKnD7cegTQXwxwGpo9P44vYuR5gHK7w+RYgePHbCfIZb6Y1lsnq1stCpdhgPss1TYHD8g8TS22oKXMvK4dxlL+LwCsv7JwwNsN8aXP45w= ; Received: from unknown (HELO dualcore) (adam.r@sbcglobal.net@75.2.73.81 with login) by smtp113.sbc.mail.re2.yahoo.com with SMTP; 8 Jan 2007 04:57:59 -0000 X-YMail-OSG: blo_OLkVM1nnvjSBBKV2XTkAF8FO1fkxpOiyBNDN_VlzNVSGZ77jiiBgm5P0IrqzClIZYyl7EScKNFlouUU5V3LGxSLlCBk_zWb7dS2OcboEl72TlGhvC7ukKTArJL9CREbcH7mHEPPOlJl4.c2VKVOeCctfN0Xv6PLHct7t5vm8R0i5bF5YtKaYpJJJ From: "Adam Rich" To: "'Tom Lane'" , "'Joshua D. Drake'" Cc: "'PostgreSQL Performance'" Subject: Re: High update activity, PostgreSQL vs BigDBMS Date: Sun, 7 Jan 2007 22:59:01 -0600 Message-ID: <023401c732e1$b6d34d00$6400a8c0@dualcore> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.2627 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.2826 In-Reply-To: <19873.1168224472@sss.pgh.pa.us> Importance: Normal X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.003 tagged_above=0 required=5 tests=AWL, BAYES_00, DNS_FROM_RFC_ABUSE, DNS_FROM_RFC_POST X-Spam-Level: X-Archive-Number: 200701/93 X-Sequence-Number: 22694 Here's the queries and explains... Granted, it's not a huge difference here, but both timings are absolutely consistent. Using max(), this runs almost 15 queries/sec and "limit 1" runs at almost 40 queries/sec. Is the differene in explain analyze expected behavior? (rows=168196 vs. rows=1) (The table is freshly analayzed) select max(item_id) from receipt_items Result (cost=0.04..0.05 rows=1 width=0) (actual time=0.030..0.031 rows=1 loops=1) InitPlan -> Limit (cost=0.00..0.04 rows=1 width=4) (actual time=0.023..0.024 rows=1 loops=1) -> Index Scan Backward using receipt_items_pkey on receipt_items (cost=0.00..6883.71 rows=168196 width=4) (actual time=0.020..0.020 rows=1 loops=1) Filter: (item_id IS NOT NULL) Total runtime: 0.067 ms select item_id from receipt_items order by item_id desc limit 1 Limit (cost=0.00..0.04 rows=1 width=4) (actual time=0.010..0.011 rows=1 loops=1) -> Index Scan Backward using receipt_items_pkey on receipt_items (cost=0.00..6883.71 rows=168196 width=4) (actual time=0.008..0.008 rows=1 loops=1) Total runtime: 0.026 ms A couple more similar examples from this table: select max(create_date) from receipt_items Result (cost=0.05..0.06 rows=1 width=0) (actual time=0.032..0.032 rows=1 loops=1) InitPlan -> Limit (cost=0.00..0.05 rows=1 width=8) (actual time=0.025..0.026 rows=1 loops=1) -> Index Scan Backward using test_idx_1 on receipt_items (cost=0.00..7986.82 rows=168196 width=8) (actual time=0.022..0.022 rows=1 loops=1) Filter: (create_date IS NOT NULL) Total runtime: 0.069 ms select create_date from receipt_items order by create_date desc limit 1; Limit (cost=0.00..0.05 rows=1 width=8) (actual time=0.011..0.012 rows=1 loops=1) -> Index Scan Backward using test_idx_1 on receipt_items (cost=0.00..7986.82 rows=168196 width=8) (actual time=0.009..0.009 rows=1 loops=1) Total runtime: 0.027 ms -----Original Message----- From: Tom Lane [mailto:tgl@sss.pgh.pa.us] Sent: Sunday, January 07, 2007 8:48 PM To: Adam Rich Cc: 'Craig A. James'; 'Guy Rouillier'; 'PostgreSQL Performance' Subject: Re: [PERFORM] High update activity, PostgreSQL vs BigDBMS "Adam Rich" writes: > I'm using 8.2 and using order by & limit is still faster than MAX() > even though MAX() now seems to rewrite to an almost identical plan > internally. Care to quantify that? AFAICT any difference is within measurement noise, at least for the case of separately-issued SQL commands. > Count(*) still seems to use a full table scan rather than an index scan. Yup. Don't hold your breath for something different. Postgres has made design choices that make certain cases fast and others slow, and count(*) is one case that has come out on the short end of the stick. If that's your most important measure of performance, then indeed you should select a different database that's made different tradeoffs. regards, tom lane