Thursday, February 24, 2022

How to copy a good plan of a database and bring it in another database

Copying explain plan from one database to another: :: On Source System :: 1- Run the query on source system and get the sql_id and plan_hash_value. - Get Sql_id from toad and plan_hash_value from the following query: select distinct plan_hash_value from v$sql where sql_id='ffy11b370kvn5'; 2- Load the plan to cursor to SPM (Sql plan management) as baseline. declare ret binary_integer; l_sql_id varchar2(13); l_plan_hash_value number; l_fixed varchar2(3); l_enabled varchar2(3); Begin l_sql_id := '&&sql_id'; l_plan_hash_value := to_number('&&plan_hash_value'); l_fixed := 'Yes'; l_enabled := 'Yes'; ret := dbms_spm.load_plans_from_cursor_cache( sql_id=>l_sql_id, plan_hash_value=>l_plan_hash_value, fixed=>l_fixed, enabled=>l_enabled); end; / 3- Create a staging table on source system. BEGIN DBMS_SPM.CREATE_STGTAB_BASELINE( table_name => 'explaintab', table_owner => 'APPS', tablespace_name => 'SYSAUX'); END; 4- Query the Sql_handle and Plan_name from the following query, It will be used in the next step. select SQL_HANDLE, PLAN_NAME from dba_sql_plan_baselines; 5- Pack the baseline. DECLARE my_plans number; BEGIN my_plans := DBMS_SPM.PACK_STGTAB_BASELINE( table_name => 'explaintab', enabled => 'yes', table_owner => 'APPS', plan_name => 'SQL_PLAN_4uk4uq5q77u35d8257584', sql_handle => 'SQL_4d489ab16c73e865'); END; / 6- export explaintab table from source and export on target database. exp file=/opt/plan.exp tables=apps.explaintab :: On Target System :: 7- import explaintab table: imp file=/opt/plan.exp tables=explaintab fromuser=apps touser=apps 8- Unpack the baseline: DECLARE l_plans_unpacked PLS_INTEGER; BEGIN l_plans_unpacked := DBMS_SPM.unpack_stgtab_baseline( table_name => 'explaintab', table_owner => 'APPS'); DBMS_OUTPUT.put_line('Plans Unpacked: ' || l_plans_unpacked); END; / 9- Enable the optimizer to get the base plan for the query: DECLARE l_plans_altered PLS_INTEGER; BEGIN l_plans_altered := DBMS_SPM.alter_sql_plan_baseline( sql_handle => 'SQL_4d489ab16c73e865', PLAN_NAME => 'SQL_PLAN_4uk4uq5q77u35d8257584', ATTRIBUTE_NAME => 'fixed', attribute_value => 'YES'); DBMS_OUTPUT.put_line('Plans Altered: ' || l_plans_altered); END; / 10- Retest the query. ------------------------------------------------------------------------- https://docs.rackspace.com/blog/transfer-sql-plans-by-using-sql-plan-baseline/ https://dbaclass.com/article/drop-sql-baselines-oracle/

No comments:

Post a Comment