Study Vault Search notes, aliases and full text
Graph
ConceptDBISAI-written · not yet proofread

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 kk tables T1,…,TkT_1, \ldots, T_k:

#cols(T1×⋯×Tk)=∑i=1k#cols(Ti),#rows(T1×⋯×Tk)=∏i=1k#rows(Ti).\#\mathrm{cols}(T_1 \times \dots \times T_k) = \sum_{i=1}^{k} \#\mathrm{cols}(T_i), \qquad \#\mathrm{rows}(T_1 \times \dots \times T_k) = \prod_{i=1}^{k} \#\mathrm{rows}(T_i).

Cost estimate for query planning, with TiT_i having nin_i rows and kik_i columns:

cost(T1×T2)∼n1⋅n2⋅(k1+k2),rows=n1n2,cols=k1+k2.\text{cost}(T_1 \times T_2) \sim n_1 \cdot n_2 \cdot (k_1 + k_2), \qquad \text{rows} = n_1 n_2, \qquad \text{cols} = k_1 + k_2.

This estimate is accurate, since the result of that size has to be written anyway.

Properties#

  • SQL: the FROM clause with several comma-separated tables denotes their Cartesian product; WHERE then selects rows of it (SQL Query Structure). Without a WHERE clause 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 ∣R1∣=0|R_1| = 0, then ∣R1×R2∣=0|R_1 \times R_2| = 0 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#

Search

Type In
↑ ↓ move ↵ open ⇥ next section Ctrl↵ results page Prefix # to search text only