| 1 |
|
| 2 |
Complexity |
| 3 |
|
| 4 |
Abstract |
| 5 |
Purpose of this document is to describe in a detailed way the |
| 6 |
complexity of relational algebra operations. The evaluation will be |
| 7 |
done on the specific implementation of this program, not on theorical |
| 8 |
lower limits. |
| 9 |
|
| 10 |
Latest implementation can be found at: |
| 11 |
https://codeberg.ogr/ltworf/relational |
| 12 |
|
| 13 |
Notation |
| 14 |
Big O notation will be used. Constant values will be ignored. |
| 15 |
|
| 16 |
Single letters will be used to indicate relations and letters between |
| 17 |
| will indicate the cardinality (number of tuples) of the relation. |
| 18 |
|
| 19 |
Number of tuples can't be enough. For example a relation with one |
| 20 |
touple and thousands of fields, will not take O(1) in general to be |
| 21 |
evaluated. So we assume that relations will have a reasonable and |
| 22 |
comparable number of fields. |
| 23 |
|
| 24 |
Then after evaluating the big O notation, an attempt to find more |
| 25 |
precise results will be done, since it will be important to know |
| 26 |
with a certain precision the weight of the operation. |
| 27 |
|
| 28 |
1. UNARY OPERATORS |
| 29 |
|
| 30 |
Relational defines three unary operations, and they will be studied |
| 31 |
in this section. It doesn't mean that they should have similar |
| 32 |
complexity. |
| 33 |
|
| 34 |
1.1 Selection |
| 35 |
|
| 36 |
Selection works on a relation and on a python expression. For each |
| 37 |
tuple of the relation, it will create a dictionary with name:value |
| 38 |
where name are names of the fields in the relation and value is the |
| 39 |
value for the specific row. |
| 40 |
We can consider the inner cycle as constant as its value doesn't |
| 41 |
depend on the relation itself but only on the kind of the relation |
| 42 |
(how many field it has). |
| 43 |
Then comes the evaluation. A python expression in truth could do |
| 44 |
much more things than just checking if a>b. Anyway, ssuming that |
| 45 |
nobody would ever write cycles into a selection condition, we have |
| 46 |
another constant complexity for this operation. |
| 47 |
Then, the tuple is inserted in a new relation if it satisfies the |
| 48 |
condition. Since no check on duplicated tuples is performed, this |
| 49 |
operation is constant too. |
| 50 |
|
| 51 |
In the end we have O(|n|) as complexity for a selection on the |
| 52 |
relation n. |
| 53 |
|
| 54 |
1.2 Rename |
| 55 |
|
| 56 |
The rename operation itself is very simple, just modify the list |
| 57 |
containing the name of the fields. |
| 58 |
The big issue is to copy the content of the relation into a new |
| 59 |
relation object, so the new one can be modified. |
| 60 |
|
| 61 |
So the operation depends on the size of the relation: O(|n|). |
| 62 |
|
| 63 |
1.3 Projection |
| 64 |
|
| 65 |
The projection operation creates a copy of the original relation |
| 66 |
using only a subset of its fields. Time for the copy is something |
| 67 |
like O(|n|) where f is the number of fields to copy. |
| 68 |
But that's not all. Since relations are set, duplicated items are not |
| 69 |
allowed. So after extracting the wanted elements, it has to check if |
| 70 |
the new tuple was already added to the new relation. And this brings |
| 71 |
the complexity to O(|n|²). |
| 72 |
|
| 73 |
But the projection can also be used to "rearrange" fields, which |
| 74 |
makes no sense in pure relational algebra, but can be usefull to make |
| 75 |
two relations match (in fact it is used internally to make relations |
| 76 |
match if they have the same fields in different order). In this case |
| 77 |
there is no need to check if the tuple already exists, because it is |
| 78 |
assumed that the relation was correct. This gives a complexity of |
| 79 |
O(|n|) in the best case. |
| 80 |
|
| 81 |
2. BINARY OPERATORS |
| 82 |
|
| 83 |
Relational defines nine binary operations, and they will be studied |
| 84 |
in this section. Since we will deal with two relations per operation |
| 85 |
here, we will call them m and n, and f and g will be the number of |
| 86 |
their fields. |
| 87 |
|
| 88 |
2.1 Product |
| 89 |
|
| 90 |
Product is a very complex operations. It is O(|n|*|m|). |
| 91 |
Obvious. |
| 92 |
|
| 93 |
2.2 Intersection |
| 94 |
|
| 95 |
Same as product even if it does a different thing. But it has to |
| 96 |
compare every tuple from n with every tuple from m, to see if they |
| 97 |
match, and in this case, put them in the resulting relation. |
| 98 |
So this operation is O(|n|*|m|) as well. |
| 99 |
|
| 100 |
2.3 Difference |
| 101 |
|
| 102 |
Same as above: |
| 103 |
|
| 104 |
2.4 Union |
| 105 |
|
| 106 |
This operation first creates a new relation copying all the tuples |
| 107 |
from one of the originating relations, then compares them all with |
| 108 |
tuples from the other relation, and if they aren't in, they will be |
| 109 |
added. |
| 110 |
In fact it is same as above: O(|n|*|m|) |
| 111 |
|
| 112 |
2.5 Thetajoin |
| 113 |
|
| 114 |
This operation is the combination of a product and a selection. So it |
| 115 |
is O(|n|*|m|) too. |
| 116 |
|
| 117 |
2.6 Outer |
| 118 |
|
| 119 |
This operation is the union of the outer left and the outer right |
| 120 |
join. Makes it O(|n|*|m|) too. |
| 121 |
|
| 122 |
2.7 Outer_left |
| 123 |
|
| 124 |
O(|n|*|m|), very depending on the number of the fields, because they |
| 125 |
are compared. |
| 126 |
|
| 127 |
2.8 Outer_right |
| 128 |
|
| 129 |
Mirror operation of outer_lef |
| 130 |
|
| 131 |
2.9 Join |
| 132 |
|
| 133 |
Same as above. |
| 134 |
|
| 135 |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |