postgresql - Is a foreign key mandatory to make a join query scale? -
i discuss simple postgres query , figure out if have got bit of theory behind postgres , dbs in general. here comes:
-- query 1 select posts.*, users.* posts inner join users on posts.user_id = users.id posts.user_id = :id order posts.creation_time it involves 2 tables (users , posts) joined together. there index on (posts.creation_time, posts.user_id) speed search.
my understanding need foreign key associates posts.user_id users.id not enforce form of referencial integrity such, olso, , more importantly, speed on posts.user_id = users.id bit of query.
am right in saying so?
now conside version of query:
-- query 2 select posts.*, users.* posts inner join users on posts.user_id = users.id users.id = :id order posts.creation_time assuming aforementioned foreign key (posts.creation_time, posts.user_id) exists, query scale, taking account variable in equation lives on other side of join?
my guess won't scale
thanks
check post:
postgres , indexes on foreign keys , primary keys
foreign keys necessary maintaining referential integrity of data. indexes helps make queries faster. creating foreign key not automatically create index.
Comments
Post a Comment