You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

含WHERE连接条件的CROSS JOIN SQL是否为真交叉连接?

Is This Really a CROSS JOIN?

Great question—this is a super common point of confusion when digging into auto-generated SQL from ORMs like Hibernate. Let’s break this down clearly:

Short Answer

While the syntax uses CROSS JOIN keywords, this query is logically equivalent to an INNER JOIN and will not produce a true Cartesian product (the "real" unfiltered CROSS JOIN behavior).

Detailed Breakdown

  1. Syntax vs. Actual Execution
    A true CROSS JOIN returns every possible combination of rows from the joined tables (no filtering or joining conditions). But here, the WHERE clause includes critical implicit join conditions:

    • c.targetsystempublication = t.id
    • c.canonicalitem = m.id
      Database optimizers immediately recognize these as join conditions, so they won’t waste resources generating a full Cartesian product first. Instead, they’ll execute this query exactly like an INNER JOIN with explicit ON clauses.
  2. Hybrid of ANSI-89 and ANSI-92 Syntax
    This query mixes two join syntax standards:

    • The CROSS JOIN keyword comes from the newer ANSI-92 standard
    • The join conditions tucked into the WHERE clause are classic ANSI-89 style (before explicit JOIN ... ON became the norm)
      Hibernate sometimes generates this hybrid when translating entity associations into SQL, especially if the mapping doesn’t explicitly define join types using modern syntax.
  3. Equivalent Modern INNER JOIN Query
    To make this even clearer, here’s the exact same logic rewritten using standard ANSI-92 INNER JOIN syntax—this will produce identical results and execution plans:

    SELECT max(c.item) 
    FROM ItemPubStatus c
    INNER JOIN TargetSystemPub t ON c.targetsystempublication = t.id
    INNER JOIN Item m ON c.canonicalitem = m.id
    WHERE t.targetsystem = ? 
      AND m.datapool = ? 
      AND m.itemtype = ?
    

Why Hibernate Generates This?

It usually comes down to how your entity mappings are set up. If Hibernate doesn’t detect an explicit join definition (like a @JoinColumn with a clear association), it may fall back to this syntactic quirk. Rest assured, though—this isn’t a bug, and the query will perform exactly like a standard INNER JOIN.

内容的提问来源于stack exchange,提问作者Justin

火山引擎 最新活动