Post · 11 min read· pp. 167–173

Functional Dependencies and Armstrong's Axioms For Query Optimization

Consider this SQL query:

sql
SELECT a, sum(b) FROM (SELECT a, a+1 AS a2, b FROM ab) GROUP BY a, a2

In English, for each row in ab, compute a2 as a + 1. Then, for every set of rows having the same value of (a, a2), compute a and the sum of their b values.

With a little thought, you might be able to convince yourself that we can simplify this query to

sql
SELECT a, sum(b) FROM ab GROUP BY a

because any two rows that share a value of (a) will also share a value of (a, a2) and vice versa, so changing the grouping columns has no effect on the computation.

The art of query planning is making these observations then figuring out how to mechanize them so a database can apply them automatically. The way we automate this particular kind of observation is via a surprisingly powerful tool called Functional Dependencies (FDs).

Functional Dependencies

Let’s play a game! You and I are going to agree on a relation. Say, this one, which tells us where various people live.

nameregiontown
ForteCAPasadena
PaulNYNew York
IliaONOakville
BilalONMississauga
JustinONMississauga
IrfanONToronto

I’m going to pick a row and tell you the value of name, and you have to tell me the value of town. This is a pretty easy game for you: you always have a winning strategy since you can always tell exactly which row I was looking at. If I tell you “Ilia,” you know the value of town: it has to be “Oakville.” Likewise, if I tell you “Justin,” you know the value of town: it has to be “Mississauga.”

Let’s flip the game on its head. I pick a row and tell you the value of town, and you have to tell me the value of name for that row. This one is harder: if I tell you the value of town is Mississauga, you don’t know if I’m looking at the “Bilal” row or the “Justin” row.

In the first case, you could win because the value of name determines the value of town, in the second case, you couldn’t, because town does not determine name.

Let’s try a third case. I tell you the value of town, and you tell me the value of region. You can actually win this one too. Even though you can’t tell me which row I’m looking at, it doesn’t matter, since every row I could be looking at has the same value of region. town determines region.

