4

So, I was looking at a view we have in our database, and there appears to be a nested junction. I was wondering in what situations that practice would be useful.

In my specific example, it's used to join with a table that has a composite key, but only joining with the tables that have the data as their primary key. For instance, assume a structure like this :

Activity(*IdActivity*, name)
Activity_Week(*IdActivity*, *WeekNo*, subject)
Attendance(*IdPerson*, *IdActivity*, *WeekNo*, isPresent)
Person(*IdPerson*, Name)

In that case, my query would look something like :

SELECT p.Id, p.Name, COUNT(at.*) 
FROM Person p
INNER JOIN Activity a
  INNER JOIN Activity_Week aw 
    ON aw.IdActivity = a.IdActivity
  INNER JOIN Attendance at 
    ON at.IdActivity = a.IdActivity 
    AND at.WeekNo = aw.WeekNo
    AND at.isPresent = 1
  ON p.IdPerson = at.IdPerson
GROUP BY p.Id, p.Name

So while in this situation I would highly doubt that it's any useful (since you can simply join with attendance and cascade from there), is there any other situation where nested junctions like that are actually a good thing, or would it always be better to join with a request instead?

What I mean by joining with a request would be something along the lines of :

SELECT p.Id, p.Name, COUNT(at.*) 
FROM Person p
INNER JOIN (
  SELECT a.name, at.* 
  FROM Activity a
  INNER JOIN Activity_Week aw 
    ON a.IdActivity = aw.IdActivity
  INNER JOIN Attendance at 
    ON at.IdActivity = a.IdActivity 
    AND at.WeekNo = aw.WeekNo
    AND at.isPresent = 1
) attendance_query ON p.IdPerson = attendance_query.IdPerson
GROUP BY p.Id, p.Name
Julien Blanchard
  • 825
  • 5
  • 18
  • 2
    What do you mean by "nested junction"? – Gordon Linoff Nov 27 '15 at 20:05
  • The guy who did the view joined multiple tables together twice, and joined the result together. Basically, the syntax looks like `SELECT ... FROM ... JOIN ... JOIN ... ON ... ON ... WHERE ...` – Julien Blanchard Nov 27 '15 at 20:06
  • 2
    I think the OP means the construction, where the `ON` part of the `Join` comes later. In this case the `INNER JOIN Activity a` has its `ON p.IdPerson=at.IdPerson` after all the other joins... – Shnugo Nov 27 '15 at 20:07
  • You could try both and compare the execution plans. I must admit, I didn't know, that this works... – Shnugo Nov 27 '15 at 20:51
  • @Shnugo Yeah, this is what I was gonna do, but the query currently has many more issues, so I'm gonna have to build a somewhat heavy query (2-8s) from scratch and evaluate from there. And it actually took me a while to understand how there could be 2 `ON` clauses next to each other, first time hearing about it myself. – Julien Blanchard Nov 27 '15 at 20:53
  • @Shnugo preliminary tests seems to indicate that nested junctions is actually a thing... I'll have to check further probably over the weekend (I'll also most probably add a bounty on Monday to something other than a handful of tests done by a single person who's using a white cane). – Julien Blanchard Nov 27 '15 at 21:38
  • Doesn't make any difference for inner joins. Can make a difference with outer joins though. Example of that here http://stackoverflow.com/a/7313507/73226 – Martin Smith Nov 27 '15 at 23:40
  • @MartinSmith Unfortunately, all the link you provided says is that "this method exist and nobody knows about it". What I'm asking is "is that method actually useful, or should we always use the alternatives (like selecting from a subquery)". **EDIT**: The duplicate question does touch on it, actually. – Julien Blanchard Nov 28 '15 at 00:41

0 Answers0