What is TRIVIAL PLAN in SQL Server?

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:

USE adventureworks
GO
 
SELECT * FROM sys.dm_exec_query_optimizer_info
WHERE counter='trivial plan'

Observe the value of ‘occurrence’ field.

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

SELECT * FROM person.contact

Now, run the DMV again:

SELECT * FROM sys.dm_exec_query_optimizer_info
WHERE counter='trivial plan'

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

SELECT * FROM person.address
GO
SELECT * FROM sys.dm_exec_query_optimizer_info
WHERE counter='trivial plan'
GO

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.

 

 

   

About Amit Bansal

Amit Bansal is always brainstorming around SQL Server. Despite working with SQL since 1997, he is amazed that he keeps learning new things every single day. SQL Server is AB's first love, and his wife does not mind that. He tries to share as much and spreads the SQL goodness. Internals and Performance Tuning excites him, and also gives him sleepless nights at times, simply because he is not a genius, but quite a hard worker and does not give up. It has been a long and exciting journey since 1997, you can read here: http://sqlmaestros.com/amit-bansal/ He is on Twitter: https://www.twitter.com/A_Bansal

View all posts by Amit Bansal →

2 Comments on “What is TRIVIAL PLAN in SQL Server?”

  1. Interesting taster Amil, thank you, I’ll be taking a deeper look at that, how SQL Server works internally is always fascinating to know.

    Jon.

Leave a Reply

Your email address will not be published.