Cartesian Product
Also Cross-Product · Cross Product
The Cartesian product of two tables is the table of all concatenations of a row of the first with a row of the second, so its columns add up and its rows multiply.
Know these firstRelational Model
Formula#
For tables :
Cost estimate for query planning, with having rows and columns:
This estimate is accurate, since the result of that size has to be written anyway.
Properties#
- SQL: the
FROMclause with several comma-separated tables denotes their Cartesian product;WHEREthen selects rows of it (SQL Query Structure). Without aWHEREclause the full product really must be computed. - Order of products: associative and commutative up to a permutation of the columns, but the cost of intermediate products differs. Combine small tables first, and push selections and projections below the product (Relational Algebra > Equivalences used in query planning).
- Empty input: if , then and the product costs 0.
- Join: a selection with an equality between one column of each table on top of a product is a Join, which is computed without materialising the product (hash join, Merge Join). With an arbitrary join condition the worst case falls back to the full product.
- Disconnected patterns: a SPARQL query whose triples fall into groups that share no variable translates to a SQL query whose result is the Cartesian product of the groups’ results (SPARQL to SQL Translation).
Example#
Sources#
- Slides 03, p. 29–32 (FROM as cross-product, sets)
- Slides 04, p. 13–14 (definition, example), p. 26 (cost estimate)
- Slides 05, p. 9 (product + select vs. join)
- DBIS_VL03 > FROM: the cross-product, DBIS_VL04 > Cartesian product, DBIS_VL04 > Cost and size estimates, DBIS_VL05 > Join operation