To add onto yesterday’s post about which cardinality estimator (CE) your query will use, there’s an additional complexity. This specifically applies to cross database queries.
The database context in which you run the query will determine which CE is used. Let’s look at an example.
First, check on the master and StackOverflow2010 compatibility levels
Querying a 3-part name: StackOverflow2010.dbo.Users
USE [StackOverflow2010]
GO
SELECT TOP 100 *
FROM StackOverflow2010.dbo.Users AS u
WHERE u.Reputation = 1001
This is our baseline query from yesterday. As expected, when we look in the properties of the SELECT operator in the execution plan, we’ll see CE version 70.
Changing database context to master
In this example, I’ll use the master database but it could be any other database.
USE [master]
GO
SELECT TOP 100 *
FROM StackOverflow2010.dbo.Users AS u
WHERE u.Reputation = 1001
So, now that we’re running in master, we used CE version 140. Now in this execution plan, it didn’t make a difference which CE was used. I apologize for not having a good demo of that yet.
I hope this helps when looking at upgrading compatibility levels. The CE version will change based on what database context was used.
Stay tuned!