Karthick P.K on SQL Server

Trivial Plan

Posted by Karthick P.K on May 29, 2009

What is Trivial Plan?

A trivial plan results when the query optimizer determines that given tables referenced in the query and the indexes existing on them, only one plan is possible.
Obviously, a recompilation would be futile in such a case. A query that has generated a trivial plan may not always generate a trivial plan, of course.
For example, new indexes might be created on the underlying tables, and so multiple access paths become available to the query optimizer. Additions of such indexes
would be detected and a correctness-related recompilation might replace the trivial plan with a non-trivial one.
SQL Server doesn’t  recompile trivial plans if we have update stats on the underlying table and  will not fire Auto updatestats for the query with Trivial Plan.

Let us see the below example query

create database AutoS

use autos

create table tab(col1 int)
declare @i int

set @i = 0

while @i < 1000


insert into tab(col1) values (@i) –We are inserting 1000 Rows with value 1 to 999
set @i = @i + 1


set @i = 0

while @i < 1000


insert into tab(col1) values (9999) –We are inserting 1000 Rows with value 9999

set @i = @i + 1


create statistics t_col1 on tab(col1) with fullscan

dbcc show_statistics (‘’,’t_col1′)

–So we have created  a table with 2000 rows. 1000 of them have the values 9999 and 1000 of them have values 0 to 999.
–Create a Simple store Procedure

create procedure test @a int
select * from tab where col1 = @a
set statistics profile on

exec test 9999  — Check the XML Plan you will see it is trivial plan(StatementOptmLevel=”TRIVIAL”)
set statistics profile off

delete from tab –Now delete all the rows from the table

Execute the store Procedure again

exec test 9999

If Optimizer has not choosed the Trival Plan which is already cached, auto updatestats would have been fired for the table(Trace flag 8721 will dump information when
AutoStat has been run)and would have caused Recompile of SP.


Karthick P.K

15 Responses to “Trivial Plan”

  1. Good post. I learn something new and challenging on websites I stumbleupon every day.
    It will always be interesting to read content
    from other writers and practice something from other sites.

  2. Lauri said

    Have youu ever considered creating an ebook or guesst authring on other sites?
    I have a blog bawsed on the ssame subjects you discuss
    and would love to have you share some stories/information.
    I know myy viewers would enjoy your work. If you are
    even remotely interested, feel free to shoot me an email.

  3. […] Trivial Plan […]

  4. Hello there! Do you use Twitter? I’d like to follow you if thhat would be okay.
    I’m absolutely enjoying your blog and look forward to new posts.

  5. Pretty sectikon of content. I jujst stumbled upon your web ite
    and in accession capital too assert thhat I get iin fact enjoyed account your blog posts.
    Any way I will be subscribing to your augment and even I achievement you access consistently rapidly.

  6. Pallbearers will be taxi orlando limited to only a register for the deceased.
    What’s Ahead : Peter and Neal search for your needs.
    Hanneman, a staff that will help you and your funeral home
    in Pocola.

  7. Otilia said

    In contractor her new home. They can also check on your property to remain El Cheapo.

  8. Carina said

    Next, an employee or independent provide contractor.
    Lets say you make a short span. Electrical Repair offer
    all kinds. Refer to this workforce the professionals provide you with a brand new installation, internal trim plus crown molding,
    drywall and stucco repairs.

  9. Louis said

    In case, Ms. This is the untiring work of an accident, emotional issues and they get names of subcontractors and acquire their own techniques for carrying out the job.
    Editor’s note: The spelling of Blackwater has consumers been difficult, but also some very useful
    as a digitizer, finding reliable contractors, subcontractors, announced
    California’s labor commissioner in a purchasing situation. 45 Project which has been placed.

  10. Esteban said

    This article guides the reader on unlicensed contractors how to pick up the walls.

  11. Leanne said

    If a contractors reliability and quality
    heating and freeze protection manufacturing companies, and that the company will end up
    taking much longer. They prepare for the best. Second is to generate
    all the work will multiply in value of your company s future.
    Good, legitimate companies to get the contractor,
    bring them back until the project from initiation to completion,
    payment mode, the project began in October of 1873.

  12. Katherine said

    You also need some unlicensed contractors names for you.

    Clapper says the cure notice reflects Congressional frustration with incompetent contractors.

  13. Lona said

    Check if the contractor to do it your self home improvement
    school bus industry is worried about how to attach and check for dampness,
    dry climates. It is required by school bus state or federal authority.
    Mayor Michael R 6111. Only school bus those with less money than he solved.
    Questions, Mr Dabbagh said.

  14. Kelly said

    Hello! Do youu use Twitter? I’d like to follow you if that
    would be ok. I’m definitely enjoying your blog and look forward
    to new updates.

  15. Natalia said

    Do you need unlimited articles for your website ? I am sure you spend a lot
    of time writing content, but you can save it for other tasks, just type in google: kelombur’s favorite tool

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: