Why Multiple JOINs are bad for Query or Do Not Get in the Way of Optimizer

Recently, I came across an application that generated DB queries. I understand that there is nothing new about that, but when application began running slow and I had to find out the reason of the slowdown, I was amazed to find these queries. Here is what SQL Server sometimes has to deal with:

Names of the objects have been changed.

The most striking thing was that the same table was used multiple times, and the number of parenthesis simply drew me crazy. I was not the only one who didn’t like this code, SQL Server didn’t doesn’t appreciate it as well and spent much resources for building a plan for it. The query may run for 50-150 ms, and plan creation may take up to 2.5 ms. Today, I will not consider the ways to fix the issue, but I will tell one thing – in my case, it was impossible to fix query generation in the application.

Instead, I would like to analyze the reasons why SQL Server builds the query plan for so long. In any DBMS, including SQL Sever, the main optimization issue is the method of joining tables with each other. Besides the join method, the sequence of table joins is very important.

Let’s talk about the sequence of table joins in detail. It is very important to understand that the possible number of table joins grows exponentially, not linearly. Fox example, there are only 2 possible methods to join 2 tables, and the number can reach 12 methods for 3 tables. Different join sequences can have different query cost, and SQL Server optimizer must select the most optimal method. But when the number of tables is high, it becomes a resource-intensive task. If SQL Server begins going over all possible variants, such query may never be executed. That is why, SQL Server never does it and always looks for a quite good plan, not the best plan. SQL Server always tries to reach compromise between execution time and plan quality.

Here is an example of the exponential growth of join methods. SQL Server can select various join methods (left-deep, right-deep, bushy trees). Visually, it looks in the following way:

Exponential growth of join methods

The table below shows the possible join methods when the number of tables increases:

You can get these values on your own:

For left-deep: 5! = 5 x 4 x 3 x 2 x 1 = 120

For bushy tree: (2n–2)!/(n–1)!

Conclusion: Pay special attention to the number of JOINs and do not get in the way with optimizer. If you fail to get the desired result in the query containing multiple JOINs, break it in several small queries and you will be surprised with the result.

P.S. Of course, we must understand that besides defining a sequence of table joins, the query optimizer must also select the join type, the data access method (Scan, Seek) etc.

Dmitry Zaytsev

Dmitry Zaytsev

Dmitry started working with SQL Server back in 2010 and how has large experience in various aspects of working with SQL Server, including administration, development, migration, monitoring, performance, etc. Dmitry is the founder of sqlcom.ru
Dmitry Zaytsev

Dmitry Zaytsev

Dmitry started working with SQL Server back in 2010 and how has large experience in various aspects of working with SQL Server, including administration, development, migration, monitoring, performance, etc. Dmitry is the founder of sqlcom.ru