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

从图中你看到了两个adhoc(即时查询),分别是我在第一步执行的join查询和我在第二步执行的这个select。

 

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

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

上面的query_plan字段就是所谓的执行计划,以xml的形式保存在字段中。。。所以说解析这个xml还是很费时间的。。。

  好了,到现在你应该认识到重新生成执行计划是不容易的。。。下一篇我们讨论讨论重用,重编译,重新生成等相关情况。

来源:路径丢了

Guess you like

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