Monday, December 5, 2011

Concept behind SQL Query execution

Summary :

When you submit a query to a Sql Server DB then a number of process on the server go to work on that query.
- Some process occure in the Relational Engine
- Some process occure in the Storage Engine

In the relational engine the query is parsed and then processed by the query optimizer, which genarate an execution plan. The plan is sent(in binary formate) to the storage engine, which it then uses to retrieve or update the underlying data.

The process in storage engine such as locking, index maintenence and transection occure.

Note : The execution plan is created in the relational engine.

Details :

When you pass a T-Sql statement to the Sql Server system then the first palce it goes to is the relational engine. As the T-Sql arrives then it passes througn a process that check the T-Sql syntax. This process is known as Query Parsing. The output of this process is a Parse Tree.

Then, this parse tree is passed to a process called the Algebrizer. The algebrizer resolves all the names of the various objects, tables and columns, reffered to within the query string. The algebrizer indentify (at the individual level) all the types (varchar(50) vs nvarchar(25) and so on) of the object being occure. It also determine the location of the aggregate function within the query, a process called Aggregation Binding. The algebrizer process is important because the quey may have the alias or synonms name that does not exist in the DB, that need to be resolved. The output of the algebrizer is a binary called Query Processor Tree which is then passed to the Query Optimizer.

Then using the query processor tree (o/p of algebrizer) and the statistics it has about the data and applying the model The Query Optimizer generate an execution plan for query. The optimizer will generate and evalute many plans and will choose the lowest cost plan.

Once the optimizer arrives at an execution plan, the actual plan is created and stored in a memory space known as Plan Cache. - Unless an identical plan is already exist in the cache. As the optimizer generates potential plan, it compare them to previously generated plans in the cache. If it find a match the it will use that plan.

After the plan is finilized the plan is passed to the Storage engine and in the storage engine, many process like locking, indexes mentainence and tranction are performed and finally the query is executed accoring to the plan And result is displayed.


Reuse of Execution Plan

It is expensive for server to generate the execution plan so sql server will keep and reuse plans whenever possible.

When a query is submitted to the server then an estimated plan is created by optimizer. Once that plan is created and before it get passed to the storage engine, the optimizer compare this estimated plans to actual plans that already exist in the plan cache. If an actual plan is found that matches the estimated one then the optimizer will reuse the existing plan, since it is already been used before by the query engine. This reuse of plan, avoids the overhead of creating actual plan for large and complex queries.

Each plan is stored once. Execution plans are not kept in memory these are stored in Cache (Plan Cache).

The lazywriter process, an internal process that work to free all types of cache (including plan cache using AgeFormula), periodically scans the objects in the cache and decrease this value by one each time.

No comments:

Popular Posts