• SQLRNNR (3/26/2015)


    dwain.c (3/26/2015)


    ChrisM@Work (3/26/2015)


    Where’s a plan showing huge Estimated Operator Costs when you need one? Have a look at the estimated plan for this conditional query i.e. the IF as well as the SELECT. You probably don’t want to execute the query in prod and you don’t need to – you only see the skew in the estimated plan. The explanation is surprisingly simple 😉

    IF EXISTS (SELECT 1 FROM (VALUES(CAST(1 AS BIT))) d (n))

    SELECT 'Anything' FROM sys.columns a

    FULL OUTER JOIN sys.columns b

    ON a.[precision] = b.[precision]

    Quite a bizarre estimated plan to be sure. Facts:

    - The issue goes away if you remove the IF

    - The issue doesn't show in the actual execution plan

    - The query itself runs reasonably quickly.

    Edit: This query shows the same thing:

    IF EXISTS (SELECT 1 FROM (VALUES(CAST(1 AS BIT))) d (n))

    SELECT * FROM sys.all_columns;

    Which means that it has something to do with the 100% cost calculated for the Constant Scan generated for the IF. The remainder of the query must be getting its costs rated against that.

    Add this to the list of SQL Developer interviewee questions...

    Interesting indeed.

    I generally "manufacture" a plan if I really want to demonstrate something out of whack and quite get it to show with data or fascinating queries. It is all just xml afterall.

    With minimal skew, but skew just the same:

    IF EXISTS (SELECT n FROM (VALUES(CAST(1 AS BIT))) d (n))

    SELECT 1 FROM (VALUES(0),(0)) d (n) CROSS JOIN (VALUES(0),(0)) e (n);


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St