sqlserver implementation plan

We know in our top sql personnel to the underlying opened a window, that is the implementation plan, the arrival of the implementation plan, we know how those rotten sql is executed, so

You can easily find the defects and optimize the point of sql.

One: the execution plan generation process

  When it comes to the implementation plan, first thing to know about is the execution plan generated by the process, so that you can do on an idea, here I painted sketch:

1. analysis

  These three relatively easy to understand, we need to ensure that the syntax sql can not be wrong, select and join the table must exist, and you have permission to execute this sql, right. . .

We go over first process execution program life cycle.

2. The compilation process

      Sql ensure the above three points, then the engine will have to bite the bullet and you see such a large pile of rotten sql, the deleted delete, change the change, the conversion conversion, such as your "subquery" will translate into

"Table Connection" and so on. . . In fact, quite hard for the engine, give you an example.

<1> produced by the subquery sql:

<2> join generated sql:


The results from the two above, you can see that we are all playing join, and if you look closely, you will find is a "hash match", one is "nested loop", why not the same, this

Of course, the engine is based on a comprehensive selection out of many situations, such as: disk IO, logical reads, resource consumption, hardware environment and so on. . . This is the so-called "Plan optimal selection" operation.


3. execution

  Since the implementation of the plan were elected, of course, we will perform, and after the implementation will sql and implementation plan into the cache, so the next time the same sql have come directly from the can

Cache extracted, and does not require re-generation plan, you also see the execution plan is quite time-consuming CPU.


Two: to see cached plan and execute the sql

  Just said, sql and plan have been placed in the cache, that my curiosity is stronger, and I wanted to see sql plan Where and what is ugly long way, just

sqlserver is quite able to meet our G-spot.

1. In order to facilitate the view cache, first of all I need to clear the cache, such as the following statement.

DBCC freeproccache
SELECT c.* FROM dbo.Category AS c
JOIN dbo.Product AS p
ON c.CategoryId=p.CategoryId
WHERE c.CategoryId=23794

2. 通过sys.dm_exec_cached_plans拿到sql和plan的指针(plan_handle),如下图

SELECT * FROM sys.dm_exec_cached_plans



3. 现在我们已经拿到了2个adhoc的plan_handle,然后通过dm_exec_sql_text查看他们的sql分别是怎样?

4. 看完text缓存,接下来我们继续看看sql的plan缓存在哪?可以通过dm_exec_query_plan来查看。




Guess you like

Origin www.cnblogs.com/Tony100/p/10967350.html