X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 0E8549DC819 for ; Thu, 23 Feb 2006 18:12:31 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 81914-02 for ; Thu, 23 Feb 2006 18:12:32 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from r1.mycybernet.net (r1.mycybernet.net [209.5.206.8]) by postgresql.org (Postfix) with ESMTP id 072A39DC801 for ; Thu, 23 Feb 2006 18:12:28 -0400 (AST) Received: from 227-54-222-209.mycybernet.net ([209.222.54.227]:35148 helo=phlogiston.dydns.org) by r1.mycybernet.net with esmtp (Exim 4.50 (FreeBSD)) id 1FCOhI-0003jL-7K for pgsql-sql@postgresql.org; Thu, 23 Feb 2006 17:12:28 -0500 Received: by phlogiston.dydns.org (Postfix, from userid 1000) id CB3CE4040; Thu, 23 Feb 2006 17:12:23 -0500 (EST) Date: Thu, 23 Feb 2006 17:12:23 -0500 From: Andrew Sullivan To: pgsql-sql@postgresql.org Subject: Re: Question about index scan vs seq scan when using count() Message-ID: <20060223221223.GF11392@phlogiston.dyndns.org> References: <18AE59788A3FC640A367E5652E664D800199BEA7@xmb-sjc-237.amer.cisco.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <18AE59788A3FC640A367E5652E664D800199BEA7@xmb-sjc-237.amer.cisco.com> User-Agent: Mutt/1.5.9i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.085 required=5 tests=[AWL=0.085] X-Spam-Score: 0.085 X-Spam-Level: X-Archive-Number: 200602/286 X-Sequence-Number: 24296 On Thu, Feb 23, 2006 at 01:44:43PM -0800, Kashmira Patel (kupatel) wrote: > My understanding of this statement is that if I use count() without a > WHERE clause, then essentially, it is applied to the entire table and > hence requires a seq scan. > But it should not require a seq scan if I have a condition. It may not require it, but it might select it anyway. > For example: I have a table vm_message with an index on column msgid. > Will the following do a sequential scan or an index? > > select count(*) from vm_message where msgid = 3; How much of the table is that? How many rows? EXPLAIN ANALYSE will tell you if you have the right plan (estimate vs. actual). The real question is, are you sure an indexscan is faster? A -- Andrew Sullivan | ajs@crankycanuck.ca It is above all style through which power defers to reason. --J. Robert Oppenheimer