Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone explain me how the cartesian product works in relational algebra

here it says

Selection and cross product

Cross product is the costliest operator to evaluate. If the input relations have N and M rows, the result will contain NM rows. Therefore it is very important to do our best to decrease the size of both operands before applying the cross product operator.

suppose that we have 2 relations

first relation is called Student and has 3 attributes, thus

    student
  |a |b   |c |
  ------------
  |__|___|___|
  |__|___|___|
  |__|___|___|

second relation is university and again with 3 attributes

   university
  |e |f   |g |
  ------------
  |__|___|___|
  |__|___|___|
  |__|___|___|

we have 3 rows for each relation, so after applying the cross product operation we will get a relation which has 3*3 = 9 rows

now, I don't understand, why 9 and not 3?

won't the final relation be

 final relation
 |a |b   |c |d |e   |f |g |
 --------------------------
 |__|___|___|__|____|__|__|
 |__|___|___|__|____|__|__|
 |__|___|___|__|____|__|__|

doesn't this have 3 rows again?

Thanks

like image 652
jonathan Avatar asked Oct 10 '11 14:10

jonathan


People also ask

What is Cartesian product operation with Example discuss?

Cartesian product operationIt combines R1 and R2 without any condition. It is denoted by X. Degree of R1 XR2 = degree of R1 + degree of R2. {degree = total no of columns}

What is relational algebra explain Union and Cartesian product operation with example?

RELATIONAL ALGEBRA is a widely used procedural query language. It collects instances of relations as input and gives occurrences of relations as output. It uses various operations to perform this action. SQL Relational algebra query operations are performed recursively on a relation.

Which operator defines Cartesian product in relational algebra?

The cartesian product operation is denoted by a cross(X) symbol. It allows us to combine information from any two relations. We write cartesian product of two relations R1 and R2 as R1 X R2. The cartesian product of any two relations R1 (of degree m) and R2 (of degree n) yields a relation R1 X R2 of degree m+n.


2 Answers

If the rows in Student are row1, row2 and row3, and the rows in University are row4, row5 and row6, then the cartesian product will contain

row1row4, row1row5, row1row6, row2row4, row2row5, row2row6, row3row4, row3row5, row3row6

Each possible combination of rows. That's how it is defined. Nothing more to it.

Except for your remark "Therefore it is very important to do our best to decrease the size of both operands before applying the cross product operator.". It is important to realise that there do exist optimizers which are able to "rewrite" certain algebra operations. It is certainly not the case that the onus is always on the query writer to determine the "most appropriate way of combining restrictions with other operations". In fact, "moving restrictions to the inside as far as possible" is one of the things industrial optimizers are actually very good at.

like image 74
Erwin Smout Avatar answered Sep 27 '22 21:09

Erwin Smout


Just imagine that you have two tables one with the students and one with the universities, when you do a Cartesian query against a relational database you will get a row for every student which in turn is joined to every university.

Select *
   From students, 
        universities;

OR

SELECT * FROM students CROSS JOIN universities

I know this has little to do with algebra but since your on stackoverflow :D

like image 43
Michael D. Irizarry Avatar answered Sep 27 '22 21:09

Michael D. Irizarry