Nested loops

One way to join two tables is to use a nested loop. The principle is simple. Here is some pseudo code:

for x in table1:    for y in table2:          if x.field == y.field                issue row          else                keep doing 

Nested loops are often used if one of the sides is very small and contains only a limited set of data. In our example, a nested loop would lead to 100 million x 200 million iterations through the code. This is clearly not an option because runtime would simply explode.

A nested loop is generally O(n2), so it is only efficient if one side of the join is very small. In my example, this is not the case, so a nested loop can be ruled out to calculate the view.

Get Mastering PostgreSQL 10 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.