Hi Friends,

SQL Server’s Query Optimizer is one of the most complex components in the DB engine. The job of the optimizer is to produce the cheapest execution plan for a given workload. Mind you, I am not saying ‘best’ or ‘fastest’, I am saying ‘cheapest’; because the optimizer does a cost based optimization; which means that a faster or better plan may exist but the optimizer has already met the required internal cost threshold for the given query, so it outputs that plan. Remember, its cost based optimization; if the optimizer spends its own sweet time in finding out the ‘fastest’ plan – it could have executed the query by that time.

The optimization processconstitutes of several phases. The first phase is known as TRIVIAL PLAN optimization. In some cases, there could be only one viable plan. The optimizer then will not spend time in trying to optimize the plan. The actual cost based optimization process really need not run in such cases as optimizer knows that such queries can only produce a single plan. “SELECT * from ” is a classic example. Let us see:

Run the following code:

Observe the value of ‘occurrence’ field.

Now, let us run a simple “SELECT *” statement:

Now, run the DMV again:

You will see the value being incremented. Run another “SELECT *” statement, followed by the DMV:

You will see the value gets incremented again.

The DMV sys.dm_exec_query_optimizer_info gives some statistics about what the optimizer is doing.