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 02B47D1B7EF for ; Sat, 26 Jun 2004 18:06:28 -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 71261-08 for ; Sat, 26 Jun 2004 21:06:25 +0000 (GMT) Received: from lucifer.oficina (unknown [200.68.121.201]) by svr1.postgresql.org (Postfix) with ESMTP id 21EF5D1B7DA for ; Sat, 26 Jun 2004 18:06:21 -0300 (ADT) Received: from localhost (localhost.akyasociados.com.ar [127.0.0.1]) by lucifer.oficina (8.12.10/8.12.10) with ESMTP id i5QL6OBF065073; Sat, 26 Jun 2004 18:06:24 -0300 (ART) (envelope-from franco@akyasociados.com.ar) Received: from lucifer.oficina ([127.0.0.1]) by localhost (lucifer.oficina [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 64612-03; Sat, 26 Jun 2004 18:06:22 -0300 (ART) Received: from [192.168.1.198] (taz.oficina [192.168.1.198]) (authenticated bits=0) by lucifer.oficina (8.12.10/8.12.10) with ESMTP id i5QL6M2C065067; Sat, 26 Jun 2004 18:06:22 -0300 (ART) (envelope-from franco@akyasociados.com.ar) Subject: Re: Need indexes on inherited tables? From: Franco Bruno Borghesi To: Phil Endecott Cc: pgsql-sql@postgresql.org In-Reply-To: <1902614@chezphil.org> References: <1902614@chezphil.org> Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="=-zJSmuVO4lsYeefuhcEhw" Organization: AK y Asociados S.R.L. Message-Id: <1088283980.1215.19.camel@taz.oficina> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.6 Date: Sat, 26 Jun 2004 18:06:20 -0300 X-Virus-Scanned: by amavisd-new at akyasociados.com.ar X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.1 tagged_above=0.0 required=5.0 tests=HTML_FONTCOLOR_UNSAFE, HTML_MESSAGE X-Spam-Level: X-Archive-Number: 200406/266 X-Sequence-Number: 18211 --=-zJSmuVO4lsYeefuhcEhw Content-Type: multipart/alternative; boundary="=-BZqJwdGRK3j67pZeSO6D" --=-BZqJwdGRK3j67pZeSO6D Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Table T is not inheriting any indexes, neither the primary key constraint. That means that the search is going to use an index scan on table B and a sequential scan on table T (unless of course you add a primary key constraint or an index on table T). You can check this things doing:=20 ->SET enable_seqscan TO off; ->EXPLAIN SELECT * FROM B WHERE id=3D5;=20 you'll see an index scan on table B and sequential scans on the other tables. Doing: ->SELECT C.relname AS table_name, C2.relname AS index_name FROM pg_index I LEFT JOIN pg_class C ON (I.indrelid=3DC.oid) LEFT JOIN pg_class C2 ON (C2.oid=3DI.indexrelid) WHERE C.relname ILIKE '' you can find out what indexes are available for table_name (or \d in psql). On Sat, 2004-06-26 at 16:29, Phil Endecott wrote: > Dear Postgresql experts, >=20 > I have a base table that declares a primary key spanning a couple of colu= mns: >=20 > create table B ( > id integer, > xx someothertype, > ..... > primary key (id, xx) > ); >=20 > and a number of derived tables that inherit from B: >=20 > create table T ( > .... > ) inherits (B); >=20 > An index is automatically created for B because of the primary key. >=20 > If I search for something in T using the key columns, e.g. I do >=20 > select * from T where id=3D1 and xx=3Dsomething; >=20 > will the index be used? Or must I explicity create an index on id and xx= for T and each of the other derived tables? >=20 > Is it any different if I search in B and find rows that are actually in T? >=20 > (Slightly unrelated: does the index on (id,xx) help when I am searching o= nly on id?) >=20 > Thanks for any insight anyone can offer. >=20 > --Phil. >=20 > ---------------------------(end of broadcast)--------------------------- > TIP 7: don't forget to increase your free space map settings >=20 --=-BZqJwdGRK3j67pZeSO6D Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable Table T is not inheriting any indexes, neither the primary key constraint. = That means that the search is going to use an index scan on table B and a s= equential scan on table T (unless of course you add a primary key constrain= t or an index on table T).

You can check this things doing:
->SET enable_seqscan TO off;
->EXPLAIN SELECT * FROM B WHERE id=3D5;
you'll see an index scan on table B and sequential scans on the other table= s.

Doing:
->SELECT C.relname AS table_name, C2.relname AS index_name FROM pg_index= I LEFT JOIN pg_class C ON (I.indrelid=3DC.oid) LEFT JOIN pg_class C2 ON (C= 2.oid=3DI.indexrelid) WHERE  C.relname ILIKE '<table_name>'

you can find out what indexes are available for table_name (or \d <table= _name> in psql).


On Sat, 2004-06-26 at 16:29, Phil Endecott wrote:
Dear Postgresql experts,

I have a base table that declares a primary key spanning a couple of column=
s:

create table B (
  id integer,
  xx someothertype,
  .....
  primary key (id, xx)
);

and a number of derived tables that inherit from B:

create table T (
 ....
) inherits (B);

An index is automatically created for B because of the primary key.

If I search for something in T using the key columns, e.g. I do

  select * from T where id=3D1 and xx=3Dsomething;

will the index be used?  Or must I explicity create an index on id and xx f=
or T and each of the other derived tables?

Is it any different if I search in B and find rows that are actually in T?

(Slightly unrelated: does the index on (id,xx) help when I am searching onl=
y on id?)

Thanks for any insight anyone can offer.

--Phil.

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings
--=-BZqJwdGRK3j67pZeSO6D-- --=-zJSmuVO4lsYeefuhcEhw Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (FreeBSD) iD8DBQBA3eVM21dVnhLsBV0RAn2GAJwIlSQtyRrRfemGq4fibwcbDoHvaACfZsSh I3+B/McBtkM2u2Y+RPJZ7fg= =4hdX -----END PGP SIGNATURE----- --=-zJSmuVO4lsYeefuhcEhw--