Re: slow self-join query

From: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
To: "Robert Poor" <rdpoor(at)gmail(dot)com>
Cc: <mmoncure(at)gmail(dot)com>,"Scott Marlowe" <scott(dot)marlowe(at)gmail(dot)com>, <pgsql-performance(at)postgresql(dot)org>
Subject: Re: slow self-join query
Date: 2012-03-19 16:45:56
Message-ID: 4F671C74020000250004641B@gw.wicourts.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Robert Poor <rdpoor(at)gmail(dot)com> wrote:

> @kevin: I hear you. (I'm deeply steeped in Ruby on Rails and
> foolishly assume that it's easy to read.) With that in mind:
>
> \d user_associations

> id | integer | not null default
> nextval('followings_id_seq'::regclass)

I assume that this is needed to keep RoR happy. Since a row seems
meaningless without both leader_id and follower_id, and that is
unique, the synthetic key here is totally redundant. Performance
(both modifying the table and querying against it) would be faster
without this column, but I understand that many ORMs (including, as
I recall, RoR) are more difficult to work with unless you have this.

> leader_id | integer |
> follower_id | integer |

I'm surprised you didn't declare both of these as NOT NULL.

> created_at | timestamp without time zone | not null
> updated_at | timestamp without time zone | not null

I can understand tracking when the follow was initiated, but what
would you ever update here? (Or is this part of a generalized
optimistic concurrency control scheme?)

> Indexes:
> "followings_pkey" PRIMARY KEY, btree (id)
> "index_followings_on_leader_id_and_follower_id" UNIQUE, btree
> (leader_id, follower_id)
> "index_followings_on_follower_id" btree (follower_id)
> "index_followings_on_leader_id" btree (leader_id)

This last index is of dubious value when you already have an index
which starts with leader_id. It will be of even more dubious
benefit when we have index-only scans in 9.2.

-Kevin

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Rafael Martinez 2012-03-21 11:21:23 DBD-Pg prepared statement versus plain execution
Previous Message Robert Poor 2012-03-19 15:22:17 Re: slow self-join query