Forcing SQL Server execution plan to use parallel elevation for performance under complex query statements

Source: Internet
Author: User

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:

SELECT *
From
INNER JOIN [Sales]. SalesOrderHeader b
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
INNER 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.

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.