159 DBMS_SPD
The DBMS_SPD
package provides subprograms for managing SQL plan directives (SPD).
This chapter contains the following topics:
See Also:
-
Oracle Database SQL Tuning Guide regarding SQL plan directives
159.1 DBMS_SPD Overview
This package provides subprograms for managing SQL plan directives (SPD).
SPD are objects generated automatically by Oracle. For example, if Oracle detects that the single table cardinality estimated made by the optimizer is different from the actual number of rows returned when accessing the table, it will automatically create a directive to perform dynamic statistics for the table. When any SQL statement referencing the table is compiled, the optimizer will perform dynamic statistics for the table to get a more accurate estimate.
159.2 DBMS_SPD Security Model
DBMS_SPD
is an invoker-rights package. The invoker requires ADMINISTER SQL MANAGEMENT OBJECT
privilege for executing most of the subprograms in this package. Also, the subprograms commit the current transaction (if any), perform the operation, and then commit it again.
159.3 DBMS_SPD Views
The DBA view DBA_SQL_PLAN_DIRECTIVES
shows all the directives created in the system and the view DBA_SQL_PLAN_DIR_OBJECTS
displays the objects that are included in the directives.
159.4 Summary of DBMS_SPD Subprograms
This table lists and briefly describes the DBMS_SPD
package subprograms.
Table 159-1 DBMS_SPD Package Subprograms
Subprogram | Description |
---|---|
Changes different attributes of a SQL plan directive |
|
Creates a staging table into which to pack (export) SQL plan directives. |
|
Drops a SQL plan directive |
|
Allows for manual flushing of the SQL plan directives that are automatically recorded in SGA memory while executing SQL statements. |
|
Gets the values for preferences for SQL plan directives |
|
Packs (exports) SQL plan directives into a staging table. |
|
Allows the setting of different preferences for SQL plan directives |
|
Unpacks (imports) SQL plan directives from a staging table. |
159.4.1 ALTER_SQL_PLAN_DIRECTIVE Procedure
This procedure changes different attributes of a SQL plan directive.
Syntax
DBMS_SPD.ALTER_SQL_PLAN_DIRECTIVE ( directive_id IN NUMBER, attribute_name IN VARCHAR2, attribute_value IN VARCHAR2);
Parameters
Table 159-2 ALTER_SQL_PLAN_DIRECTIVE Procedure Parameters
Parameter | Description |
---|---|
|
SQL plan directive ID |
|
|
|
Possible values:
|
Exceptions
-
ORA-38171 INSUFFICIENT_PRIVILEGE
: The user does not have proper privilege to perform the operation. -
ORA-28104 INVALID_INPUT
: The input value is not valid. -
ORA-13158 OBJECT_DOES_NOT_EXIST
: The specified object does not exist.
Usage Notes
The ADMINISTER SQL MANAGEMENT OBJECT
privilege is required to execute this procedure.
Examples
BEGIN DBMS_SPD.ALTER_SQL_PLAN_DIRECTIVE (12345, 'STATE', 'PERMANENT'); END;
159.4.2 CREATE_STGTAB_DIRECTIVE Procedure
This procedure creates a staging table into which to pack (export) SQL plan directives.
Syntax
DBMS_SPD.CREATE_STGTAB_DIRECTIVE ( table_name IN VARCHAR2, table_owner IN VARCHAR2 := USER, tablespace_name IN VARCHAR2 := NULL);
Parameters
Table 159-3 CREATE_STGTAB_DIRECTIVE Procedure Parameters
Parameter | Description |
---|---|
|
Name of staging table |
|
Name of schema owner of staging table. Default is current schema. |
|
Name of tablespace. Default |
Exceptions
-
ORA-38171 INSUFFICIENT_PRIVILEGE
: The user does not have proper privilege to perform the operation. -
ORA-28104 INVALID_INPUT
: The input value is not valid. -
ORA-44001 INVALID_SCHEMA
: The input schema does not exist. -
ORA-13159 TABLE_ALREADY_EXISTS
: The specified table already exists. -
ORA-29304 TABLESPACE_MISSING
: The specified tablespace does not exist.
Usage Notes
The ADMINISTER SQL MANAGEMENT OBJECT
privilege is required to execute this procedure.
159.4.3 DROP_SQL_PLAN_DIRECTIVE Procedure
This procedure drops a SQL plan directive.
Syntax
DBMS_SPD.DROP_SQL_PLAN_DIRECTIVE ( directive_id IN NUMBER);
Parameters
Table 159-4 DROP_SQL_PLAN_DIRECTIVE Procedure Parameters
Parameter | Description |
---|---|
|
SQL plan directive ID |
Exceptions
-
ORA-38171 INSUFFICIENT_PRIVILEGE
: The user does not have proper privilege to perform the operation. -
ORA-28104 INVALID_INPUT
: The input value is not valid. -
ORA-13158 OBJECT_DOES_NOT_EXIST
: The specified object does not exist.
Usage Notes
The ADMINISTER SQL MANAGEMENT OBJECT
privilege is required to execute this procedure.
Examples
BEGIN DBMS_SPD.DROP_SQL_PLAN_DIRECTIVE (12345); END;
159.4.4 FLUSH_SQL_PLAN_DIRECTIVE Procedure
This procedure allows for manual flushing of the SQL plan directives that are automatically recorded in SGA memory while executing SQL statements.
The information recorded in the SGA is periodically flushed by an Oracle background process. This procedure provides a way to flush the information manually.
Syntax
DBMS_SPD.FLUSH_SQL_PLAN_DIRECTIVE;
Exceptions
ORA-38171 INSUFFICIENT_PRIVILEGE
: The user does not have proper privilege to perform the operation.
Usage Notes
The ADMINISTER SQL MANAGEMENT OBJECT
privilege is required to execute this procedure.
Examples
BEGIN DBMS_SPD.FLUSH_SQL_PLAN_DIRECTIVE; END;
159.4.5 GET_PREFS Function
This function returns the value for the specified preferences for SQL plan directives.
Syntax
DBMS_SPD.GET_PREFS ( pname IN VARCHAR2) RETURN VARCHAR2;
Parameters
Table 159-5 GET_PREFS Function Parameters
Parameter | Description |
---|---|
|
Preference name. The procedure supports the preference |
Return Values
Preference value
Exceptions
-
ORA-38171 INSUFFICIENT_PRIVILEGE
: The user does not have proper privilege to perform the operation. -
ORA-28104 INVALID_INPUT
: The input value is not valid.
Usage Notes
-
The
ADMINISTER SQL MANAGEMENT OBJECT
privilege is required to execute this procedure. -
SPD_RETENTION_WEEKS
- SQL plan directives are purged if not used for more than the value set for this preference.
Examples
SELECT DBMS_SPD.GET_PREFS('SPD_RETENTION_WEEKS') FROM DUAL;
159.4.6 PACK_STGTAB_DIRECTIVE Function
This function packs (exports) SQL plan directives into a staging table.
Syntax
DBMS_SPD.PACK_STGTAB_DIRECTIVE ( table_name IN VARCHAR2, table_owner IN VARCHAR2 := USER directive_id IN NUMBER := NULL, obj_list IN OBJECTTAB := NULL) RETURN NUMBER
Parameters
Table 159-6 PACK_STGTAB_DIRECTIVE Function Parameters
Parameter | Description |
---|---|
|
Name of staging table |
|
Name of schema owner of staging table. Default is current schema. |
|
SQL plan directive ID. Default |
|
Used to filter the directives to be packed based on the objects used in directives. If |
Return Values
Number of SQL plan directives packed.
Exceptions
-
ORA-38171 INSUFFICIENT_PRIVILEGE
: The user does not have proper privilege to perform the operation. -
ORA-28104 INVALID_INPUT
: The input value is not valid. -
ORA-44001 INVALID_SCHEMA
: The input schema does not exist. -
ORA-29304 INVALID_STGTAB
: The specified staging table is invalid or does not exist. -
ORA-13158 OBJECT_DOES_NOT_EXIST
: The specified object does not exist.
Usage Notes
The ADMINISTER SQL MANAGEMENT OBJECT
privilege is required to execute this procedure.
Examples
-- Pack all directives in the system SELECT DBMS_SPD.PACK_STGTAB_DIRECTIVE('mydirtab') FROM DUAL; SET SERVEROUTPUT ON; -- Pack directives relevant to objects in SH schema DECLARE my_list DBMS_SPD.OBJECTTAB := DBMS_SPD.ObjectTab(); dir_cnt NUMBER; BEGIN my_list.extend(1); my_list(1).owner := 'SH'; -- schema name my_list(1).object_name := NULL; -- all tables in SH my_list(1).object_type := 'TABLE'; -- type of object dir_cnt := DBMS_SPD.PACK_STGTAB_DIRECTIVE('mydirtab', obj_list => my_list); DBMS_OUTPUT.PUT_LINE('dir_cnt = ' || dir_cnt); END; -- Pack directives relevant to tables SALES and CUSTOMERS in SH schema DECLARE my_list DBMS_SPD.OBJECTTAB := DBMS_SPD.ObjectTab(); dir_cnt NUMBER; BEGIN my_list.extend(2); -- SALES table my_list(1).owner := 'SH'; my_list(1).object_name := 'SALES'; my_list(1).object_type := 'TABLE'; -- CUSTOMERS table my_list(2).owner := 'SH'; my_list(2).object_name := 'CUSTOMERS'; my_list(2).object_type := 'TABLE'; dir_cnt := DBMS_SPD.PACK_STGTAB_DIRECTIVE('mydirtab', obj_list => my_list); DBMS_OUTPUT.PUT_LINE('dir_cnt = ' || dir_cnt); END;
159.4.7 SET_PREFS Procedure
This procedure allows the setting of different preferences for SQL plan directives.
Syntax
DBMS_SPD.SET_PREFS ( pname IN VARCHAR2, pvalue IN VARCHAR2);
Parameters
Table 159-7 SET_PREFS Procedure Parameters
Parameter | Description |
---|---|
|
Preference name. The procedure supports the preference |
|
Preference value.
|
Exceptions
-
ORA-38171 INSUFFICIENT_PRIVILEGE
: The user does not have proper privilege to perform the operation. -
ORA-28104 INVALID_INPUT
: The input value is not valid.
Usage Notes
-
The
ADMINISTER SQL MANAGEMENT OBJECT
privilege is required to execute this procedure. -
SPD_RETENTION_WEEKS
- SQL plan directives are purged if not used for more than the value set for this preference.
Examples
BEGIN DBMS_SPD.SET_PREFS('SPD_RETENTION_WEEKS', '4'); END;
159.4.8 UNPACK_STGTAB_DIRECTIVE Function
This procedure unpacks (imports) SQL plan directives from a staging table.
Syntax
DBMS_SPD.UNPACK_STGTAB_DIRECTIVE ( table_name IN VARCHAR2, table_owner IN VARCHAR2 := USER, directive_id IN NUMBER := NULL, obj_list IN OBJECTTAB := NULL) RETURN NUMBER
Parameters
Table 159-8 UNPACK_STGTAB_DIRECTIVE Function Parameters
Parameter | Description |
---|---|
|
Name of staging table |
|
Name of schema owner of staging table. Default is current schema. |
|
SQL plan directive ID. Default |
|
Used to filter the directives to be unpacked based on the objects used in directives. If |
Return Values
Number of SQL plan directives unpacked.
Exceptions
-
ORA-38171 INSUFFICIENT_PRIVILEGE
: The user does not have proper privilege to perform the operation. -
ORA-28104 INVALID_INPUT
: The input value is not valid. -
ORA-44001 INVALID_SCHEMA
: The input schema does not exist. -
ORA-29304 INVALID_STGTAB
: The specified staging table is invalid or does not exist. -
ORA-13158 OBJECT_DOES_NOT_EXIST
: The specified object does not exist.
Usage Notes
The ADMINISTER SQL MANAGEMENT OBJECT
privilege is required to execute this procedure.
Examples
-- Unack all directives in the staging table SELECT DBMS_SPD.UNPACK_STGTAB_DIRECTIVE('mydirtab') FROM DUAL; SET SERVEROUTPUT ON; -- Unpack directives relevant to objects in SH schema DECLARE my_list DBMS_SPD.OBJECTTAB := DBMS_SPD.ObjectTab(); dir_cnt number; BEGIN my_list.extend(1); my_list(1).owner := 'SH'; -- schema name my_list(1).object_name := null; -- all tables in SH my_list(1).object_type := 'TABLE'; -- type of object dir_cnt := DBMS_SPD.UNPACK_STGTAB_DIRECTIVE('mydirtab', obj_list => my_list); DBMS_OUTPUT.PUT_LINE('dir_cnt = ' || dir_cnt); END; -- Unpack directives relevant to tables SALES and CUSTOMERS in SH schema DECLARE my_list DBMS_SPD.OBJECTTAB := DBMS_SPD.ObjectTab(); dir_cnt NUMBER; begin my_list.extend(2); -- SALES table my_list(1).owner := 'SH'; my_list(1).object_name := 'SALES'; my_list(1).object_type := 'TABLE'; -- CUSTOMERS table my_list(2).owner := 'SH'; my_list(2).object_name := 'CUSTOMERS'; my_list(2).object_type := 'TABLE'; dir_cnt := DBMS_SPD.UNPACK_STGTAB_DIRECTIVE('mydirtab', obj_list => my_list); DBMS_OUTPUT.PUT_LINE('dir_cnt = ' || dir_cnt); END;