Option (Force Order)
Today, share with you. Enforce join order in SQL Option (Force order)
SQL itself SQL Engine optimization has done very well, but also has the default multi-table connection engine effect does not reach the value we expect,
So we need to enforce our multi-table join order.
eg
CREATE TABLE #Student (RowId int identity (), Name varchar (), age int) GO
CREATE Table #Course (studentno int, Coursetype Varchar ()) GO
INSERT INTO #Student VALUES (' Tom ', 21)
INSERT INTO #Student VALUES (' Jerry ', 22)
INSERT INTO #Student VALUES (' John ', 23)
INSERT into #Course VALUES (1, ' languages ')
INSERT into #Course VALUES (1, ' math ')
INSERT into #Course VALUES (1, ' English ')
INSERT into #Course VALUES (1, ' chemistry ')
INSERT into #Course VALUES (2, ' languages ')
INSERT into #Course VALUES (2, ' math ')
INSERT into #Course VALUES (3, ' English ')
INSERT into #Course VALUES (3, ' chemistry ')
GO
By the result we obviously found that the SQL search engine by default chooses the #student table to do our base table to perform inner joins
But actually our second paragraph of SQL wants to #course as our base table execution
At this point we need Option Force order to enforce the order in our connection
SQL default engine optimization is not the optimal execution plan when this statement is what we expect.
Keep the query syntax indicated by the Join Order option (Force order)