Recently in the tuning of a customer to find a very interesting phenomenon, for a complex query (involving 12 tables) to establish the necessary indexes, the statement used by the IO dropped sharply, but the execution time does not drop the reverse rise, from the original 8 seconds to 20 seconds.
By observing the execution plan, it was found that the previous execution plan used a hash join in many large table connections, and the query optimizer chose to use parallel execution because of the large number of data involved in the table. And we optimized the execution plan due to the existence of the index, and the data in the table is very large, the value of the filter in a wide range of statistics, resulting in a large deviation of the estimated number of rows (the filter is actually 15000 rows, the estimated average number of rows in the step is about 800 rows), so the query optimizer chose the loop Join, and no parallel execution is selected, so the execution time does not fall back.
Since the statement is implemented in a stored procedure, we use a undocument query hint directly on the statement, which causes the parallel cost threshold of the query to be reduced to 0, which forces the statement to go parallel and the statement execution time from 20 seconds to 5 seconds (note: Using the hash join hint is 7 seconds).
The following is a simple example of the effect of using this hint, as shown in Listing 1 of the sample T-sql:
from [AdventureWorks]. [Sales]. [SalesOrderDetail] A on a.salesorderid=b.salesorderid
Code Listing 1.
The statement does not go in parallel by default, as shown in Plan 1:
Figure 1.
Let's add a hint to the statement, as shown in Listing 2.
SELECT * from [adventureworks].[ Sales]. [SalesOrderDetail] A JOIN [Sales]. SalesOrderHeader b on a.salesorderid=b.salesorderid OPTION (querytraceon 8649)
Code Listing 2.
At this point the execution plan will follow the prompts to walk in parallel, as shown in 2:
Figure 2.
When you encounter a similar situation with some complex DSS or OLAP queries, consider using the undocument hint to require SQL Server to use parallelism as much as possible, thus reducing execution time.
Forcing SQL Server execution plan to use parallel elevation for performance under complex query statements