Thursday, 29 December 2011

SQL server receives a user query

SQL server receives a user query; it follows well-defined steps to produce a result for user.

Step-1

First, it creates a compiled plan, which is a set of logical instructions such as how to join rows.

Step-2

Next, it creates an execution plan based on the compiled plan. This execution plan contains instructions with all logical references translated to actual objects, and tracking mechanisms for query execution.

Step-3

Finally, the server starts execution from the top of instruction tree.

 Creating a compiled plan is expensive because the server needs to find the optimal plan out of hundreds of potential candidates. Distinguishing between compile and execution helps overall server performance because each compiled plan can be cached and shared among multiple execution plans. The memory grant estimate follows the same overall sequence. It has parameters saved in compiled plan, and a mechanism to calculate actual grant size at execution time.

A successful query execution involves 3 major memory consumers: compile, cache, and memory grant.

  • Compile: Building and searching the most optimal plan out of hundreds candidates typically requires significant amount of memory. The lifetime of this usage is typically short because optimizer releases memory as soon as optimal plan is found. Lack of available memory would cause delays in compile, and potentially inefficient (slow) plans.

  • Cache: Finding optimal plan is costly in terms of CPU and memory usage. SQL server tries to store compiled plans in caches for later reuse. Lifetime of this memory usage is long-term. Lack of cache memory would cause more unnecessary re-compiles.

  • Memory grant: This memory is used to store temporary rows for sort and hash join. The lifetime of memory grant is the same as the lifetime of query. Lack of available memory grant causes a query to use hard disk, which affects query performance.

SQL Server maintains the balance between these 3 consumers with internal facility called "memory broker". Based on the usage and physical memory available, the memory broker sets the usage limit and tells each component to trim its memory usage if it anticipates a shortage. Generally, a well behaving server would have about the same contributions from these consumers.

Posted by: MR. JOYDEEP DAS

 

 

2 comments:

  1. This is really good one. We always not keep in mind at the time of query writing..

    Expecting more on Sql Server BI related to SSIS, SSAS, SSRS...

    ReplyDelete