From the course: Advanced SQL for Query Tuning and Performance Optimization

Unlock the full course today

Join today to access over 23,400 courses taught by industry experts.

Merge joins

Merge joins

- [Instructor] The merge join is the last join algorithm we'll examine. Merge joins, also known as sort merge joins, sort the input table prior to looking up rows by key values. By sorting, the algorithm can take advantage of ordering of the tables to reduce the number of rows that have to be checked. Let's look at an example. Assume we have performed the sort operations. In this example, we're joining two tables on region_id. The first row of the tables have a region_id of one, so we have a match and we can return that row. Next, we move to the second row of the larger table. Again, we have a match on the first row of the smaller table so we can stop after the first row in that table. Now, we move on and focus on the third row. There's no match with the first row of the second table, so we advanced to the next row of the second table. There's no match on the second row, so we advance to the third row of the second table and…

Contents