Additional Oracle Performance Extensions

In addition to update batching, Oracle JDBC drivers support the following extensions

that improve performance by reducing round-trips to the database:

■Prefetching rows

This reduces round-trips to the database by fetching multiple rows of data each

time data is fetched. The extra data is stored in client-side buffers for later access

by the client. The number of rows to prefetch can be set as desired.

■Specifying column types

This avoids an inefficiency in the standard JDBC protocol for performing and

returning the results of queries.

■Suppressing database metadata TABLE_REMARKS columns

This avoids an expensive outer join operation.

Oracle provides several extensions to connection properties objects to support these

performance extensions. These extensions enable you to set the remarksReporting

flag and default values for row prefetching and update batching.

Oracle Row-Prefetching Limitations

There is no maximum prefetch setting. The default value is 10. Larger or smaller
values may be appropriate depending on the number of rows and columns expected
from the query. You can set the default connection row-prefetch value using a
Properties object.
When a statement object is created, it receives the default row-prefetch setting from the
associated connection. Subsequent changes to the default connection row-prefetch
setting will have no effect on the statement row-prefetch setting.
If a column of a result set is of data type LONG, LONG RAW or LOBs returned through
the data interface, that is, the streaming types, then JDBC changes the statement
ow-prefetch setting to 1, even if you never actually read a value of either of these
types.
Setting the prefetch size can affect the performance of an application. Increasing the
prefetch size will reduce the number of round-trips required to get all the data, but
will increase memory usage. This will depend on the number and size of the columns
in the query and the number of rows expected to be returned. It will also depend on
the memory and CPU loading of the JDBC client machine. The optimum for a
standalone client application will be different from a heavily loaded application server.
The speed and latency of the network connection should also be considered.
Note:Starting from Oracle Database 11g Release 1 (11.1), the Thin
driver can fetch the first prefetch_size number of rows from the
server in the very first roundtrip. This saves one roundtrip in SELECT
statements.
If you are using the JDBC Thin driver, then use the useFetchSizeWithLongColumn
connection property, because it will perform PARSE, EXECUTE, and FETCH in a single
round-trip.
Be aware that setting the Oracle fetch size value can affect not
only queries, but also explicitly refetching rows in a result set
through the result set refreshRow method, which is relevant
for scroll-sensitive/read-only, scroll-sensitive/updatable, and
scroll-insensitive/updatable result sets, and the window size of
a scroll-sensitive result set, affecting how often automatic
refetches are performed. However, the Oracle fetch size value
will be overridden by any setting of the fetch size.
Defining Column Types
The implementation of defineColumnType changed significantly since Oracle
Database 10g. Previously, defineColumnType was used both as a performance
optimization and to force data type conversion. In previous releases, all of the drivers
benefited from calls to defineColumnType. Starting from Oracle Database 10g, the
JDBC Thin driver no longer needs the information provided. The JDBC Thin driver
achieves maximum performance without calls to defineColumnType. The JDBC
Oracle Call Interface (OCI) and server-side internal drivers still get better performance
when the application uses defineColumnType.
You can also use defineColumnType to control how much memory the client-side
allocates or to limit the size of variable-length data.
Follow these general steps to define column types for a query:
1. If necessary, cast your statement object to OracleStatement,
  OraclePreparedStatement, or OracleCallableStatement, as applicable.
2. If necessary, use the clearDefines method of your Statement object to clear
  any previous column definitions for this Statement object.
3. On each column, call the defineColumnType method of your Statement
  object, passing it these parameters:
■ Column index (integer)
■ Type code (integer)
Use the static constants of the java.sql.Types class or
oracle.jdbc.OracleTypes class, such as Types.INTEGER,
Types.FLOAT, Types.VARCHAR, OracleTypes.VARCHAR, and
OracleTypes.ROWID. Type codes for standard types are identical in these
two classes.
Type name (string)
For structured objects, object references, and arrays, you must also specify the
type name. For example, Employee, EmployeeRef, or EmployeeArray.
Maximum field size (integer)
Optionally specify a maximum data length for this column.
You cannot specify a maximum field size parameter if you are defining the
column type for a structured object, object reference, or array. If you try to
include this parameter, it will be ignored.
Form of use (short)
Optionally specify a form of use for the column. This can be
OraclePreparedStatement.FORM_CHAR to use the database character set
or OraclePreparedStatement.FORM_NCHAR to use the national character
set. If this parameter is omitted, the default is FORM_CHAR.
For example, assuming stmt is an Oracle statement, use:
stmt.defineColumnType(column_index, typeCode);
If the column is VARCHAR or equivalent and you know the length limit:
stmt.defineColumnType(column_index, typeCode, max_size);
You can also use defineColumnType to control how much memory the client-side
allocates or to limit the size of variable-length data.
Follow these general steps to define column types for a query:
1. If necessary, cast your statement object to OracleStatement,
  OraclePreparedStatement, or OracleCallableStatement, as applicable.
2. If necessary, use the clearDefines method of your Statement object to clear
  any previous column definitions for this Statement object.
3. On each column, call the defineColumnType method of your Statement
  object, passing it these parameters:
