Most efficient way to select events from users having common subscriptions

From: Brice André <brice(at)famille-andre(dot)be>
To: pgsql-sql(at)lists(dot)postgresql(dot)org
Subject: Most efficient way to select events from users having common subscriptions
Date: 2022-08-22 06:52:41
Message-ID: CAOBG12mB0Vhx11xq73K2Yxjxg35Ayw9vSzsz-Hv06xNJGgXbSA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hello,

I have a project on which users may be registered to different challenges.
And I would want to implement a "news" panel, where users are notified of
events performed by other users, which have at least one challenge in
common.

My DB schema has tables like follows :

- table "user"
- user_id
- etc.
- table "challenge"
- challenge_id
- etc.
- table "user_challenge_association"
- user_id
- challenge_id
- team_id
- table "activity"
- user_id

I would thus want to perform a request returning all activities of all
users which have at least one entry in "user_challenge_association" where
"challenge_id" is the same as another entry in "user_challenge_association"
associated to the user for which I am doing the request.

Imagine "user" contains (user_1, user_2, user_3), imaging "challenge"
contains (challenge_1, challenge_2), imagine that user_1 is only registered
to challenge_1 user_2 is only registered to challenge_2, and user_3 is
registered to both challenges, I would want :

- that request for user_1 only returns activities of user_3
- that request for user_2 only returns activities of user_3
- that request for user_3 returns activities of user_1 and user_2

What would be the best way to perform such a query ?

Additional question : How could I integrate in the result a column
indicating if, in all existing challenges, the resulting activity is linked
to a user that was at least once in the same team as the user for which we
perform the request ? (team information is in "user_challenge_association"
table

Many thanks for your advices,
Brice

Browse pgsql-sql by date

  From Date Subject
Next Message Voillequin, Jean-Marc 2022-08-30 16:22:45 execution id
Previous Message Zainik Theme 2022-08-18 08:46:45 #1 Best Open Source eCommerce Platform of 2022