X-Original-To: pgsql-general@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id B4C0A476315; Tue, 8 Apr 2003 16:42:20 -0400 (EDT) Received: from snoopy.oit.edu (snoopy.OIT.EDU [140.211.135.12]) by postgresql.org (Postfix) with ESMTP id 8FADD476302; Tue, 8 Apr 2003 16:42:19 -0400 (EDT) Received: from cvc.net (purvine-141-62.oit.edu [140.211.141.62]) by snoopy.oit.edu (8.12.9/8.12.9/OIT-4.0) with ESMTP id h38KgEGM010079; Tue, 8 Apr 2003 13:42:14 -0700 Message-ID: <3E93348C.20208@cvc.net> Date: Tue, 08 Apr 2003 13:43:56 -0700 From: Dennis Gearon Reply-To: gearond@cvc.net User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.3b) Gecko/20030210 X-Accept-Language: en-us, ru MIME-Version: 1.0 To: Dann Corbit Cc: Denis , pgsql-performance@postgresql.org, pgsql-general@postgresql.org, pgsql-sql@postgresql.org Subject: Re: Yet Another (Simple) Case of Index not used References: In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, hits=-38.8 required=5.0 tests=BAYES_01,EMAIL_ATTRIBUTION,IN_REP_TO,QUOTED_EMAIL_TEXT, REFERENCES,REPLY_WITH_QUOTES,USER_AGENT_MOZILLA_UA autolearn=ham version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200304/530 X-Sequence-Number: 40595 from mysql manual: ------------------------------------------------------------- "COUNT(*) is optimized to return very quickly if the SELECT retrieves from one table, no other columns are retrieved, and there is no WHERE clause. For example: mysql> select COUNT(*) from student;" ------------------------------------------------------------- A nice little optimization, maybe not possible in a MVCC system. Dann Corbit wrote: >>-----Original Message----- >>From: Denis [mailto:denis@next2me.com] >>Sent: Tuesday, April 08, 2003 12:57 PM >>To: pgsql-performance@postgresql.org; >>pgsql-general@postgresql.org; pgsql-sql@postgresql.org >>Subject: [GENERAL] Yet Another (Simple) Case of Index not used >> >> >>Hi there, >>I'm running into a quite puzzling simple example where the >>index I've created on a fairly big table (465K entries) is >>not used, against all common sense expectations: The query I >>am trying to do (fast) is: >> >>select count(*) from addresses; >> >>This takes more than a second to complete, because, as the >>'explain' command shows me, the index created on 'addresses' >>is not used, and a seq scan is being used. > > > As well it should be. > > >>One would assume >>that the creation of an index would allow the counting of the >>number of entries in a table to be instantanous? > > > Traversing the index to perform the count will definitely make the query > many times slower. > > A general rule of thumb (not sure if it is true with PostgreSQL) is that > if you have to traverse more than 10% of the data with an index then a > full table scan will be faster. This is especially true when there is > highly redundant data in the index fields. If there were an index on > bit data type, and you have half and half 1 and 0, an index scan of the > table will be disastrous. > > To simply scan the table, we will just sequentially read pages until the > data is exhausted. If we follow the index, we will randomly jump from > page to page, defeating the read buffering. > [snip] > > > ---------------------------(end of broadcast)--------------------------- > TIP 6: Have you searched our list archives? > > http://archives.postgresql.org >