首页 > 代码库 > What happens when a SQL Query runs?

What happens when a SQL Query runs?

Hi Folks,

It has been such a long time that I have written on SQL Server.  Let’s go for a trip on SQL architecture and few things which really might interest us.

Every one of us write queries and expect SQL to return results within no time. Had any of us questioned yourself, what’s the mechanism inside SQL that is serving the requests of users and making us happy !!

Well, let’s see the architecture of SQL and how it behaves when we submit a query.

SQL Server Architecture and Life Cycle of a Query:

技术分享

The above picture clearly depicts that SQL Engine is split into two main engines: Relational Engine and Storage Engine.

The Relational engine is query processor as its functionality is query optimization and execution. Confused with the words optimization and execution ? Don’t worry, will discuss on that.

Storage engine– the name itself indicates that it is for managing SQL Server memory.

Buffer Pool:  This is the major component as it holds the SQL Server memory. The buffer pool contains plan cache and data cache. Plan cache is the one which is the storage area for execution plans and data cache holds the data.

What happens when we submit SELECT query ?

SNI (SQL Server Network interface): When we submit a query, we are actually interacting with the SQL engine, which means that some connection has been established between client (user) and server to have communication. SNI is a protocol layer which establishes connection between client and server. The connection is responsible for sending requests and receiving data.

Command Parser: This component in relational engine checks the syntax of the query and returns any errors that would reach the client via protocol layer. If the syntax is correct, query plan will be generated and will be checked against the plan cache. If the plan already exists in plan cache, it will be reused. However, if it’s not found, query tree would be generated (each node in the tree represents operation to be preformed) and this will be passed as input to query optimizer.

Optimizer: It is the major component in SQL. It finds out multiple ways to execute a query and finds out the best plan (with low cost and that would take less time to execute). Finding out the efficient plan brings out the capability of optimizer.

Query Executor: The name itself is self-explanatory. It executes the query plan i.e executes each step in the plan, interacts with storage engine to retrieve/modify data.

In order to access data, query executor interacts with storage engine via OLEDB to access methods.

Access Methods: It contains several methods to retrieve the data. This actually sends the request to buffer manager.

Buffer Manager: It manages the buffer pool. If some records need to be fetched, buffer manager checks data cache and the data pages are already cached, they would be fetched and passed back to access methods. If the data pages are not cached, they would be read from the disk, put it on data cache and then the results would be passed to access methods.

Transaction Manager: This has two major components- Lock manager and Log manager. The Lock manager provides concurrency to the data and maintains isolation levels. The Log manager writes the changes to the transaction log.  As and when we hear about log and lock manager we would be landing up with many questions. Let’s take a break here and have deep dive when I write about  transaction logs and isolation levels.

What happens when a SQL Query runs?