views:

44

answers:

3
CREATE INDEX alias_pub_idx2
  ON info.palias
  USING btree
  (publisher_id, player_id, pub_player_id);

CREATE INDEX alias_pub_idx3
  ON info.palias
  USING btree
  (player_id);

The first includes the three columns; the latter includes only the one. I'm thinking they are redundant- that the first btree index is sufficient, but I'm not terribly familiar w/ the PostgreSQL indexing methods. Thoughts?

+7  A: 

PostgreSQL multicolumn indexes are "ordered", which means this would be redundant only if player_id was the first column mentioned in alias_pub_idx2.

This, however, would be redundant :

CREATE INDEX alias_pub_idx2
  ON info.palias
  USING btree
  (publisher_id, player_id, pub_player_id);

CREATE INDEX alias_pub_idx3
  ON info.palias
  USING btree
  (publisher_id); /* first column of another index => waste of space */
Samuel_xL
So it right to say that if player_id is the column more joined on (by far), it should either be the first column in the index, or have its own standalone index?
Wells
@Wells yes, that's right. From the documentation: " the index is most efficient when there are constraints on the leading (leftmost) columns"
Samuel_xL
Great; thanks! I appreciate it.
Wells
A: 

Neither index makes the other redundant - a query against info.palias that looks for particular player_ids would favour the second index over the first if publisher_id is not also a criteria.

Will A
A: 

USING btree

BTREE indexes are ordered, that's why your second index is redundant: The first column is the same and can be used in all queries where player_id is a condition.

Frank Heikens