X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id A29F8D1B1AB for ; Tue, 13 Jul 2004 17:30:06 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 51588-01 for ; Tue, 13 Jul 2004 20:30:03 +0000 (GMT) Received: from corpmail.sheetz.com (fw.sheetz.com [209.158.1.62]) by svr1.postgresql.org (Postfix) with ESMTP id 15286D1B198 for ; Tue, 13 Jul 2004 17:29:56 -0300 (ADT) Received: from [10.1.3.38] (localhost [127.0.0.1]) by corpmail.sheetz.com (8.11.6+Sun/8.11.6) with ESMTP id i6DKTfa00400 for ; Tue, 13 Jul 2004 16:29:56 -0400 (EDT) Message-ID: <40F44635.6050202@ricker.us> Date: Tue, 13 Jul 2004 16:29:41 -0400 From: Vic Ricker User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7) Gecko/20040616 MultiZilla/1.6.4.0b X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: How do I convice postgres to use an index? Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200407/86 X-Sequence-Number: 18338 I apologize for the following stupid question. I have been doing some searching and haven't found anything really helpful. The problem is that postgres (7.4.2) keeps choosing to do a sequential scan on a table when an index scan would be significantly faster. The queries that I'm using look at daily statistics from events logged by our Checkpoint firewall and generate graphs. Since they are bit complicated, I simplified it to "select count(*) from log where timestamp>='7/12/2004'" for testing. The table looks like this: Column | Type | Modifiers --------------+-----------------------------+----------- loc | integer | src | inet | dst | inet | interface | character varying(10) | direction | character varying(8) | proto | character varying(4) | service | integer | icmp_code | integer | sport | integer | timestamp | timestamp without time zone | rule | character varying(8) | message_info | text | action | character varying(16) | icmp_type | integer | orig | inet | Indexes: "log_dst_key" btree (dst) "log_src_key" btree (src) "log_timestamp_key" btree ("timestamp") To test, I started with vacuum analyze. (My table has approximately 5.8M rows.) fw1=# select count(*) from log where timestamp>='7/12/2004'; count -------- 246763 (1 row) Time: 161199.955 ms fw1=# set enable_seqscan='off'; SET Time: 47.662 ms fw1=# select count(*) from log where timestamp>='7/12/2004'; count -------- 247149 (1 row) Time: 12428.210 ms Notice the execution time differences. The query plan before turning enable_seqscan off looks like this: Aggregate (cost=208963.26..208963.26 rows=1 width=0) -> Seq Scan on log (cost=0.00..208380.89 rows=232948 width=0) Filter: ("timestamp" >= '2004-07-12 00:00:00'::timestamp without time zone) Any suggestions? Thanks, -Vic