在proc*c中如何提高oracle游标的效率

在oracle的proc编程过程中,游标是一个大量使用的特性。但是proc的程序作为oracle的客户端程序,是需要通过通讯与oracle服务端进行交互的。所以在大量数据fetch的过程中,抛开查询本身的问题外,网络交互将成为影响性能的一个重要的指标。
实际上,proc有一个预编译选项,叫做PREFETCH。在oracle的手册中提到:
PREFETCH
Purpose
Use this option to speed up queries by pre-fetching a number of rows.
Syntax
PREFETCH=integer
Default
1
Usage Notes
Can be used in a configuration file or on the command-line. The value of the integer is
used for execution of all queries using explicit cursors, subject to the rules of
precedence.
When used in-line it must placed before OPEN statements with explicit cursors. Then
the number of rows pre-fetched when that OPEN is done is determined by the last
in-line PREFETCH option in effect.
The value range allowed is 0.. 65535.

 
我们可以在linux下用strace查看程序实际的运行过程。在增加了PREFETCH=50的情况下:
write(5, "\0\25\0\0\6\0\0\0\0\0\3\5$\2\0\0\0003\0\0\0", 21) = 21
read(5, "\7\333\0\0\6\0\0\0\0\0\6\0\2\320\4\0\0\0\0\0003\0\0\0\0"..., 2064) = 1448
read(5, "\1\7xl\3\7\1\1\1\5\304\0051J\17\5\304\0051J\17\5\304\005"..., 563) = 563
read(5, "\2\n\0\0\6\0\0\0\0\0jbVkTyK\4\303\v\1\34\0033FB\25\10\000"..., 2064) = 522
第一个write调用是oracle客户端向服务端请求数据的请求,后续的3个read调用,是客户端从服务端获取数据的过程,按照上面的说法,这里应该一次获取了50条的数据。

我们再把PREFETCH这个预编译选项去掉的话,可以看见:
write(5, "\0\25\0\0\6\0\0\0\0\0\3\5\27\2\0\0\0\2\0\0\0", 21) = 21
read(5, "\1\31\0\0\6\0\0\0\0\0\6\0\2\320\4\0\0\0\0\0\2\0\0\0\0\0"..., 2064) = 281
很明显一个请求发出后,后面收到的数据就少多了,按照文档中的说法,这里只获取了1条的数据。

相比之下通讯的开销差异是很大的。

你可能感兴趣的:(oracle,C++,c,linux,C#)