X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.192]) by svr1.postgresql.org (Postfix) with ESMTP id 74196D227D for ; Fri, 11 Jul 2003 16:58:15 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr4.postgresql.org [64.117.224.192]) (amavisd-new, port 10024) with ESMTP id 20822-02 for ; Fri, 11 Jul 2003 16:58:28 +0000 (GMT) Received: from mailav.tor1.inquent.com (mail.inquent.com [216.208.117.106]) by svr1.postgresql.org (Postfix) with ESMTP id AAD84D3FAE for ; Fri, 11 Jul 2003 10:37:26 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by mailav.tor1.inquent.com (Postfix) with ESMTP id 0895A10258; Fri, 11 Jul 2003 09:30:58 -0400 (EDT) Received: from [10.0.2.7] (unknown [10.0.2.7]) by mailav.tor1.inquent.com (Postfix) with ESMTP id 0366510248; Fri, 11 Jul 2003 09:30:55 -0400 (EDT) Subject: Re: force the use of a particular index From: Rod Taylor To: Scott Cain Cc: Postgresql Performance In-Reply-To: <1057929459.5766.7.camel@localhost.localdomain> References: <1057850280.1451.22.camel@localhost.localdomain> <1057920672.46100.149.camel@jester> <1057929459.5766.7.camel@localhost.localdomain> Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="=-u5Sm2bo4G7N40JScR94f" Message-Id: <1057930695.46100.172.camel@jester> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.0 Date: 11 Jul 2003 13:38:16 +0000 X-Virus-Scanned: by AMaViS perl-11 X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200307/174 X-Sequence-Number: 2467 --=-u5Sm2bo4G7N40JScR94f Content-Type: text/plain Content-Transfer-Encoding: quoted-printable On Fri, 2003-07-11 at 13:17, Scott Cain wrote: > The problem (at least as it appears to me) is not that it is performing > a table scan instead of an index scan, it is that it is using the wrong > index. Here is the output from EXPLAIN ANALYZE: >=20 > = QUERY PLAN > -------------------------------------------------------------------------= ---------------------------------------------------------------------------= -------- > Unique (cost=3D494008.47..494037.59 rows=3D166 width=3D54) (actual time= =3D114660.37..114660.38 rows=3D1 loops=3D1) > -> Sort (cost=3D494008.47..494012.63 rows=3D1664 width=3D54) (actual= time=3D114660.37..114660.37 rows=3D1 loops=3D1) > Sort Key: f.name, fl.fmin, fl.fmax, fl.strand, f.type_id, f.feat= ure_id > -> Nested Loop (cost=3D0.00..493919.44 rows=3D1664 width=3D54)= (actual time=3D2596.13..114632.90 rows=3D1 loops=3D1) > -> Index Scan using feature_pkey on feature f (cost=3D0.= 00..134601.43 rows=3D52231 width=3D40) (actual time=3D105.74..56048.87 rows= =3D13825 loops=3D1) > Filter: (type_id =3D 219) > -> Index Scan using featureloc_idx1 on featureloc fl (co= st=3D0.00..6.87 rows=3D1 width=3D14) (actual time=3D4.23..4.23 rows=3D0 loo= ps=3D13825) > Index Cond: ("outer".feature_id =3D fl.feature_id) > Filter: ((srcfeature_id =3D 6) AND (fmin <=3D 258558= 1) AND (fmax >=3D 2565581)) > Total runtime: 114660.91 msec > it is using on featureloc (featureloc_idx1) is on the foreign key > feature_id. It should instead be using another index, featureloc_idx3, > which is built on (srcfeature_id, fmin, fmax). Nope.. The optimizer is right in the decision to use featureloc_idx1.=20 You will notice it is expecting to retrieve a single row from this index, but the featureloc_idx3 is bound to be larger (due to indexing more data), thus take more disk reads for the exact same information (or in this case, lack thereof). What is taking a long time is the scan on feature_pkey. It looks like it is throwing away a ton of rows that are not type_id =3D 219. Either that, or you do a pile of deletes and haven't run REINDEX recently. Create an index consisting of (feature_id, type_id). This will probably make a significant different in execution time. --=-u5Sm2bo4G7N40JScR94f Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (FreeBSD) iD8DBQA/Dr3G6DETLow6vwwRAhQxAJ9FFy+CGHh99UjCETJeDiSDFtMGSgCcCjQ3 7S5fDFJ5d8n1Jamw0jhCfXk= =Tu/i -----END PGP SIGNATURE----- --=-u5Sm2bo4G7N40JScR94f--