Академический Документы
Профессиональный Документы
Культура Документы
• What’s new:
– Create Local domain indexes for range-
partitioned tables
– Collect statistics, cost and selectivity for local
domain indexes
• Benefits: Extends local index capabilities to
application-specific domain indexes which enables:
– Concurrent operations on two or more partitions
– Concurrent rebuild of two or more partitions
– Efficient searching on partition level indexes
Examples
In both examples, the SPATIAL_INDEX INDEXTYPEis used. This INDEXTYPE
has the following properties:
• Supports the SDO_RELATE operator
• Indexes objects of type SDO_GEOMETRY
• Is implemented using the SPATIALTIMPLTYPE
CREATE INDEXTYPE
The CREATE INDEXTYPE command defines a new INDEXTYPE, SPATIAL_INDEX, that
supports the SDO_RELATE operator and is capable of supporting local domain indexes on
range-partitioned tables.
ALTER INDEXTYPE
The ALTER INDEXTYPE command alters an existing INDEXTYPE, SPATIAL_INDEX, to
specify that it now also has support for local domain indexes on range partitioned tables.
ODCIStats Changes
Oracle9i extensible optimizer extensions include support for local domain indexes. Modify
ODCIStats routines to collect statistics at the index partition level as well as the index
level.
This support has been extended to statistics for column types too. So, user-defined statistics
for columns should be modified to collect statistics at the table partition level as well as the
table level.
Similarly, the ODCIIndexCost function should be modified to compute the cost of a
subset of index partitions as well as the cost for the entire index.
The ODCIStatsSelectivityshould be modified to calculate the selectivity of
predicates relative to interesting partitions as well as to the entire table.
Concepts
• Transient types: type-descriptions, that is type meta-data, that are not stored persistently in
the database.
• Persistent types: SQL types created using the CREATE TYPE SQL statement. These type
descriptions are stored persistently in the database.
• Self-descriptive data: Data encapsulating type information along with the actual contents.
• Self-descriptive Multi-Set: encapsulation of a set of data instances of the same type, along
with their type description. They should all have the same type description.
Overview
In Oracle 8.0, the only way for the user to create type descriptions was through the CREATE
TYPE statement in SQL. Access to these type descriptions (OCIType) was provided through
OCIDescribeAny().
The Type Interfaces provide C and SQL APIs to enable dynamic creation and access of type
descriptions. Additionally, these new interfaces allow dynamic creation of transient type
descriptions, that are not stored persistently in the DBMS.
The SQL datatypes that will correspond to these data types are implemented using the Opaque
Type Mechanism. This will enable creation of Database table columns and SQL queries on such
data.
The type interfaces are heterogeneous in nature, that is, a type can hold values of any other type in
the DBMS and it can be converted back to a value of the appropriate type. Such heterogeneity can
be modeled with an opaque type mechanism.
Opaque types can be used from all kinds of database contexts: SQL, PLSQL, OCI, JAVA, triggers,
and others.
Extensibility New Features 2-3
Features and Benefits
Using DBMS_TYPES
The package DBMS_TYPES contains constants that used to define the type of data being accessed
when using the SYS.AnyType, SYS.AnyData, or SYS.AnyDatSet APIs. It also contains
exceptions that are raised when the data type enumerator is invalid.
Displaying DBMS_TYPES Type Codes
To see a complete list the codes in DBMS_TYPES in SQL*Plus, enter the command:
DESC SYS.DBMS_TYPES
Package DBMS_TYPES Type Codes
TYPECODE_DATE PLS_INTEGER := 12;
TYPECODE_NUMBER PLS_INTEGER := 2;
TYPECODE_RAW PLS_INTEGER := 95;
TYPECODE_CHAR PLS_INTEGER := 96;
TYPECODE_VARCHAR2 PLS_INTEGER := 9;
TYPECODE_VARCHAR PLS_INTEGER := 1;
TYPECODE_MLSLABEL PLS_INTEGER := 105;
TYPECODE_BLOB PLS_INTEGER := 113;
TYPECODE_BFILE PLS_INTEGER := 114;
TYPECODE_CLOB PLS_INTEGER := 112;
TYPECODE_CFILE PLS_INTEGER := 115;
TYPECODE_TIMESTAMP PLS_INTEGER := 187;
PL/SQL Example
SQL> CREATE PROCEDURE build_anytype_wh (
2 p_any_type IN OUT sys.anytype )
3 AS
4 /* AnyType properties
*/
5 BEGIN
6 sys.AnyType.BeginCreate
7 (DBMS_TYPES.TYPECODE_OBJECT, p_any_type);
8 p_any_type.AddAttr (
9 aname => 'warehouse_id',
10 typecode => DBMS_TYPES.TYPECODE_NUMBER,
11 prec => 3,
12 scale => 0,
13 len => NULL,
14 csid => NULL,
15 csfrm => NULL );
Procedure created.
Procedure created.
SQL> DECLARE
2 v_any_type sys.AnyType; /* AnyType variable */
3 BEGIN
4 /* Build the ANYTYPE variable. */
5 build_anytype_wh(v_any_type);
6 /* Now, get the information about the type's attributes.
*/
7 dbms_output.put_line('Properties for the 1st attribute');
8 print_anytype_attr(v_any_type, 1);
9 dbms_output.put_line('.');
10 dbms_output.put_line('Properties for the 2nd attribute');
11 print_anytype_attr(v_any_type, 2);
12 dbms_output.put_line('.');
13 dbms_output.put_line('Properties for the 3rd attribute');
14 print_anytype_attr(v_any_type, 3);
15 END;
16 /
SQL>
Table created.
Type created.
1 row created.
Extensibility New Features 2-17
SQL and PL/SQL Example (continued)
SQL> /* Insert a user-defined object after explicit conversion to
an AnyData */
SQL> INSERT INTO any_data_table
2 VALUES (SYS.AnyData.ConvertObject (
3 warehouse_typ(1, 'Southlake, Texas', 1400) ));
1 row created.
SQL> COMMIT;
Commit complete.
SQL> DECLARE
2 v_any_type SYS.AnyType;
3 v_any_data SYS.AnyData;
4 v_warehouse warehouse_typ; /* Put AnyData here */
5 /* AnyType properties
*/
6 v_prec PLS_INTEGER; /* Numeric precision
*/
7 v_scale PLS_INTEGER; /* Numeric precision
*/
8 v_len PLS_INTEGER; /* Character length
*/
9 v_csid PLS_INTEGER; /* Character set ID
*/
10 v_csfrm PLS_INTEGER; /* Character set form
*/
11 v_attr_elt_type sys.AnyType; /* Object attribute
*/
12 v_schema_name VARCHAR2(40); /* Object owner
*/
13 v_type_name VARCHAR2(40); /* Object name
*/
14 v_version VARCHAR2(40); /* Version */
15 v_count PLS_INTEGER; /* Number of attributes
*/
16 v_aname VARCHAR2(40); /* Attribute name
*/
17 v_type_code PLS_INTEGER; /* Object type code
*/
18 v_return PLS_INTEGER; /* Return code = status
*/
19 v_n NUMBER;
20
Extensibility New Features 2-18
21 CURSOR any_data_c IS
SQL and PL/SQL Example (continued)
27 LOOP
28 FETCH any_data_c INTO v_any_data;
29 EXIT WHEN any_data_c%NOTFOUND;
30
31 /* Get the type of data stored in v_any_data */
32 v_type_code := v_any_data.GetType(v_any_type);
33
34 /* Test the type. */
35 IF v_type_code = DBMS_TYPES.TYPECODE_NUMBER THEN
36 /* It is a number, so print it */
37 v_return := v_any_data.GetNumber(v_n);
38 dbms_output.put_line(
39 'The value for the number datatype is: ' || v_n);
40 ELSIF (v_type_code = DBMS_TYPES.TYPECODE_OBJECT) THEN
41 /* It is an object, so get the schema and type */
42 v_type_code := v_any_type.GetInfo (
43 v_prec,
44 v_scale,
45 v_len,
46 v_csid,
47 v_csfrm,
48 v_schema_name,
49 v_type_name,
50 v_version,
51 v_count );
52 /* Verify that the schema and type is correct */
53 IF v_schema_name = USER
54 AND (v_type_name = 'WAREHOUSE_TYP') THEN
55 /* It is, so print its attribute information */
56 dbms_output.put_line('1st attribute');
57 print_anytype_attr(v_any_type, 1);
58 dbms_output.put_line('.');
59 dbms_output.put_line('2nd attribute');
60 print_anytype_attr(v_any_type, 2);
61 dbms_output.put_line('.');
62 dbms_output.put_line('3rd attribute');
63 print_anytype_attr(v_any_type, 3);
SQL>
PL/SQL Example
SQL> DECLARE
2 v_any_type SYS.AnyType;
3 v_any_data SYS.AnyData;
4 v_any_data_set SYS.AnyDataSet;
5 v_warehouse warehouse_typ; /* Put AnyData here */
6 /* AnyType properties */
7 v_return PLS_INTEGER; /* Return code = status */
8
9 BEGIN
10
11 /* Create an AnyType object from a user-defined type */
12 /* Used to create the AnyDataSet */
13 v_any_type := sys.AnyType.GetPersistent
14 (user, 'WAREHOUSE_TYP');
15
16 /* Start building the AnyDataSet */
SQL>
Extensibility New Features 2-26
Summary
Aggregation Interface
To create a UDAG function, the developer creates an object type that implements the four
primitives described earlier. The object types includes a method for each of the four primitives.
This set of functions is collectively known as ODCIAggregate Interface, where ODCI is the
abbreviation for Oracle Data Cartridge Interface.
All of these primitives return a numeric value to indicate whether the function was successful.
Use ODCIConst.Success on success or ODCIConst.Error on error.
The aggregation interface is implemented using the method names defined below.
ODCIAggregateInitialize
This routine is invoked by Oracle to initialize the computation of the UDAG. The initialized
aggregation context is passed back to Oracle as an object type instance.
ODCIAggregateIterate
This routine is repeatedly invoked by Oracle as each row is processed. On each invocation, new
values, as well as the current aggregation context, are passed as input. The routine processes the
new values and returns the updated aggregation context back to Oracle.
ODCIAggregateMerge
This routine is invoked by Oracle to combine two aggregation contexts. This routine takes the
two contexts as inputs, combines them and returns the resulting aggregate context.
ODCIAggregateterminate
This routine is invoked by oracle as the final step of aggregation. The routine takes the
aggregation context as input, performs any additional computation that may be required, and
returns the result aggregate value.
CREATE
CREATE TYPE
TYPE SecondMaxImpl
SecondMaxImpl AS
AS OBJECT
OBJECT ((
max
max NUMBER,
NUMBER, --highest
--highest value
value
secmax
secmax NUMBER,
NUMBER, --second
--second highest
highest value
value
STATIC
STATIC FUNCTION
FUNCTION ODCIAggregateInitialize
ODCIAggregateInitialize
(self_ctx
(self_ctx OUT
OUT SecondMaxImpl)
SecondMaxImpl)
RETURN
RETURN NUMBER,
NUMBER,
.. .. ..
MEMBER
MEMBER FUNCTION
FUNCTION ODCIAggregateIterate.
ODCIAggregateIterate. .. ..
MEMBER
MEMBER FUNCTION
FUNCTION ODCIAggregateMerge.
ODCIAggregateMerge. .. ..
MEMBER
MEMBER FUNCTION
FUNCTION ODCIAggregateTerminate.
ODCIAggregateTerminate. .. ..
);
);
//
Example
All of the code for creating the object type specification is listed below:
CREATE
CREATE OR
OR REPLACE
REPLACE TYPE
TYPE BODY
BODY SecondMaxImpl
SecondMaxImpl IS
IS
STATIC
STATIC FUNCTION
FUNCTION ODCIAggregateinitialize
ODCIAggregateinitialize ((
...
...
MEMBER
MEMBER FUNCTION
FUNCTION ODCIAggregateiterate
ODCIAggregateiterate ((
...
...
MEMBER
MEMBER FUNCTION
FUNCTION ODCIAggregateterminate
ODCIAggregateterminate ((
...
...
MEMBER
MEMBER FUNCTION
FUNCTION ODCIAggregatemerge
ODCIAggregatemerge ((
...
...
END;
END;
Example
All of the code for creating the object type body is listed below:
END;
/
• Guidelines:
• Match the RETURN data type with the data
type in the ODCIAggregateTerminate
method
• The USING clause maps this function to its
ODCIAggregate interface
Guidelines
Because the second parameter in ODCIAggregateTerminateis the value returned by the
user-defined group function, its data type must match the data type returned by this function.
This is the RESULT parameter in the example, and it has a datatype of NUMBER.
The USING clause provides the name of the PL/SQL package that implements the
ODCIAggregate interface. Is is SecondMaxImpl in the example.
SQL>
SQL> SELECT
SELECT department_id,
department_id,
22 SecondMax(salary)
SecondMax(salary) AS
AS sal2
sal2
33 FROM
FROM employees
employees
44 GROUP
GROUP BY
BY department_id
department_id
55 HAVING
HAVING SecondMax(salary)
SecondMax(salary) >> 2000;
2000;
DEPTNO
DEPTNO SAL2
SAL2
----------
---------- ----------
----------
10
10 2450
2450
20
20 2975
2975
Example Implementation
The example on the following slides converts the StockTable into a different format. StockTable
includes the stock's ticker symbol, opening price, and closing price. For example, a row might
contain the values: 'ORCL', 41, 42. However, there is a requirement to process the stocks using a
format of ticker symbol, price type, and closing price. The price type is:
• O for an opening price
• C for a closing price
The row 'ORCL', 41, 42 would be converted into two rows:
ORCL, O, 41
ORCL, C, 42
The objects created below are used in the example.
TickerType Object
The TickerType object is the format of the rows returned from ODCITableFetch method. It returns
two rows for each input row.
CREATE TYPE TickerType AS OBJECT (
Ticker VARCHAR2(4),
PriceType VARCHAR2(1),
Price NUMBER );
/
Example
The code for creating the object type specification is presented in the slide, and the code for
creating the object type body definition is presented below.
These methods all return ODCIConst.Success on success or ODCIConst.Error
otherwise.
CREATE TYPE BODY StockPivotImpl IS