Force SQL Server Execution plan to use parallel elevation for performance under complex query statements

Source: Internet
Author: User
Tags execution hash join query

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.