In some relation RR, if by knowing the values of some set of columns XX, we can determine the values of some set of columns YY, we say the following:

  • XX functionally determines YY in RR,
  • YY functionally depends on XX in RR, or
  • R⊨X→YR \models X \rightarrow Y (read “RR satisfies X→YX \rightarrow Y").

The expression X→YX \rightarrow Y is called a functional dependency.

Let’s return to the query we opened on:

sql
SELECT a, sum(b) FROM (SELECT a, a+1 AS a2, b FROM ab) GROUP BY a, a2

Once we turn this query into an initial query plan, it will look something like this:

text
Group By {Output: [a, sum(b)], GroupingCols: [a, a2]}
                        ^
                        |
                Map {[a + 1 AS a2]}
                        ^
                        |
                     Scan ab

Our query planner will then annotate each operator with the FDs it is able to infer for its output. Since a + 1 AS a2 is computed deterministically from a, we can infer the FD {a}→{a2}\{\texttt{a}\} \rightarrow \{\texttt{a2}\} and annotate our Map operator with it (if we were clever we might be able to infer the reverse direction as well):

text
Group By {Output: [a, sum(b)], GroupingCols: [a, a2]}
                        ^
                        |
                Map {[a + 1 AS a2]}
                  - FDs:[{a} -> {a2}]
                        ^
                        |
                     Scan ab

We then have an optimization rule on our GroupBy operator that looks at the FDs satisfied by the input relation and checks if our grouping columns have any redundancies: given a set XX, we might be able to find a smaller set X′X' such that X→X′X \rightarrow X' and X′→XX' \rightarrow X, meaning for any pair of rows, if they match on one, they match on both, which is exactly the condition we need to change our grouping columns. Once we determine that {a}→{a,a2}\{\texttt{a}\} \rightarrow \{\texttt{a}, \texttt{a2}\} and {a,a2}→{a}\{\texttt{a}, \texttt{a2}\} \rightarrow \{\texttt{a}\}, we can simplify our query:

text
Group By {Output: [a, sum(b)], GroupingCols: [a]}
                        ^
                        |
                Map {[a + 1 AS a2]}
                  - FDs:[{a} -> {a2}]
                        ^
                        |
                     Scan ab

and as one final step, we will recognize that as a2 is no longer needed, we can avoid computing it altogether:

text
Group By {Output: [a, sum(b)], GroupingCols: [a]}
                        ^
                        |
                     Scan ab

This is just one application of FDs but there are many others. We will see more about how to manipulate FDs mechanically a little later.

Keys

A key of a relation is a set of columns which uniquely identify a row. The term “key” is meant to evoke the idea that we given a set of values for one, we could, in theory, “look up” a row by them.

More concretely, a set KK of columns is a key in RR if no two rows in RR have the same values for KK.

nameregiontown
ForteCAPasadena
PaulNYNew York
IliaONOakville
BilalONMississauga
JustinONMississauga
IrfanONToronto

In this relation, {name}\{\texttt{name}\} is a key, as are {name,region}\{\texttt{name}, \texttt{region}\}, {name,town}\{\texttt{name}, \texttt{town}\}, and {name,region,town}\{\texttt{name}, \texttt{region}, \texttt{town}\}. {region}\{\texttt{region}\} is not a key, because several rows share the same value of region\texttt{region}.

In SQL, keys are enforced via integrity constraints like UNIQUE or PRIMARY KEY, but keys can easily arise in the middle of computation in a variety of ways, for instance, the grouping columns of a Group By operation will always form a key in its output.

Mechanically Manipulating FDs

If we’re going to use these constructions to mechanically optimize our queries, we need tools to manipulate them in a mechanical way. The tools used to manipulate FDs are a set of rules called Armstrong’s Axioms, and they are as follows (set union, normally written A∪BA \cup B, in this field is written ABAB, for some reason):

  • If Y⊆XY \subseteq X, then X→YX \rightarrow Y [Ref].
  • If X→YX \rightarrow Y, then XZ→YZXZ \rightarrow YZ [Aug].
  • If X→YX \rightarrow Y and Y→ZY \rightarrow Z, then X→ZX \rightarrow Z [Tra].

The Juice

Armstrong’s Axioms give us the ability to generate new FDs from ones we are already aware of. This manifests in a handful of ways, but the most useful is an algorithm to determine if a given functional dependency holds.

The F\mathcal F-closure of a set of columns XX is written XFX^\mathcal F and is the set of columns which are functionally dependent on XX. Put another way, “if you know the values of XX, what other values do you also know?”

There is a very simple algorithm to compute XFX^\mathcal F: you start with XX and just keep adding columns implied by it until you can do so no longer:

  1. Let Y=XY = X.
  2. For each FD in F\mathcal F, if the left side is contained in YY and the right side is not, then add the right side to YY and start over.
  3. Output XF=YX^ \mathcal F = Y.

To check if X→YX \rightarrow Y is implied by F\mathcal F, check if Y⊆XFY \subseteq X^{\mathcal F}.

If you are building a query planner and looking for the actionable, useful thing to take away here, this is it: this is how you manipulate FDs in your code.

We’ll define this more precisely and also see that this algorithm is indeed correct later on.

The Squeeze

Now, let’s figure out if Armstrong’s Axioms are indeed appropriate for reasoning about FDs. When we have a set of axioms for manipulating statements like this, we care that they satisfy two important properties:

  • They should not let us derive any statements which are false. This is called soundness.
  • They should let us derive any statements which are true. This is called completeness.

It’s very easy to satisfy either one of these in isolation. If we say “you are not allowed to derive any new statements,” we are guaranteed to be sound, because we certainly will not derive something false if we can derive nothing at all. If we say “you can derive anything you want,” we are guaranteed to be complete, because if we can prove anything, we certainly can prove anything which happens to be true.

So clearly, we care about satisfying them both at the same time.

It should be not too hard to convince yourself Armstrong’s Axioms are sound, they’re all just true facts about relations. That Armstrong’s Axioms are complete is less obvious.

Completeness of Armstrong’s Axioms

To prove Armstrong’s Axioms are “good,” our general approach is going to be:

  • Define what it means for an FD to “hold” (logicians call this semantics).
    • If X→YX \rightarrow Y holds in F\mathcal F, we write F⊨X→Y\mathcal F \models X \rightarrow Y.
  • Define what it means for an FD to be “provable” from Armstrong’s Axioms (logicians call this syntax).
    • If X→YX \rightarrow Y is provable from F\mathcal F, we write F⊢X→Y\mathcal F \vdash X \rightarrow Y.
  • Prove “soundness,” (we won’t bother showing this explicitly) meaning F⊢X→Y\mathcal F \vdash X \rightarrow Y implies F⊨X→Y\mathcal F \models X \rightarrow Y (“if it’s provable, it’s true”).
  • Prove “completeness,” meaning F⊨X→Y\mathcal F \models X \rightarrow Y implies F⊢X→Y\mathcal F \vdash X \rightarrow Y (“if it’s true, it’s provable”).
  • Once we’ve shown that ⊨\models and ⊢\vdash always agree, we conclude that we’re on the same page as our computer when it comes to FDs.

Defining Semantics

If F\mathcal{F} is a set of functional dependencies, we write R⊨FR \models \mathcal F ("RR satisfies F\mathcal F") if R⊨fR \models f for all f∈Ff \in \mathcal F (note that we’re overloading ⊨\models here).

For R⊨FR \models \mathcal F to be true, even though we only require specifically the things in F\mathcal F, there might be some additional consequences. For instance, say (\mathcal F = {{\texttt{a}} \rightarrow {\texttt{b}}, {\texttt{b}} \rightarrow {\texttt{c}} }). If R⊨FR \models \mathcal F, then it’s actually also the case that (R \models {\texttt{a}} \rightarrow {\texttt{c}} ) (knowing a\texttt{a} means you know b\texttt{b}, knowing b\texttt{b} means you know c\texttt{c}, hence, knowing a\texttt{a} means you know c\texttt{c}).

If every relation RR that satisfies F\mathcal F also satisfies X→YX \rightarrow Y, we say F⊨X→Y\mathcal F \models X \rightarrow Y.

Defining Syntax

A functional dependency is a string X→YX \rightarrow Y, where XX and YY are subsets of some attribute set A\mathcal A.

A set F\mathcal F of functional dependencies is axiom-closed if AAs hold within it:

  • Y⊆XY \subseteq X implies X→Y∈FX \rightarrow Y \in \mathcal F,
  • X→Y∈FX \rightarrow Y \in \mathcal F implies XZ→YZ∈FXZ \rightarrow YZ \in \mathcal F,
  • X→Y∈FX \rightarrow Y \in \mathcal F and Y→Z∈FY \rightarrow Z \in \mathcal F together imply X→Z∈FX \rightarrow Z \in \mathcal F.

The closure of a set F\mathcal F, written F+\mathcal F^+, is the smallest axiom-closed set that contains F\mathcal F.

Now XFX^{\mathcal{F}} is now the largest YY such that X→Y∈F+X \rightarrow Y \in \mathcal F^+.

Completeness of Armstrong’s Axioms

This post came to be because I was lamenting to Forte that I didn’t understand why Armstrong’s Axioms were complete, and he came up with and showed me about four different proofs.

This is my attempt at paraphrasing the one that made the most sense to me.

Claim 1:

F⊨X→Y implies Y⊆XF\mathcal{F} \models X \rightarrow Y \text{ implies } Y \subseteq X^\mathcal{F}

Proof of claim 1

Contrapositive:

Y⊈XF implies F⊭X→YY \not\subseteq X^\mathcal{F} \text{ implies } \mathcal{F} \not\models X \rightarrow Y

We must show there is some relation RR such that R⊨FR \models \mathcal F and (R \not\models X \rightarrow Y).

The following is such a relation:

text
        X^F         |       not(X^F)
-------------------------------------------
1 | 1 | ... | 1 | 1 | 1 | 1 | ... | 1 | 1 |
1 | 1 | ... | 1 | 1 | 0 | 0 | ... | 0 | 0 |

This satisfies F\mathcal{F} (not hard to check) but not X→YX \rightarrow Y since YY intersects XF‾\overline{X^\mathcal F} and the two rows differ there, but not in XX.

Claim 2:

Y⊆XF implies F⊢X→YY \subseteq X^\mathcal{F} \text{ implies } \mathcal{F} \vdash X \rightarrow Y

Proof of claim 2

Since Y⊆XFY \subseteq X^\mathcal{F}, F⊢XF→Y\mathcal{F} \vdash X^\mathcal{F} \rightarrow Y by Ax1. By the definition of XFX^\mathcal{F}, (\mathcal{F} \vdash X \rightarrow X^\mathcal{F}), and then by transitivity F⊢X→Y\mathcal{F} \vdash X \rightarrow Y.

Mechanically Manipulating Keys

We can extend Armstrong’s Axioms to handle keys by adding the following two axioms:

  • If key(K)\text{key}(K), then K→AK \rightarrow \mathcal A (where A\mathcal A is the set of all columns) [K1].
  • If key(A)\text{key}(A) and B→AB \rightarrow A, then key(B)\text{key}(B) [K2].

I’m sure it’s not novel, but I haven’t seen this particular presentation of keys before.

Extending our FD language with these two new rules is also sound and complete. It’s not too hard to show using the fact that Armstrong’s Axioms themselves are complete.

Conclusion

There’s two steps to any optimization we want to apply:

  1. Observe some kind of redundancy or unnecessary work according to our understanding of how relations work.
  2. Figure out how to translate that into some kind of mechanical system that can be reasoned about by a computer.

These two steps correspond very roughly to semantics and syntax from logic: understanding what something means, how we represent it, and being confident those two ideas are in alignment, are all important, and we need a wide arsenal of these tricks for a query optimizer to do its best work.

Thanks Forte, Madhav, Ilia, Paul, and Jordan for looking over this post.