An accidental CROSS JOIN occurs when tables are combined without an appropriate relationship condition, causing every row from one table to match every row from another table.
Accidental Cartesian products can dramatically increase result size and produce incorrect business results.
Joining 10,000 customers with 5,000 products without a valid condition could produce up to 50 million combinations.
SELECT
c.CustomerName,
o.OrderID
FROM Customers c
INNER JOIN Orders o
ON c.CustomerID = o.CustomerID;
Important when developing complex queries against large production databases.
- Always verify the JOIN condition.
- Missing relationships can create huge result sets.
- Check row counts when adding JOINs.
- Use explicit JOIN syntax.
- Review execution plans for unexpectedly large operations.
What causes an accidental Cartesian product?
It can occur when tables are combined without an appropriate relationship condition.
How can accidental CROSS JOINs be prevented?
Clearly define the correct JOIN condition and validate the resulting row count.