■ Column index (integer)
■ Type code (integer)
Use the static constants of the java.sql.Types class or
oracle.jdbc.OracleTypes class, such as Types.INTEGER,
Types.FLOAT, Types.VARCHAR, OracleTypes.VARCHAR, and
OracleTypes.ROWID. Type codes for standard types are identical in these
two classes.
Type name (string)
For structured objects, object references, and arrays, you must also specify the
type name. For example, Employee, EmployeeRef, or EmployeeArray.
Maximum field size (integer)
Optionally specify a maximum data length for this column.
You cannot specify a maximum field size parameter if you are defining the
column type for a structured object, object reference, or array. If you try to
include this parameter, it will be ignored.
Form of use (short)
Optionally specify a form of use for the column. This can be
OraclePreparedStatement.FORM_CHAR to use the database character set
or OraclePreparedStatement.FORM_NCHAR to use the national character
set. If this parameter is omitted, the default is FORM_CHAR.
For example, assuming stmt is an Oracle statement, use:
stmt.defineColumnType(column_index, typeCode);
If the column is VARCHAR or equivalent and you know the length limit:
stmt.defineColumnType(column_index, typeCode, max_size);
You can also use defineColumnType to control how much memory the client-side
allocates or to limit the size of variable-length data.
Follow these general steps to define column types for a query:
1. If necessary, cast your statement object to OracleStatement,
  OraclePreparedStatement, or OracleCallableStatement, as applicable.
2. If necessary, use the clearDefines method of your Statement object to clear
  any previous column definitions for this Statement object.
3. On each column, call the defineColumnType method of your Statement
  object, passing it these parameters:
■ Column index (integer)
■ Type code (integer)
Use the static constants of the java.sql.Types class or
oracle.jdbc.OracleTypes class, such as Types.INTEGER,
Types.FLOAT, Types.VARCHAR, OracleTypes.VARCHAR, and
OracleTypes.ROWID. Type codes for standard types are identical in these
two classes.
Type name (string)
For structured objects, object references, and arrays, you must also specify the
type name. For example, Employee, EmployeeRef, or EmployeeArray.
Maximum field size (integer)
Optionally specify a maximum data length for this column.
You cannot specify a maximum field size parameter if you are defining the
column type for a structured object, object reference, or array. If you try to
include this parameter, it will be ignored.
Form of use (short)
Optionally specify a form of use for the column. This can be
OraclePreparedStatement.FORM_CHAR to use the database character set
or OraclePreparedStatement.FORM_NCHAR to use the national character
set. If this parameter is omitted, the default is FORM_CHAR.
For example, assuming stmt is an Oracle statement, use:
stmt.defineColumnType(column_index, typeCode);
If the column is VARCHAR or equivalent and you know the length limit:
stmt.defineColumnType(column_index, typeCode, max_size);
You can also use defineColumnType to control how much memory the client-side
allocates or to limit the size of variable-length data.
Follow these general steps to define column types for a query:
1. If necessary, cast your statement object to OracleStatement,
  OraclePreparedStatement, or OracleCallableStatement, as applicable.
2. If necessary, use the clearDefines method of your Statement object to clear
  any previous column definitions for this Statement object.
3. On each column, call the defineColumnType method of your Statement
  object, passing it these parameters:
■ Column index (integer)
■ Type code (integer)
Use the static constants of the java.sql.Types class or
oracle.jdbc.OracleTypes class, such as Types.INTEGER,
Types.FLOAT, Types.VARCHAR, OracleTypes.VARCHAR, and
OracleTypes.ROWID. Type codes for standard types are identical in these
two classes.
Type name (string)
For structured objects, object references, and arrays, you must also specify the
type name. For example, Employee, EmployeeRef, or EmployeeArray.
Maximum field size (integer)
Optionally specify a maximum data length for this column.
You cannot specify a maximum field size parameter if you are defining the
column type for a structured object, object reference, or array. If you try to
include this parameter, it will be ignored.
Form of use (short)
Optionally specify a form of use for the column. This can be
OraclePreparedStatement.FORM_CHAR to use the database character set
or OraclePreparedStatement.FORM_NCHAR to use the national character
set. If this parameter is omitted, the default is FORM_CHAR.
For example, assuming stmt is an Oracle statement, use:
stmt.defineColumnType(column_index, typeCode);
If the column is VARCHAR or equivalent and you know the length limit:
stmt.defineColumnType(column_index, typeCode, max_size);
For an NVARCHAR column where the original maximum length is desired and
conversion to the database character set is requested:
stmt.defineColumnType(column_index, typeCode, 0,
OraclePreparedStatement.FORM_CHAR );
For structured object, object reference, and array columns:
stmt.defineColumnType(column_index, typeCode, typeName);
Set a maximum field size if you do not want to receive the full default length of
the data. Calling the setMaxFieldSize method of the standard JDBC
Statement class sets a restriction on the amount of data returned. Specifically, the
size of the data returned will be the minimum of the following:
■ The maximum field size set in defineColumnType
■ The maximum field size set in setMaxFieldSize
■ The natural maximum size of the data type
After you complete these steps, use the executeQuery method of the statement to
perform the query.
Additional Oracle Performance Extensions
原文地址:https://www.cnblogs.com/freewater/p/2158380.html