adding line break changes execution plan

  • Hi ,

    I am confused I had seen a rather long query in SQL profiler its all on one line and I ran it to capture the query plan.

    For readability I added some line breaks and executed it again.. and the query plan had totally changed to a far more efficient plan.

    I thought I was mad so I went back and tried it again and again.. and every time the query plan was different when line breaks where introduced.

    I have no idea why this is happening? Any ideas?

    Many thanks

  • Whenever you fire same query second time or more, it will be faster anyways as it will pick the result from cache and gives faster results. This may be the reason for this.

  • Bhaskar.Shetty (6/18/2013)


    Whenever you fire same query second time or more, it will be faster anyways as it will pick the result from cache and gives faster results. This may be the reason for this.

    Hi I am aware of that , sorry I didn't make myself clear, interchanging between the two queries , one with no line breaks and one with line breaks causes a different execution plan to fire

  • Edward-445599 (6/18/2013)


    Bhaskar.Shetty (6/18/2013)


    Whenever you fire same query second time or more, it will be faster anyways as it will pick the result from cache and gives faster results. This may be the reason for this.

    Hi I am aware of that , sorry I didn't make myself clear, interchanging between the two queries , one with no line breaks and one with line breaks causes a different execution plan to fire

    care to share the queries and exec plans?

    ________________________________________________________________
    you can lead a user to data....but you cannot make them think
    and remember....every day is a school day

  • J Livingston SQL (6/18/2013)


    Edward-445599 (6/18/2013)


    Bhaskar.Shetty (6/18/2013)


    Whenever you fire same query second time or more, it will be faster anyways as it will pick the result from cache and gives faster results. This may be the reason for this.

    Hi I am aware of that , sorry I didn't make myself clear, interchanging between the two queries , one with no line breaks and one with line breaks causes a different execution plan to fire

    care to share the queries and exec plans?

    The query's are large and would take a while to rename everything for privacy. But that's beside the point I am more interested what would be the reason for something like this to happen?

    Does anyone know of anything which who cause this to happen

  • Remember, the query optimizer looks for a verbatimly equal query in the plan cache and will use that if it finds one. Your replacing CRs and LFs changes the query, hence it will look for a different plan, which if it doesn't exist will cause it to create a new one. And that leaves parameter sniffing out of the equation...

    --------------------------------------------------------------------------
    A little knowledge is a dangerous thing (Alexander Pope)
    In order for us to help you as efficiently as possible, please read this before posting (courtesy of Jeff Moden)[/url]

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply