Oracle Proc performance optimization experience program

 

Proc AP is one kind of database operations provided by Oracle. It is based on ESql technology, after the required pre-compiled code that can be turned into ordinary c, very intuitive and inconvenient to use, there are also difficult to read.

Because of these problems led to the development of programmers usually appears in the low presence of some Proc operating efficiency, this article describes some Proc some compilers experience, I hope to give you a reference.

 

Below needs a simple example:

DB1 required to export the data inside a data table tbl_hch_test to watch the same DB2.

The fastest way is to use oracle data pump tools to export and then import compression, but expdp / impdp have special requirements for the database environment, so we need to use Proc programming, remove the data start DB1, and then insert the Db2 inside.

 

Simple implementation:

Open a cursor, DB1 out data from the FETCH cycle, and then assembled into the insert statement using sprintf, DB2 to use exec sql Insert and commit instruction execution database.

 

optimization:

While the above approach may complete the requirements, but there are not optimized space efficiency. In turn described below:

1 using bind variables instead of sprintf assembled sql

       Practice has proved that the efficiency of the sprintf function in high demand scenarios easily become a performance bottleneck, avoid using bind variables sprintf call.

       Also, since the sql execution inside the database are the same, do not need to re-analyze every sql execution plan, can greatly reduce the database load and increase efficiency.

 

Two pairs insert Pre-compiling statements, once compiled multiple times, avoid the use of hidden cursor, recompile every time.

       Ibid., Pre-compiler is more consumption of cpu operation. If sql same, the cursor can be reused, reducing the performance expenses.

3 using batch operations, each taking the number of inserted data and using arrays bind.

       Batch operation can reduce the number of interactions between the client and server to speed up the operation data.

4 appropriate to increase the transaction commits interval (insert multiple rows commit time)

       You need to brush back a log from memory when the Oracle database transactions submitted to disk and wait for the disk operation to complete before returning, improve transaction commit interval can reduce the wait consumption.

 

Optimize the efficiency of the test before and after comparison:

 

The same operation is reduced from 1.3s to 23s

 

Other efficiency measures:

These are some of the optimization program, combined with the oracle some features, but also further enhance the speed, introduce a few simple optimization tips:

1 to delete the target table index, insert data and then complete reconstruction

Close log table 2, redo logs to reduce, alter table tbl_hch_test nologing. After loading the data to change it back.

3 using append HINT, inserting the data directly above the high water level, to accelerate the speed of insertion.

4 using the parallel (parallel) queries, or use a partition query (select * from tab parttion (par_name)) speed up queries.

5 If possible, try to execute the program on the database server, reducing network overhead transmission

 

 

The key code reference:

 

 

 

 

 

 

 

 

 

 

Further, usually attached using several Proc prone to errors.

1 using an array of values ​​stored char type field of the database varchar2

       Char array corresponding to the type of database is Proc char, varchar structure corresponding to the type is varchar2. Using an array of values ​​stored in the database varchar2 char type field, it will cause the data as extracted char types, automatically add a space at the end.

       Solution may be used EXEC SQL VAR / EXEC SQL TYPE same variables or data type. Or specified in the pre-compile time CHAR_MAP = string a permanent labor battle

 

 

2 Note proc Some commands are pre-compile command takes effect only during the pre-compiler, and C macro language is very similar. For example, the following command:

EXEC SQL CONTEXT USE

ESEC SQL Whenever Sqlerror Do

 

Here is an explanation common errors:

 

To fully understand the proc out of principle, the proposed multi-difference analysis code .c files generated after .pc file with porc precompiled.

 

3 proc pointer variable

Proc correctly identify common variables and pointers, and pointer variables as the binding method using an ordinary variable, as in the foregoing variables can colon.

Official documents had this to say:

 

 

 

Some programmers do not know how to use pointers in proc, use memcpy to copy the data more than once, increasing unnecessary consumption.

 

 

 

4 proc with the C language macros

Proc能识别C语言的一些简单的宏,但如果有复杂的宏(如不定参数宏),proc在预编译时会报错。

此时可以考虑使用gcc –E先对.pc文件进行预处理,之后再使用proc进行预编译。

 

 

 

如果对于proc编程还有什么其它疑惑的地方,欢迎大家与我讨论,或者查阅官方帮助文档。《Oracle Proc官方文档.pdf》

Guess you like

Origin www.cnblogs.com/kingstarer/p/11968247.html