Entering content frame

 Joins 

Before you can understand how the Optimizer finds search strategies for joins, you need some information about how the database system processes joins.

Creating Results Tables

Joins are statements that join tables together. The tables are joined together in a series of steps:

...

       1.      The first table is scanned.

       2.      The result is stored in a temporary results table.

       3.      The temporary results table is sorted by the join columns of the following join step.

       4.      The next table that still exists is scanned.

       5.      The existing temporary results table is joined with the new table.

       6.      The result is stored in a new temporary results table.

       7.      This temporary results table is sorted by the join columns of the following join step.

       8.      The old temporary results table is deleted.

       9.      If there are more tables, the procedure is repeated from step 4.

The last temporary results table is the results table that the user wants.

Order of Processing for Join Tables

The aim of an optimization is to save time and memory. For joins, this means that the rows of the new tables that you want to join must be accessed as directly as possible. The temporary results tables must be as small as possible.

For this reason, the Optimizer starts to process joins for the smallest tables with the most restrictive search conditions. Initially, this keeps the temporary results tables small. This means that the Optimizer itself specifies the order in which the join tables are processed; the order in which the tables are specified in the FROM clause of the SELECT statement is not relevant here.

Search Strategies for Joins

The following search strategies can be used to access rows in the new table, starting from the join column values of the old temporary results table.

JOIN VIA INDEXED COLUMN

JOIN VIA KEY COLUMN

JOIN VIA KEY RANGE

JOIN VIA MULTIPLE INDEXED COLUMNS

JOIN VIA MULTIPLE KEY COLUMNS

JOIN VIA RANGE OF MULTIPLE INDEXED COL.

JOIN VIA RANGE OF MULTIPLE KEY COLUMNS

If the two columns that you want to compare in a join step do not have the same length, then not all of the search strategies can be used. To avoid this restriction, define the same value range for any columns that you want to join together.

You can use the EXPLAIN statement (EXPLAIN Statement for Joins) to find out the search strategy chosen by the Optimizer.

 

Leaving content frame