Received: from news.tht.net (news.hub.org [216.126.91.242]) by postgresql.org (8.11.3/8.11.4) with ESMTP id fA9Lbem11809 for ; Fri, 9 Nov 2001 16:37:40 -0500 (EST) (envelope-from news@news.tht.net) Received: (from news@localhost) by news.tht.net (8.11.4/8.11.4) id fA9LRCD69279 for pgsql-admin@postgresql.org; Fri, 9 Nov 2001 16:27:12 -0500 (EST) (envelope-from news) From: "Jeff Boes" X-Newsgroups: comp.databases.postgresql.admin Subject: Timestamp indexes aren't used for ">=" Date: Fri, 09 Nov 2001 16:28:46 -0500 Organization: Nexcerpt, Inc. Lines: 86 Message-ID: <9shhnf$23ks$1@news.tht.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Complaints-To: scrappy@hub.org User-Agent: Pan/0.9.90 (Unix) To: pgsql-admin@postgresql.org X-Archive-Number: 200111/38 X-Sequence-Number: 2563 We have a table which has approximately 400,000 rows. It has 17 columns, and 4 indexes. The primary key is a int4 (filled by a sequence), additionally we have two more int4 indexes and a timestamp index. The documentation for create index (http://www.postgresql.org/idocs/index.php?sql-createindex.html) gives a nice query to display the list of known operators for each of the index types. Running this yields the following useful data.... acc_name | ops_name | ops_comp ----------+---------------+---------- btree | timestamp_ops | < btree | timestamp_ops | <= btree | timestamp_ops | = btree | timestamp_ops | = btree | timestamp_ops | > btree | timestamp_ops | >= Now, if this is true.... how can this result be consistant? (selecting by equality with timestamp value) explain select fetch_status, count(*) from stat_fetch where fetch_date = '2001-11-08 00:00:00-05' group by fetch_status; NOTICE: QUERY PLAN: Aggregate (cost=8.05..8.06 rows=1 width=12) -> Group (cost=8.05..8.05 rows=1 width=12) -> Sort (cost=8.05..8.05 rows=1 width=12) -> Index Scan using ix_stat_fetch_3 on stat_fetch (cost=0.00..8.04 rows=1 width=12) EXPLAIN .....BUT..... (selecting by comparison ">=" to timestamp value) explain select fetch_status, count(*) from stat_fetch where fetch_date >= '2001-11-08 00:00:00-05' group by fetch_status; NOTICE: QUERY PLAN: Aggregate (cost=12322.64..12522.06 rows=3988 width=12) -> Group (cost=12322.64..12422.35 rows=39884 width=12) -> Sort (cost=12322.64..12322.64 rows=39884 width=12) -> Seq Scan on stat_fetch (cost=0.00..8917.33 rows=39884 width=12) EXPLAIN .....AND YET..... set enable_seqscan to off; explain select fetch_status, count(*) from stat_fetch where fetch_date >= '2001-11-08 00:00:00-05' group by fetch_status; NOTICE: QUERY PLAN: Aggregate (cost=38193.97..38393.39 rows=3988 width=12) -> Group (cost=38193.97..38293.68 rows=39884 width=12) -> Sort (cost=38193.97..38193.97 rows=39884 width=12) -> Index Scan using ix_stat_fetch_3 on stat_fetch (cost=0.00..34788.66 rows=39884 width=12) EXPLAIN Note the cost of the "Index" scan is actually a higher estimate than the sequential scan. This leads me to the conclusion that either postgres has a bug that is preventing it from actually using the operator that is defined on the index (thus falling back to the non- indexed comparison), or explain is broken, or my understanding of indexes is broken. -- Jeff Boes vox 616.226.9550 Database Engineer fax 616.349.9076 Nexcerpt, Inc. jboes@nexcerpt.com