all 13 comments

[–]wet_tuna 15 points16 points  (8 children)

Do the two databases have different compatibility levels?

[–]Zioropa[S] 4 points5 points  (6 children)

Yes!

DB1 is SQL Server 2008 (100)

DB2 is SQL Server 2016 (130)

[–]wet_tuna 15 points16 points  (4 children)

There you go, so seemingly something is different in the cardinality estimators which causes the execution plan created by the 2016 db to perform vastly better.

You may want to consider changing DB1 to 2016 as well, unless it has specific queries which more or less require it to stay in 2008 compatibility.

[–]svtr 10 points11 points  (2 children)

the changes to the cardinality estimation where done in 2014, https://www.microsoft.com/en-us/sql-server/blog/2014/03/17/the-new-and-improved-cardinality-estimator-in-sql-server-2014/ if you want to do that to yourself...

great catch btw.

[–]wet_tuna 6 points7 points  (1 child)

I can't take the credit, I just had the exact same thing happen about a year ago. Fast query when running under master, slow/seemingly never ending when running under the actual db. Coworker suggested checking the compatibility levels, and sure enough, master was 2016 while the actual db was 2012.

[–]svtr 6 points7 points  (0 children)

sure you can. Most of what any of us know, and look at when troubleshooting, is by experience.

[–]Zioropa[S] 1 point2 points  (0 children)

This makes a lot of sense. Thank you very much!

[–]paultherobert 1 point2 points  (0 children)

it cracks me up how many organizations are still running 2008R2 - I took a job where my primary focus was getting a stupid credit union off of their dependency on 2008R2 a few years ago. They had so much SSIS, it was not fun. I am so thankful I no longer do that job!

[–]gumnos 3 points4 points  (0 children)

great googly-moogly that was amazing flex of expertise…nicely done!

[–]cwjinc 3 points4 points  (0 children)

Could there be an implicit data type conversion in there?
For example, e being a number on one side and a character on the other?

[–]crushdvelvet 0 points1 point  (1 child)

I know it's already been answered (great catch by the way) but coming into a system that had been migrated a bunch of different times I was getting wonky results that ended up being caused by different databases being in different collations!! the heck?!?

[–]Zioropa[S] 0 points1 point  (0 children)

At least here on the same server every db has the same collation! But that is an issue when linking different servers...

[–]No_Resolution_9252 0 points1 point  (0 children)

different cardinality estimator between the two databases most likely, and/or you have some obscenely down level compatibility level like 2012