A Hidden Benefit of Implicit Joins: Join Elimination

0
1Кб

Implicit path joins may now skip unnecessary tables in the join tree

One of jOOQ’s key features so far has always been to render pretty much exactly the SQL that users expect, without any surprises – unless some emulation is required to make a query work, of course. This means that while join elimination is a powerful feature of many RDBMS, it isn’t part of jOOQ’s feature set, so far.

This changes, to some extent, with jOOQ 3.19, and #14992, for implicit path joins only. So far, when you write:

ctx.select(ACTOR, ACTOR.film().category().NAME)
   .from(ACTOR)
   .fetch();

The resulting join tree of this query may look similar to this:

FROM
  actor
    LEFT JOIN film_actor ON actor.actor_id = film_actor.actor_id
    LEFT JOIN film ON film_actor.film_id = film.film_id
    LEFT JOIN film_category ON film.film_id = film_category.film_id
    LEFT JOIN category ON film_category.category_id = category.category_id

But, the FILM table isn’t really needed in this particular query, because no columns from it are being projected, and the presence of primary / foreign keys guarantees equivalence if we just skip the table in the join tree:

FROM
  actor
    LEFT JOIN film_actor ON actor.actor_id = film_actor.actor_id
    LEFT JOIN film_category ON film_actor.film_id = film_category.film_id
    LEFT JOIN category ON film_category.category_id = category.category_id

As soon as any column from the FILM table is projected (or referenced, in general), then the table re-appears in the join tree. E.g. for this query:

ctx.select(ACTOR, ACTOR.film().category().NAME)
   .from(ACTOR)
   // This means we have to add the FILM table again to the join tree:
   .where(ACTOR.film().TITLE.like("A%"))
   .fetch();

In many RDBMS, this doesn’t really matter, because the RDBMS may do the same optimisation, but in some, there’s a big difference. This is a great optimisation in particular because with implicit path joins, jOOQ users can’t really hand-write these optimisations as they’re not authoring the join tree in the FROM clause themselves.

Why implement this only in jOOQ 3.19

Before jOOQ 3.19, there was no support for to-many path joins, and particularly, not for many-to-many path joins, which skip the relationship table. But now, users can write:

// This
ACTOR.film().category().NAME

// Is short (and equivalent) for this:
ACTOR.filmActor().film().filmCategory().category().NAME

Note that the above examples assume that the new Settings.renderImplicitJoinToManyType flag is set to LEFT_JOIN. By default, implicit to-many joins aren’t supported because of their weird semantics in terms of query cardinalities as explained in this blog post. By default, such paths have to be declared explicitly in the FROM clause:

ctx.select(ACTOR, ACTOR.film().category().NAME)
   .from(
       ACTOR,
       ACTOR.film(),
       ACTOR.film().category())
   .fetch();

Or, just:

ctx.select(ACTOR, ACTOR.film().category().NAME)
   .from(
       ACTOR,
       ACTOR.film().category())
   .fetch();
Спонсоры
Спонсоры
Спонсоры
Спонсоры
Спонсоры
Поиск
Спонсоры
Virtuala FansOnly
CDN FREE
Cloud Convert
Категории
Больше
Другое
Anti-counterfeit packaging Market, Size, Share, Growth, Trends and Forecast (2024-2032)
According to the UnivDatos, the growing prevalence of counterfeit products across industries like...
От Praveen Gupta 2026-01-13 06:09:36 0 69
Art
Houdini 21 में आ रहे नए फीचर्स: क्या ये सच में जरूरी हैं?
Houdini, एनिमेशन और विज़ुअल इफेक्ट्स की दुनिया में एक जादुई नाम, अब एक बार फिर अपने नए वर्ज़न...
От Seema Vandana 2025-08-06 01:05:22 1 741
Crafts
Aaron Boone admits he used wrong reliever in key spot in Yankees loss
In a rare acknowledgment of a regret, manager Aaron Boone said he probably should have brought in...
От Verna Skiles 2025-10-27 03:44:05 0 402
Главная
Saturation Height: The Hidden Metric Changing Modern Photography Forever
In the evolving world of digital photography, new metrics often emerge that redefine creativity,...
От Emma Marie 2025-12-17 06:40:05 0 1Кб
Art
Astronomer’ın Yeni CEO’su Konuştu—Evet, O Anla Hakkında!
## Giriş: Astronomer’ın Yeni Yıldızı Hayat, bazen sıradan bir günün ortasında öyle bir...
От Seda Gizem 2025-08-22 11:05:27 1 762
Спонсоры
Virtuala FansOnly https://virtuala.site