%3CLINGO-SUB%20id%3D%22lingo-sub-922579%22%20slang%3D%22en-US%22%3EWhy%20Database%20ID%20Order%20Matters%20When%20Using%20Query%20Store%20to%20Force%20Plans%20With%20AlwaysOn%20AG%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-922579%22%20slang%3D%22en-US%22%3E%3CP%3ERecently%20I%20was%20engaged%20with%20a%20customer%20who%20ran%20into%20the%20following%20scenario.%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EThe%20customer%20had%20forced%20a%20query%20that%20was%20inside%20a%20stored%20procedure%20using%20the%20Query%20Store.%26nbsp%3B%26nbsp%3B%20The%20plan%20was%20manually%20forced.%3C%2FLI%3E%0A%3CLI%3EThe%20customer%20had%20a%202%20node%20AlwaysOn%20Availability%20Group%20to%20provide%20high%20availability.%3C%2FLI%3E%0A%3CLI%3EThe%20customer%20failed%20over%20from%20one%20node%20to%20another%20as%20part%20of%20a%20maintenance%20window.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EWhen%20the%20customer%20failed%20over%2C%20the%20forced%20plan%20no%20longer%20showed%20that%20it%20was%20being%20executed.%20In%20the%20sys.query_store_query%20DMV%2C%20for%20the%20exact%20same%20query_text_id%2C%20a%20new%20entry%20had%20been%20created%20with%20a%20different%20query_id.%26nbsp%3B%26nbsp%3B%20This%20new%20query%20also%20had%20a%20new%20query_plan%20associated%20with%20it%20and%20that%20plan%20was%20not%20forced%20because%20the%20query%20store%20saw%20it%20as%20a%20new%20query%20with%20a%20new%20plan.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20a%20new%20entry%20to%20be%20inserted%20into%20the%20sys.query_store_query%20DMV%2C%20at%20least%20one%20of%20five%20things%20must%20be%20different%20about%20the%20query%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3Equery_text_id%3C%2FLI%3E%0A%3CLI%3Econtext_settings_id%3C%2FLI%3E%0A%3CLI%3Eobject_id%3C%2FLI%3E%0A%3CLI%3Ebatch_sql_handle%3C%2FLI%3E%0A%3CLI%3Equery_parameterization_type%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAfter%20the%20failover%2C%20the%20one%20thing%20that%20was%20different%20between%20the%20original%20entry%20from%20the%20old%20primary%20and%20the%20new%20entry%20from%20the%20new%20primary%20was%20the%20batch_sql_handle%20column.%26nbsp%3B%26nbsp%3B%20As%20documented%20at%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-catalog-views%2Fsys-query-store-query-transact-sql%3Fview%3Dsql-server-2017%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-catalog-views%2Fsys-query-store-query-transact-sql%3Fview%3Dsql-server-2017%3C%2FA%3E%20the%20batch_sql_handle%20is%20only%20populated%20if%20a%20temporary%20table%20or%20table%20variable%20is%20used%20in%20the%20query.%20In%20this%20case%2C%20there%20was%20a%20temporary%20table%20used%20by%20the%20query%20that%20had%20its%20plan%20forced%2C%20so%20that%20did%20play%20a%20role.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhy%20would%20the%20simple%20act%20of%20failing%20over%20an%20AlwaysOn%20Availability%20Group%20create%20a%20different%20value%20for%20the%20batch_sql_handle%2C%20which%20caused%20a%20new%20query%20to%20get%20created%20therefore%20essentially%20undoing%20the%20forced%20plan%20that%20was%20put%20in%20place%20on%20the%20original%20primary%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20answer%20to%20that%20question%20is%20that%20the%20batch_sql_handle%20encodes%20the%20database%20id%20into%20the%20handle%20value.%26nbsp%3B%26nbsp%3B%20If%20the%20database%20id%20is%20different%20between%20the%20primary%20and%20secondary%20replica%2C%20and%20you%20have%20a%20forced%20plan%20that%20involves%20a%20temporary%20table%20or%20table%20variable%2C%20then%20you%20essentially%20create%20a%20new%20query%20that%20has%20a%20new%20plan.%20The%20original%20plan%20remains%20forced%2C%20but%20it%20will%20never%20be%20used%20until%20you%20fail%20back%20over%20to%20the%20original%20primary.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20following%20screenshots%20demonstrate%20this%20situation.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F138724i004A897DC94AEFDB%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22clipboard_image_0.png%22%20title%3D%22clipboard_image_0.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20655px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F138723i459950B43D437EA9%2Fimage-dimensions%2F655x230%3Fv%3D1.0%22%20width%3D%22655%22%20height%3D%22230%22%20alt%3D%22clipboard_image_1.png%22%20title%3D%22clipboard_image_1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20see%20we%20have%20a%20query%20in%20the%20query%20store%20that%20is%20query_id%202.%26nbsp%3B%26nbsp%3B%20This%20query%20contains%20the%20values%20for%20the%205%20critical%20components%20that%20compose%20a%20unique%20query%20in%20the%20sys.query_store_query%20DMV.%26nbsp%3B%26nbsp%3B%20At%20this%20point%20we%20failed%20over%20and%20reran%20the%20stored%20procedure.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20705px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F138725i473F7E725C1AFC98%2Fimage-dimensions%2F705x141%3Fv%3D1.0%22%20width%3D%22705%22%20height%3D%22141%22%20alt%3D%22clipboard_image_2.png%22%20title%3D%22clipboard_image_2.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENotice%20there%20is%20a%20slight%20difference%20in%20the%20batch_sql_handle%20value.%26nbsp%3B%26nbsp%3B%200x0A%20vs%200x0C.%26nbsp%3B%26nbsp%3B%20That%20is%20the%20hex%20encoding%20for%20database%20ID%2010%20vs%20Database%20ID%2012.%20If%20these%20databases%20had%20the%20same%20database%20ID%20on%20both%20replicas%2C%20then%20the%20batch_sql_handle%20values%20would%20have%20been%20identical%20between%20the%20replicas%20and%20there%20would%20not%20have%20been%20a%20new%20query%20entry%20for%20this%20query.%20This%20is%20very%20important%20when%20you%20force%20plans%20that%20involve%20temporary%20tables%20or%20table%20variables%20as%20it%20will%20not%20use%20the%20original%20query%20when%20you%20fail%20over%20which%20means%20that%20the%20new%20query%20generates%20a%20new%20plan%20and%20the%20forced%20plan%20will%20not%20be%20used%20until%20you%20fail%20back%20to%20the%20original%20primary.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhen%20you%20are%20restoring%20databases%20to%20a%20secondary%20replica%2C%20we%20would%20advise%20you%20to%20restore%20those%20databases%20in%20the%20order%20that%20they%20were%20created%20on%20the%20primary%20replica%20to%20keep%20the%20database%20IDs%20in%20sync%20between%20the%20replicas.%26nbsp%3B%26nbsp%3B%20This%20has%20two%20benefits%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EForced%20plans%20that%20use%20temporary%20tables%20or%20table%20variables%20will%20have%20the%20same%20batch_sql_handle%20value%20and%20therefore%20will%20continue%20to%20be%20treated%20as%20the%20same%20query%20between%20replicas%20keeping%20those%20forced%20plans%20in%20use.%3C%2FLI%3E%0A%3CLI%3EA%20secondary%20benefit%20is%20that%20the%20same%20databases%20that%20have%20parallel%20redo%20enabled%20for%20them%20on%20a%20primary%20replica%20will%20be%20consistent%20with%20the%20secondary%20replica.%26nbsp%3B%26nbsp%3B%20This%20is%20documented%20at%20%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fsql_server_team%2Fsql-server-20162017-availability-group-secondary-replica-redo-model-and-performance%23%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fblogs.msdn.microsoft.com%2Fsql_server_team%2Fsql-server-20162017-availability-group-secondary-replica-redo-model-and-%3C%2FA%3E%2F%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20are%20already%20in%20a%20scenario%20where%20the%20database%20IDs%20mismatch%20between%20the%20primary%20and%20secondary%20replica%20and%20you%20are%20encountering%20the%20behavior%20of%20the%20forced%20plan%20as%20described%20in%20this%20blog%2C%20if%20the%20forced%20plan%20has%20been%20recreated%20with%20the%20new%20query%2C%20you%20may%20have%20to%20consider%20forcing%20both%20of%20the%20equivalent%20plans%20for%20each%20query.%20This%20option%20is%20only%20valid%20if%20the%20equivalent%20already%20forced%20plan%20from%20the%20first%20replica%20has%20been%20reproduced%20on%20the%20second%20replica.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThank%20you%20reading%20the%20article.%26nbsp%3B%20I%20hope%20this%20will%20help%20explain%20why%20forced%20plan%20behavior%20can%20vary%20in%20an%20AlwaysOn%20Availability%20Group%20scenario%20and%20by%20understanding%20database%20ID%20orders%20importance%20you%20can%20proactively%20prevent%20this%20scenario%20from%20occurring.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-922579%22%20slang%3D%22en-US%22%3E%3CP%3EWhen%20combining%20AlwaysOn%20Availability%20Groups%20and%20Query%20Store%2C%20it%20is%20possible%20to%20fail%20over%20and%20have%20a%20plan%20that%20is%20forced%20not%20be%20used%20on%20the%20new%20primary%20if%20you%20have%20queries%20that%20involve%20temporary%20tables%20or%20table%20variables.%20%26nbsp%3B%20This%20article%20describes%20in%20detail%20how%20that%20can%20happen.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-922579%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EGrantCarter%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

