Home Forums SQL Server 2016 SQL Server 2016 - Development and T-SQL Odd behavior -- execution plan different when I execute in the target database vs not in the target database RE: Odd behavior -- execution plan different when I execute in the target database vs not in the target database

  • llevity - Tuesday, May 8, 2018 11:29 AM

    To clarify, I'm running the exact same query, against the exact same database target.  It's just the explain plan differs significantly in cost whether or not I have the correct database selected in the context dropdown in the upper left in SSMS.

    Attached are the two plans.  Good.sqlplan is when I'm in DataMart in the drop down DB context (and the query points to data in the DataMart database), Bad.sqlplan is when I'm in a different database context, but it's still the same query as Good.sqlplan.

    I'm baffled still.  I've tried updating stats on DataMart, no luck.

    But that context change thing means you are changing the database, so you're not running it in one, but different databases. Give me a bit and I'll look at the exec plans.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning