About SQL Server execution plan cache (below)

Source: Internet
Author: User
In the previous article, we talked about the relationship between the query optimizer and the Execution Plan cache, and the conflicts between the two. In this article, we will mainly discuss the common problems of execution plan caching and some solutions. The process of taking execution cache into account the previous article mentioned the process of parsing statements by the query optimizer. When planning cache is taken into account

In the previous article, we talked about the relationship between the query optimizer and the Execution Plan cache, and the conflicts between the two. In this article, we will mainly discuss the common problems of execution plan caching and some solutions. The process of taking execution cache into account the previous article mentioned the process of parsing statements by the query optimizer. When planning cache is taken into account

Introduction

In the previous article, we talked about the relationship between the query optimizer and execution plan cache, website space, and conflicts between the two. In this article, we will mainly discuss the common problems of execution plan caching and some solutions.

 

Process for taking execution cache into account

The previous article mentioned the process of parsing statements by the query optimizer. when taking into account the plan cache, you must first check whether the plan cache contains the statement cache. If not, will execute the compilation process, if there is, directly use the compiled execution plan. Therefore, the complete process 1 is shown.

. Process of taking plan cache into account

 

We can see that one step is to find the plan process in the cache. Therefore, it is difficult to guess that as long as this type of search is used, the Hash data structure cannot be run. You can find information about the Hash table through the DMV sys. dm_ OS _memory_cache_hash_tables, as shown in figure 2. It is worth noting that when there are too many execution plans that lead to too many objects in the Same Bucket after the hash, additional buckets are required, which may lead to low cache efficiency of the search plan. The solution is to minimize the number of plans in the Plan cache, which will be discussed later in this article.

. Information about HashTable cached by the storage plan

 

When this problem occurs, we can see the problem in the buckets_avg_scan_miss_length column. In this case, the Cache Hit rate (SQL Server: Plan Cache-Cache Hit Ratio) is relatively high, but the compilation time is too long.

 

Parameterization and non-parameterization

The only identifier of a query plan is the query statement, but assuming that the statement body is the same, but only the query condition predicates are different, is one execution plan or two execution plans in the execution plan? It's Depends.

Assume the following two statements, as shown in 3.

. Only two statements with different predicates

 

Although the execution plan is the same, two execution plans are retained in the execution plan cache, as shown in figure 4.

. The same statement and different conditions have two different execution plan caches.

 

We know that the execution plan cache depends on the query statement itself to identify the cache. Therefore, the preceding two statements are considered as two different statements in the execution plan cache. The solution to this problem is to make the query statements in the execution plan cache identical.

 

Parameterization

So that only some parameters are different, and the same query statement can be reused, It is the significance of parameterization. For example, if we enable mandatory database parameterization, Hong Kong server lease, or use stored procedures. SQL Server enforces parameters of these statements. For example, we modified the database-level options.

. Database-level options

 

Now let's execute the two statements. by querying the execution plan cache, we find that the variable is parameterized, so that the statements in the Plan cache become consistent, as shown in 6, so that they can be reused.

. Query statement after Parameter

 

However, mandatory parameters may cause some problems. The query optimizer often cannot optimize some specific queries based on statistical information. For example, it cannot apply some indexes or search during the scan. The negative effects have been mentioned in the previous article. I will not elaborate on them here.

Therefore, there are several solutions to the above problems.

 

Balanced parameterization and non-parameterization

Under specific circumstances, parameterization is sometimes good, but sometimes it is the culprit of performance problems. Let's look at several ways to balance the relationship between the two.

 

Use RECOMPILE

When the cost of an inaccurate execution plan is higher than that of the compiled VM during the query, if you use the RECOMPILE option in the stored procedure or use the RECOMPILE prompt in the ad hoc query, the execution plan will be regenerated for each query, this parameter prevents the generated execution plan from being inserted into the execution plan cache. For OLAP queries, inaccurate execution plans often cost much more than compilation costs. Therefore, you can consider this parameter or option, you can use Hint as shown in the query in code list 1.

SELECT * FROM Sales. CustomerWHERE CustomerID> 20000 AND TerritoryID = 4 OPTION (recompile)

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.