Recently I was engaged with a customer who ran into the following scenario.

  • The customer had forced a query that was inside a stored procedure using the Query Store.   The plan was manually forced.
  • The customer had a 2 node AlwaysOn Availability Group to provide high availability.
  • The customer failed over from one node to another as part of a maintenance window.

When the customer failed over, the forced plan no longer showed that it was being executed. In the sys.query_store_query DMV, for the exact same query_text_id, a new entry had been created with a different query_id.   This new query also had a new query_plan associated with it and that plan was not forced because the query store saw it as a new query with a new plan.

 

For a new entry to be inserted into the sys.query_store_query DMV, at least one of five things must be different about the query:

  • query_text_id
  • context_settings_id
  • object_id
  • batch_sql_handle
  • query_parameterization_type

 

After the failover, the one thing that was different between the original entry from the old primary and the new entry from the new primary was the batch_sql_handle column.   As documented at https://docs.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-query-store-query... the batch_sql_handle is only populated if a temporary table or table variable is used in the query. In this case, there was a temporary table used by the query that had its plan forced, so that did play a role.

 

Why would the simple act of failing over an AlwaysOn Availability Group create a different value for the batch_sql_handle, which caused a new query to get created therefore essentially undoing the forced plan that was put in place on the original primary?

 

The answer to that question is that the batch_sql_handle encodes the database id into the handle value.   If the database id is different between the primary and secondary replica, and you have a forced plan that involves a temporary table or table variable, then you essentially create a new query that has a new plan. The original plan remains forced, but it will never be used until you fail back over to the original primary.

 

The following screenshots demonstrate this situation.

 

clipboard_image_0.png

 

 

 

 

clipboard_image_1.png

 

You can see we have a query in the query store that is query_id 2.   This query contains the values for the 5 critical components that compose a unique query in the sys.query_store_query DMV.   At this point we failed over and reran the stored procedure.

 

clipboard_image_2.png

 

Notice there is a slight difference in the batch_sql_handle value.   0x0A vs 0x0C.   That is the hex encoding for database ID 10 vs Database ID 12. If these databases had the same database ID on both replicas, then the batch_sql_handle values would have been identical between the replicas and there would not have been a new query entry for this query. This is very important when you force plans that involve temporary tables or table variables as it will not use the original query when you fail over which means that the new query generates a new plan and the forced plan will not be used until you fail back to the original primary.

 

When you are restoring databases to a secondary replica, we would advise you to restore those databases in the order that they were created on the primary replica to keep the database IDs in sync between the replicas.   This has two benefits:

  1. Forced plans that use temporary tables or table variables will have the same batch_sql_handle value and therefore will continue to be treated as the same query between replicas keeping those forced plans in use.
  2. A secondary benefit is that the same databases that have parallel redo enabled for them on a primary replica will be consistent with the secondary replica.   This is documented at https://blogs.msdn.microsoft.com/sql_server_team/sql-server-20162017-availability-group-secondary-re.../

 

If you are already in a scenario where the database IDs mismatch between the primary and secondary replica and you are encountering the behavior of the forced plan as described in this blog, if the forced plan has been recreated with the new query, you may have to consider forcing both of the equivalent plans for each query. This option is only valid if the equivalent already forced plan from the first replica has been reproduced on the second replica.

 

Thank you reading the article.  I hope this will help explain why forced plan behavior can vary in an AlwaysOn Availability Group scenario and by understanding database ID orders importance you can proactively prevent this scenario from occurring.