Understanding and fixing joins
Joins are important; everybody needs them regularly. Consequently, joins are also relevant for maintaining or achieving good performance. To ensure that you can write good joins, we’ll also learn about joining in this book.
Getting joins right
Before we dive into optimizing joins, it’s important to take a look at some of the most common problems that arise with joins and which of them should sound alarm bells for you.
Here’s an example of a simple table structure to demonstrate how joins work (make sure the table we used before has been deleted):
test=# CREATE TABLE a (aid int); CREATE TABLE test=# CREATE TABLE b (bid int); CREATE TABLE test=# INSERT INTO a VALUES (1), (2), (3); INSERT 0 3 test=# INSERT INTO b VALUES (2), (3), (4); INSERT 0 3
Two tables containing a couple of rows have been created.
The following example shows a simple outer join:
test=# SELECT * FROM a LEFT JOIN b ON (aid = bid); aid | bid -...