X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 43831476195 for ; Mon, 3 Mar 2003 16:16:36 -0500 (EST) Received: from postgresql.org ([64.49.215.8]) by localhost (postgresql.org [64.49.215.8:10024]) (amavisd-new) with SMTP id 01954-02 for ; Mon, 3 Mar 2003 16:16:23 -0500 (EST) Received: from volt.beyondtheweb.com (unknown [64.141.17.13]) by postgresql.org (Postfix) with ESMTP id 3F7D147632C for ; Mon, 3 Mar 2003 16:05:20 -0500 (EST) Received: from nachbaur.com (h64-141-16-15.sunwave.net [64.141.16.15]) by volt.beyondtheweb.com (8.11.6/8.11.6) with ESMTP id h23L57B02336 for ; Mon, 3 Mar 2003 13:05:07 -0800 Date: Mon, 3 Mar 2003 13:05:22 -0800 Mime-Version: 1.0 (Apple Message framework v551) Content-Type: text/plain; charset=ISO-8859-1; format=flowed Subject: Forcing query to use an index From: Michael Nachbaur To: pgsql-sql@postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: X-Mailer: Apple Mail (2.551) X-Virus-Scanned: by amavisd-new X-Archive-Number: 200303/91 X-Sequence-Number: 12131 Hello everyone, I have a search query that does a whole bunch of LEFT OUTER JOINs=20 between multiple tables, since this is a generic search and some=20 records may not exist for certain customers (e.g. searching for the=20 text "kate" should bring up people whose customer name, street address=20 or email addresses match that word). This is for an ISP's customer=20 management database. Unfortunately one stage in the query keeps using a sequence scan rather=20 than the index. Here is the "EXPLAIN ANALYZE" results for the 115 line=20 SQL query. Sort (cost=3D6666.08..6666.08 rows=3D268 width=3D265) (actual=20 time=3D949.00..949.00 rows=3D1 loops=3D1) -> Aggregate (cost=3D6487.84..6655.27 rows=3D268 width=3D265) (actual= =20 time=3D948.86..948.86 rows=3D1 loops=3D1) -> Group (cost=3D6487.84..6648.58 rows=3D2679 width=3D265) (actu= al=20 time=3D948.70..948.70 rows=3D1 loops=3D1) -> Sort (cost=3D6487.84..6487.84 rows=3D2679 width=3D265)= =20 (actual time=3D948.66..948.66 rows=3D1 loops=3D1) -> Merge Join (cost=3D6106.42..6335.30 rows=3D2679= =20 width=3D265) (actual time=3D859.77..948.06 rows=3D1 loops=3D1) -> Merge Join (cost=3D6101.24..6319.77=20 rows=3D2679 width=3D247) (actual time=3D554.11..674.17 rows=3D2679 loops=3D= 1) -> Index Scan using customer_id_key on=20 customer c (cost=3D0.00..129.63 rows=3D2679 width=3D156) (actual=20 time=3D0.40..43.43 rows=3D2679 loops=3D1) -> Sort (cost=3D6101.24..6101.24=20 rows=3D8117 width=3D91) (actual time=3D553.64..559.58 rows=3D8117 loops=3D1) -> Seq Scan on=20 customer_month_summary cms (cost=3D0.00..5574.17 rows=3D8117 width=3D91)= =20 (actual time=3D258.03..477.11 rows=3D8117 loops=3D1) -> Sort (cost=3D5.18..5.18 rows=3D77 width=3D1= 8)=20 (actual time=3D0.70..0.80 rows=3D77 loops=3D1) -> Seq Scan on emailaddress ea=20=20 (cost=3D0.00..2.77 rows=3D77 width=3D18) (actual time=3D0.08..0.35 rows=3D7= 7=20 loops=3D1) Total runtime: 951.70 msec The table in question is "customer_month_summary"; it has 8117 rows.=20=20 Essentially, there is one record in the customer_month_summary table=20 for every month for every customer that has a cable modem (this doesn't=20 include dial-up users). I have two columns in the summary table that=20 I'm matching by: CustomerID and MonthStart, which is the first day of=20 the month in question. I also have an index on this table on=20 "Customer_Month_Summary(MonthStart, CustomerID)". It keeps trying to=20 do a sequence scan, and while the query only takes 951 msec right now,=20 the summary table will keep growing, and I don't want performance to=20 suffer a few months/years down the line (also, having to wait a second=20 between search results is a bit too much). This is currently running on a dual-proc PIII-800 with 4G of ram. I've=20 put a lot of effort to make this application very responsive, but it's=20 this one query that keeps killing me. Any help you can provide would=20 therefore be much appreciated. --man Michael A Nachbaur "I used to hate writing assignments, but now I enjoy them. I realized=20 that the purpose of writing is to inflate weak ideas, obscure poor=20 reasoning and inhibit clarity.=A0 With a little practice, writing can be=20 an intimidating and impenetrable fog!" -- Calvin