|
Home > Archive > PostgreSQL Discussion > September 2005 > Many-To-Many Bridge Table Index
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Many-To-Many Bridge Table Index
|
|
| Sergei Dubov 2005-09-29, 8:23 pm |
| Hi guys,
I'd really appreciate if you could clarify this to me. Let's say I have
a table named TABLE_A that is a bridge to many-to-many relationship
between TABLE_B and TABLE_C, as such here is my declaration:
CREATE TABLE table_a (
table_b_id INT4 NOT
NULL
REFERENCES table_b ON DELETE CASCADE ON UPDATE CASCADE,
table_c_id INT4 NOT
NULL
REFERENCES table_c ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT table_a_pkey
PRIMARY KEY (table_b_id, table_c_id)
);
This will automatically create a unique index on the primary key.
Well, if I do a query later like:
select * from table_a where table_b_id=1,
will the query use an index? Will this be true when I do join on this table?
Thanks so much,
Serge
---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend
| |
| Peter Eisentraut 2005-09-30, 3:23 am |
| Am Donnerstag, 29. September 2005 21:44 schrieb Sergei Dubov:
> select * from table_a where table_b_id=1,
>
> will the query use an index? Will this be true when I do join on this
> table?
It could use the index, but whether it will depends on the particular data
distribution. The EXPLAIN command is your friend.
--
Peter Eisentraut
http://developer.postgresql.org/~petere/
---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings
| |
| Richard Huxton 2005-09-30, 3:23 am |
| Sergei Dubov wrote:
>
> This will automatically create a unique index on the primary key.
>
> Well, if I do a query later like:
>
> select * from table_a where table_b_id=1,
>
> will the query use an index? Will this be true when I do join on this
> table?
Yes to both (if the query plan thinks that is the most efficient way).
Of course a query on "table_c_id" can't use the index.
--
Richard Huxton
Archonet Ltd
---------------------------(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
|
|
|
|
|