Thursday 29 July 2021

How an Execution Plan is Picked by SQL Optimizer for SQL Server


The Query Optimizer for SQL Server is designed to be cost-based. This means it assesses several execution plans and estimates each one’s cost before picking the plan that seems the most cost-effective.

Since this tool is unable to take every single plan into consideration, it ends up performing a balancing act by taking into account the costs involved in both - locating possible plans and implementing them. In this blog, we will look at the SQL optimizer for SQL Server and the steps involved in its process of identifying the best execution plan.


Oracle SQL Query Execution Plan Picking Process

The SQL Server Database Engine is made of two major parts - the Relational Engine (Query Processor) and the Storage Engine. The Relational Engine is responsible for all the statements sent to the SQL Server. It is tasked with creating a plan that ensures optimal execution to deliver the best results.

The Storage Engine, on the other hand, is in charge of optimally reading data from both memory and disk while preserving data integrity. Statements are sent to SQL Server through SQL language(s). SQL only specifies what type of data needs to be fetched and not the methods that can be used for this purpose. Neither does it present any of the algorithms that can be applied to process the request.



Therefore, the first step the relational engine takes when it gets an Oracle SQL query is devising a plan as fast as it can. The plan must mention the most favorable or even efficient method to run said statement. The next thing to do is to use that plan to execute the statement.

All the steps are assigned to individual parts inside the query processor. Inside, it is the job of the Query Optimizer to design a plan, which it submits to the Execution Engine to execute to receive results accordingly.

Steps Involved in Creating an Execution Plan to Carry Out a Query

Considering the SQL optimizer for SQL Server, it takes a certain number of steps before the Relational Engine can provide the Execution Engine with the best or an efficient-enough execution plan. These steps have been mentioned here:

       Parsing

       Binding

       Optimizing the Query

       Running it

 


  1. Parsing & binding - The statement, supposing it is correct, undergoes the parsing and binding process. A valid query has its output in the form of a logical tree that has nodes symbolizing each logical operation being mentioned in the statement. For example, performing an inner join or a read action in the specific table will both be represented as separate nodes in the logical tree.
  2. Query Optimization - The logical tree helps in the query optimization procedure that is made up of the following steps -

    1. Creating Potential Execution Plans – With the help of the logical tree, the Relational Engine or the Query Optimizer generates more than one potential method (execution plan) to run the query without incurring excessive costs. An Execution plan is essentially a bundle of physical tasks that must be carried out in order to provide the expected results as represented by the logical tree. These operations are part of the Oracle SQL query and include tasks such as nested loop joins and index seeks.
    2. Evaluating the Cost of Every Plan – Even though the Relational Engine doesn’t create each and every execution plan that can be generated, it does examine the expenses incurred by every plan it actually creates, in terms of cost and resources. Among these, it selects the plan whose expenses are evaluated and found to be the lowest. This plan is then relayed to the Execution Engine.
  1. Running the Query and Caching the Plan – The statement is run as per the plan that has been selected and sent to the Execution Engine by the Query Optimizer. This plan can be saved in a designated area known as the plan cache, which is located in memory.

 


No comments:

Post a Comment

What do You Mean by Oracle Performance Tuning?

Performance tuning is a process in which we fine-tune a database to improve its operational performance. This process includes working on pe...