X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (neptune.hub.org [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 585D9D1B4AE for ; Mon, 22 Dec 2003 10:52:48 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 97397-01 for ; Mon, 22 Dec 2003 06:52:00 -0400 (AST) Received: from nitrogen.id.pl (nitrogen.id.pl [193.178.214.5]) by svr1.postgresql.org (Postfix) with SMTP id D039AD1B518 for ; Mon, 22 Dec 2003 06:51:26 -0400 (AST) Received: (qmail 5148 invoked by uid 0); 22 Dec 2003 10:51:13 -0000 Received: from unknown (HELO siaco.id.pl) (213.25.114.8) by smtp.id.pl with SMTP; 22 Dec 2003 10:51:12 -0000 Received: (qmail 24417 invoked by uid 1000); 22 Dec 2003 10:51:08 -0000 Resent-From: Ryszard Lach Resent-Date: Mon, 22 Dec 2003 11:51:08 +0100 Resent-Message-ID: <20031222105108.GB23673@siaco.id.pl> Resent-To: pgsql-performance@postgresql.org Date: Mon, 22 Dec 2003 11:39:18 +0100 From: Ryszard Lach To: pgsql-performance@postgresql.org Subject: "select max/count(id)" not using index Message-ID: <20031222103918.GA23673@siaco.id.pl> Reply-To: Ryszard Lach Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-2 Content-Disposition: inline X-My-GPG-Key: echo | mail -s "send key pub" ryszard@lach.name User-Agent: Mutt/1.5.4i X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200312/279 X-Sequence-Number: 5139 Hi. I have a table with 24k records and btree index on column 'id'. Is this normal, that 'select max(id)' or 'select count(id)' causes a sequential scan? It takes over 24 seconds (on a pretty fast machine): => explain ANALYZE select max(id) from ogloszenia; QUERY PLAN ---------------------------------------------------------------------- Aggregate (cost=3511.05..3511.05 rows=1 width=4) (actual time=24834.629..24834.629 rows=1 loops=1) -> Seq Scan on ogloszenia (cost=0.00..3473.04 rows=15204 width=4) (actual time=0.013..24808.377 rows=16873 loops=1) Total runtime: 24897.897 ms Maybe it's caused by a number of varchar fields in this table? However, 'id' column is 'integer' and is primary key. Clustering table on index created on 'id' makes such a queries many faster, but they still use a sequential scan. Richard. -- "First they ignore you. Then they laugh at you. Then they fight you. Then you win." - Mohandas Gandhi.