Query processing is performed by the Relational Engine in SQL Server. It is the process of taking the T-SQL statements you write and converting them into something that can make requests to the Storage Engine and retrieve the results needed.
SQL Server takes four steps to process a query: parsing, algebrizing, optimizing, and execution. They are shown in Figure 1. The first three steps are all performed by the Relational Engine. The output of the third step is the optimized plan that is scheduled, and during which calls are made to the Storage Engine to retrieve the data that becomes the results of the query you are executing.
The following sections briefly discuss parsing and algebrizing.
During the parsing stage SQL Server performs basic checks on the source code (your T-SQL batch). This parsing looks for invalid SQL syntax, such as incorrect use of reserved words, column and table names, and so on.
If parsing completes without errors, it generates a parse tree, which is passed onto the next stage of query processing, binding. The parse tree is an internal representation of the query. If parsing detects any errors, the process stops and the errors are returned.
The algebrization stage is also referred to as the binding stage. In early versions of SQL Server this stage was referred to as normalization. During algebrizing, SQL Server performs several operations on the parse tree and then generates a query tree that is passed on to the Query Optimizer.
The steps performed during algebrizing follow this model:
- Step 1: Name resolution — Confirms that all objects exist and are visible in the security context of the user. This is where the table and column names are checked to ensure that they exist and that the user has access to them.
- Step 2: Type derivation — Determines the final type for each node in the parse tree
- Step 3: Aggregate binding — Determines where to do any aggregations
- Step 4: Group binding — Binds any aggregations to the appropriate select list
Syntax errors are detected during this stage. If a syntax error is encountered, the optimization process halts and the error is returned to the user.