Currently, Drill's planner does not consider all the possible join order sequence during the planning phase, because one particular optimizer rule (SwapJoinrule) is not enabled. The reason of not enabling this rule is because it would increase the planning time significantly otherwise.
This means that the join sequence for some queries might not be optimal; the sequence in the FROM clause would impact what the final join sequence the planner would get. For example,
The "nation" table contains 25 rows, while "customer" table contains 1.5 million rows. The optimal plan should put "customer" on the left side of hash inner join, and "nation" on the right side, since hash table is built on right side, and we would like to have hash table built on smaller dataset.
However, currently Drill planner will get the following join sequence : NATION --> CUSTOMER.
Notice in the above plan, LEFT is "nation" table, while RIGHT is "customer" table.
Before we resolve the increased planning time related to "SwapJoinRule", as a workaround for now, I would like to propose that we swap the inputs for hash inner join, after the planner finishes the planning. That is, when we build the physical plan to be run on Drill's execution engine, we swap the inputs for hash inner join physical operators, based on row count comparison.
The proposed workaround could cause performance regression for some queries, in particularly because the estimated row count is not accurate (especially after Filter / Join / Aggregation) due to lack of complete statistics.
To remedy that regression risk, we will add a new planner option for this feature, so that user could turn on/off this feature, if they see performance regression.
With this feature enabled, the above query will get the plan like :
Please note that once we resolve the issue of SwapJoinRule, we should remove this workaround solution in Drill's code.