Unix Technical Forum

index not used with inherited tables

This is a discussion on index not used with inherited tables within the pgsql Novice forums, part of the PostgreSQL category; --> Hi, there is a table person, which has some child tables. the definition (part of): CREATE TABLE person ( ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Novice

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-17-2008, 10:51 PM
Verena Ruff
 
Posts: n/a
Default index not used with inherited tables

Hi,

there is a table person, which has some child tables. the definition
(part of):

CREATE TABLE person
(
pers_id serial NOT NULL,
-- some other fields --
CONSTRAINT person_pk PRIMARY KEY (pers_id)
)

one of these child tables is mitarbeiter:
CREATE TABLE mitarbeiter
(
-- some fields --
CONSTRAINT pers_id_pk_mitarbeiter PRIMARY KEY (pers_id)
) INHERITS (person)

as you see the inherited column pers_id is used as primary key in the
child table as well. if I do
EXPLAIN SELECT * FROM mitarbeiter WHERE pers_id=7; I see that PostgreSQL
does a seq scan on mitarbeiter instead of an index scan. There are some
other child tables and it seems like in some cases the index is used, in
others not, but I have no idea why.

Any hints?

Regards,
Verena

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-17-2008, 10:51 PM
Sean Davis
 
Posts: n/a
Default Re: index not used with inherited tables




On 5/10/06 9:04 AM, "Verena Ruff" <lists@triosolutions.at> wrote:

> Hi,
>
> there is a table person, which has some child tables. the definition
> (part of):
>
> CREATE TABLE person
> (
> pers_id serial NOT NULL,
> -- some other fields --
> CONSTRAINT person_pk PRIMARY KEY (pers_id)
> )
>
> one of these child tables is mitarbeiter:
> CREATE TABLE mitarbeiter
> (
> -- some fields --
> CONSTRAINT pers_id_pk_mitarbeiter PRIMARY KEY (pers_id)
> ) INHERITS (person)
>
> as you see the inherited column pers_id is used as primary key in the
> child table as well. if I do
> EXPLAIN SELECT * FROM mitarbeiter WHERE pers_id=7; I see that PostgreSQL
> does a seq scan on mitarbeiter instead of an index scan. There are some
> other child tables and it seems like in some cases the index is used, in
> others not, but I have no idea why.
>
> Any hints?


Hi, Verena. See here:

http://www.postgresql.org/docs/8.1/i...l-inherit.html

Notice the "Caveats" section at the bottom. Indexes are not inherited.

Sean


---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-17-2008, 10:52 PM
Verena Ruff
 
Posts: n/a
Default Re: index not used with inherited tables

Hi,

Sean Davis schrieb:
>> one of these child tables is mitarbeiter:
>> CREATE TABLE mitarbeiter
>> (
>> -- some fields --
>> CONSTRAINT pers_id_pk_mitarbeiter PRIMARY KEY (pers_id)
>> ) INHERITS (person)
>>

> Hi, Verena. See here:
>
> http://www.postgresql.org/docs/8.1/interactive/ut ddl-inherit.html
>
> Notice the "Caveats" section at the bottom. Indexes are not inherited

But I define a new primary key constraint in the child table, using the
column frm the parent. Shouldn't create this an index implicitly?

Regards,
Verena

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-17-2008, 10:52 PM
Tom Lane
 
Posts: n/a
Default Re: index not used with inherited tables

Verena Ruff <lists@triosolutions.at> writes:
> But I define a new primary key constraint in the child table, using the
> column frm the parent. Shouldn't create this an index implicitly?


Yes, and I'm sure it did. The more relevant question is how much data
do you have in the child table? The planner generally won't bother with
an indexscan if the table is only one or two disk pages; trying to use
the index in such cases would end up fetching *more* disk pages overall.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-17-2008, 10:52 PM
Verena Ruff
 
Posts: n/a
Default Re: index not used with inherited tables

Hi,

Tom Lane schrieb:
> Yes, and I'm sure it did. The more relevant question is how much data
> do you have in the child table? The planner generally won't bother with
> an indexscan if the table is only one or two disk pages; trying to use
> the index in such cases would end up fetching *more* disk pages overall.
>

Yes, there isn't much data in those child tables. It seems like this is
the reason why. Thanks for this information!

Regards,
Verena

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:46 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com