Recently, in the tuning of a customer to find a very interesting phenomenon, for a complex query (involving 12 tables) to establish the necessary index, the statement used by the IO dropped sharply, but the execution time does not fall, from the original 8 seconds to 20 seconds.
By observing the execution plan, we found that the previous execution plan used a hash join for many of the large table connections, and that the query optimizer chose to use parallel execution, faster because of the large number of data involved in the table. And we optimized the execution plan because of the existence of the index, and the data in the table is very large, the filter condition value in a very wide statistical information step range, resulting in a large number of estimated line deviation (filter condition is actually 15000 lines, the estimated average number of rows in the step is about 800 rows), so the query optimizer selected the loop Join, and no parallel execution is selected, so the execution time does not fall or rise.
Because the statement is implemented in a stored procedure, so we use a undocument query prompt directly to the statement, forcing the parallel cost threshold for the query to be dropped to 0, forcing the statement to go in parallel, and the statement execution time from 20 seconds to 5 seconds (note: Use a hash join hint for 7 seconds).
The following is a simple example showing the effect of using this hint, example T-SQL as shown in Listing 1:
SELECT * from
[adventureworks].[ Sales]. [SalesOrderDetail] A
INNER JOIN [Sales]. SalesOrderHeader B on
A.salesorderid=b.salesorderid
Code Listing 1.
The statement does not go in parallel by default and the execution plan is shown in Figure 1:
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/database/SQLServer/
Figure 1.
Let's add a hint to the statement below, as shown in Listing 2.
SELECT * from
[adventureworks].[ Sales]. [SalesOrderDetail] A
INNER JOIN [Sales]. SalesOrderHeader b on
a.salesorderid=b.salesorderid
OPTION (Querytraceon 8649)
Code Listing 2.
The execution plan then walks in parallel as prompted, as shown in Figure 2:
Figure 2.
When faced with 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, thereby reducing execution time.