Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 7C1811337CA2 for ; Wed, 30 Mar 2011 13:24:07 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 60946-01-3 for ; Wed, 30 Mar 2011 16:23:57 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from exchange01.sharedmarketing.com (exchange01.sharedmarketing.com [67.217.132.12]) by mail.postgresql.org (Postfix) with ESMTP id C75311338161 for ; Wed, 30 Mar 2011 13:04:42 -0300 (ADT) Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----_=_NextPart_001_01CBEEF4.2DBDD071" Subject: Re: Postgres not using indexes X-MimeOLE: Produced By Microsoft Exchange V6.5 Date: Wed, 30 Mar 2011 11:04:41 -0500 Message-ID: <2701CF596B80DC44815FDBFFF5881A1E01340732@exchange01.sharedmarketing.com> In-Reply-To: <965AA5440EAC094E9F722519E285ACEDAC5E66A53D@WWCEXCHANGE.web.web.com> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [BUGS] Postgres not using indexes Thread-Index: Acvu60Z+XFpNS092TUKvEIe1OBdmAgACKDig References: <965AA5440EAC094E9F722519E285ACEDAC5E66A53D@WWCEXCHANGE.web.web.com> From: "Nathan M. Davalos" To: "Lawrence Cohan" , X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.909 tagged_above=-5 required=5 tests=BAYES_00=-1.9, HTML_MESSAGE=0.001, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201103/384 X-Sequence-Number: 29894 This is a multi-part message in MIME format. ------_=_NextPart_001_01CBEEF4.2DBDD071 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable I force postgresql to use indexes instead of sequential scans by setting enable_seqscan =3D off in postgresql.conf and it helps in a lot of = cases. Probably not the best practice, but it does improve a lot of the queries we will execute on a regular basis. It forces the planner to prefer indexes. I've also noticed that limit behavior which is sort of puzzling to me. =20 =20 From: pgsql-bugs-owner@postgresql.org [mailto:pgsql-bugs-owner@postgresql.org] On Behalf Of Lawrence Cohan Sent: Wednesday, March 30, 2011 10:01 AM To: pgsql-bugs@postgresql.org Subject: [BUGS] Postgres not using indexes =20 We have a huge performance issues in Postgres that surfaced due to existing indexes not being used like in the example below in both 8.35 and 9.0 versions. =20 Client_Orders table with and int ID as PK which is the order_id and indexed - about 155,000 rows Order_Items table with and int ID primary key and INDEX on Order_id (int) matching the ID in the above client_orders table. - about 33 million rows =20 A query like below takes almost ten minutes to complete however the result set is 33768 rows and Total query runtime: 427539 ms.!!! This is due to the fact that the index on Order_Items it is NOT used and a sequence scan is done instead but this is obviously not acceptable from performance point of view. If I add a LIMIT 1000 for instance then the index is used and query returns results in no time as expected but as soon as I go higher in the limit to a few thousands then the index on Order_Items.Order_id is no longer used - why??? Is there any way to force Postgres to use the existing indexes instead of table seq scan which is deadly? =20 select oi.id from order_items oi INNER JOIN client_orders co ON oi.order_id =3D co.id =20 Regards, Nenea Nelu. =20 =20 ________________________________ Attention: The information contained in this message and or attachments is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any system and destroy any copies. ------_=_NextPart_001_01CBEEF4.2DBDD071 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

I force postgresql to use indexes instead of = sequential scans by setting enable_seqscan =3D off in postgresql.conf = and it helps in a lot of cases. Probably not the best practice, but it = does improve a lot of the queries we will execute on a regular basis. It = forces the planner to prefer indexes. I’ve also noticed that limit = behavior which is sort of puzzling to me.

 

 

From:= = pgsql-bugs-owner@postgresql.org [mailto:pgsql-bugs-owner@postgresql.org] = On Behalf Of Lawrence Cohan
Sent: Wednesday, March 30, = 2011 10:01 AM
To: pgsql-bugs@postgresql.org
Subject: = [BUGS] Postgres not using indexes

 

We have a huge performance issues in Postgres that surfaced = due to existing indexes not being used like in the example below in both = 8.35 and 9.0 versions.

 

Client_Orders table with and int ID as PK which is the = order_id and indexed – about 155,000 rows

Order_Items table with and int ID = primary key and INDEX on Order_id (int) matching the ID in the above = client_orders table. – about 33 million = rows

 

A query like below takes almost ten minutes to complete = however the result set is 33768 rows and Total query runtime: 427539 = ms.!!! This is due to the fact that the index on Order_Items it is NOT = used and a sequence scan is done instead but this is obviously not = acceptable from performance point of view. If I add a LIMIT 1000 for = instance then the index is used and query returns results in no time as = expected but as soon as I go higher in the limit to a few thousands then = the index on Order_Items.Order_id is no longer used – why??? Is = there any way to force Postgres to use the existing indexes instead of = table seq scan which is deadly?

 

select oi.id from order_items oi = INNER JOIN client_orders co ON oi.order_id =3D = co.id

 

Regards,

Nenea Nelu.

 

 


Att= ention:
The information contained in this message and or attachments = is intended only for the person or entity to which it is addressed and = may contain confidential and/or privileged material. Any review, = retransmission, dissemination or other use of, or taking of any action = in reliance upon, this information by persons or entities other than the = intended recipient is prohibited. If you received this in error, please = contact the sender and delete the material from any system and destroy = any copies.

------_=_NextPart_001_01CBEEF4.2DBDD071--