Testing on SQL Server 2014, CTP2, version 12.0.1524, imagine you have two databases \u2013 one called T1 and one called T2 configured with database compatibility levels as follows:<\/p>\n
\r\nUSE [master];\r\nGO\r\n\r\nALTER DATABASE [T1] SET COMPATIBILITY_LEVEL = 120;\r\nGO\r\n\r\nALTER DATABASE [T2] SET COMPATIBILITY_LEVEL = 110;\r\nGO\r\n <\/pre>\nNow with database T1, we know that using a compatibility level of 120 means we\u2019ll be using the new cardinality estimator (CE) \u2013 assuming we\u2019re in the database session context of a new CE database and don\u2019t have a trace flag disabling the new CE behavior.<\/p>\n
Executing the following query in the context of T1 does indeed mean we use the new CE:<\/p>\n
\r\nUSE [T1];\r\nGO\r\n\r\nSELECT\u00a0\u00a0\u00a0 [member_no],\r\n[provider_no],\r\n[category_no]\r\nFROM [dbo].[charge]\r\nWHERE [charge_no] = 422;\r\n <\/pre>\n<StmtSimple StatementCompId=”1″ StatementEstRows=”1″ StatementId=”1″ StatementOptmLevel=”TRIVIAL” CardinalityEstimationModelVersion=”120″<\/span> StatementSubTreeCost=”0.0032831″ StatementText=”SELECT [member_no],[provider_no],[category_no] FROM [dbo].[charge] WHERE [charge_no]=@1″ StatementType=”SELECT” QueryHash=”0x274BD0D496403EEE” QueryPlanHash=”0x6B5F27FE55FE8A5C” RetrievedFromCache=”true”><\/p>\n
But what if we change the query to be in the context of the T2 database (legacy CE) \u2013 but still access data from the T1 database?<\/p>\n
\r\nUSE [T2];\r\nGO\r\n\r\nSELECT\u00a0\u00a0\u00a0 [member_no],\r\n[provider_no],\r\n[category_no]\r\nFROM [T1].[dbo].[charge]\r\nWHERE [charge_no] = 422;\r\n <\/pre>\nNow we see the query used a legacy CE:<\/p>\n
<StmtSimple StatementCompId=”1″ StatementEstRows=”1″ StatementId=”1″ StatementOptmLevel=”TRIVIAL” CardinalityEstimationModelVersion=”70″ <\/span>StatementSubTreeCost=”0.0032831″ StatementText=”SELECT [member_no],[provider_no],[category_no] FROM [T1].[dbo].[charge] WHERE [charge_no]=@1″ StatementType=”SELECT” QueryHash=”0x274BD0D496403EEE” QueryPlanHash=”0x6B5F27FE55FE8A5C” RetrievedFromCache=”true”><\/p>\n
What if the cross-database query is executed from a new CE session context – but the destination is the legacy CE?<\/p>\n
\r\nUSE [T1];\r\nGO\r\n\r\nSELECT\u00a0\u00a0\u00a0 [member_no],\r\n[provider_no],\r\n[category_no]\r\nFROM [T2].[dbo].[charge]\r\nWHERE [charge_no] = 422;\r\n <\/pre>\nIn this scenario, the query uses the new CE \u2013 based on the database session context \u2013 even though the destination database is set to compatibility level 110.<\/p>\n
What about accessing data from two databases (rather than my previous example of just accessing data from one database)?\u00a0 The following example results in a legacy CE plan:<\/p>\n
\r\nUSE [T2];\r\nGO\r\n\r\nSELECT\u00a0\u00a0\u00a0 [member_no],\r\n[provider_no],\r\n[category_no]\r\nFROM [T1].[dbo].[charge]\r\nWHERE [charge_no] = 422\r\nUNION\r\nSELECT\u00a0\u00a0\u00a0 [member_no],\r\n[provider_no],\r\n[category_no]\r\nFROM [T2].[dbo].[charge]\r\nWHERE [charge_no] = 422;\r\nGO\r\n <\/pre>\nAnd this query results in a new CE plan:<\/p>\n
\r\nUSE [T1];\r\nGO\r\n\r\nSELECT\u00a0\u00a0\u00a0 [member_no],\r\n[provider_no],\r\n[category_no]\r\nFROM [T1].[dbo].[charge]\r\nWHERE [charge_no] = 422\r\nUNION\r\nSELECT\u00a0\u00a0\u00a0 [member_no],\r\n[provider_no],\r\n[category_no]\r\nFROM [T2].[dbo].[charge]\r\nWHERE [charge_no] = 422;\r\nGO\r\n <\/pre>\nSo \u2013 bottom line \u2013 using the new CE doesn\u2019t mean just changing the database compatibility level.\u00a0 Database session context also matters.<\/p>\n","protected":false},"excerpt":{"rendered":"
Testing on SQL Server 2014, CTP2, version 12.0.1524, imagine you have two databases \u2013 one called T1 and one called T2 configured with database compatibility levels as follows: USE [master]; GO ALTER DATABASE [T1] SET COMPATIBILITY_LEVEL = 120; GO ALTER DATABASE [T2] SET COMPATIBILITY_LEVEL = 110; GO Now with database T1, we know that using […]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[],"class_list":["post-1163","post","type-post","status-publish","format-standard","hentry","category-cardinality-estimation"],"yoast_head":"\n
For the New CE, Database Session Context Matters - Joe Sack<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n