When a query arrives at the database engine,SQL ServerPerform two major steps to generate the expected query results. The first step is query compilation, which generates the query plan, and the second step executes the query plan.
SQL Server 2005The query compilation in consists of three steps:Analysis and AlgebraAndQuery Optimization. After completing these steps, the compiler saves the optimized query plan to the process cache. Here, the execution engine converts the Plan into an executable form, and then executes the steps to generate the query results. If the plan is included in the process cache when the same query or stored procedure is executed again in the future, skip the compilation step and reuse the cached plan to execute the query or stored procedure.
about SQL server optimization :
SQL Server Does not optimize each statement in the batch processing. Only statements that access tables and may generate multiple execution plans are optimized. SQL Server Optimize all DML ( Data Operation Language ) Statement , That is Select , Delect And Update Statement. Besides DML , Others T-SQL The statement will also be optimized, Create Index This is one of them. Only the optimized statements generate a query plan.
about SQL groups in :
Group Or Having Explicit clauses such Select The only factor that the list is grouped.According SQL As long as an aggregate function bound to a specific list appears Group Or Having Clause also causes Select List Group . The following is a simple example:
Select C1, max (C2) from DBO. T1;
This isGroup Select ,Because the query contains Max Aggregation . Because it is a group Select , Where non-clustered columns are used C1 is invalid, so the query is incorrect.