简体   繁体   中英

Performance difference between ref cursor and straight Java code

For last couple of days, I looked at some other people's code and noticed that there were lots of PL/SQL functions returning REF CURSOR. Those functions are then called in Java with CallableStatement.

For example, there might be a function defined in PL/SQL as:

FUNCTION FOO(a NUMBER) RETURN REF CURSOR IS

And in Java:

CallableStatement call = connection.prepareCall("{ ? = call foo(?) }");
call.registerOutParameter(1, OracleTypes.CURSOR);
call.setInt(2, 12345);
call.execute();
resultSet = (ResultSet)call.getObject(1);
while (resultSet.next()) {

Comparing to a second approach - to have the query planted in Java, with no PL/SQL involved, I can see pro's in both approaches:

  1. With PL/SQL, it does hide the internal database tables and structures from Java developers, gaining some sort of encapsulation; also the same functions can be reused both in Java and by other PL/SQL packages.
  2. With Java, it is a lot less vendor dependent.

Having said the above, and ignore the skill sets on a development team, could there be any performance difference, network bandwidth difference? Any insight and/or experience you can share?

I think it depends on what the application is doing inside while (resultSet.next()) loop. Ref cursor will be beneficial if, for instance, you are passing it back and forth between Oracle and client .

If client fetches all the rows I'd rather go with a bit different solution - rewrite functions so they return collection of objects, and use TABLE function on client side (eg SELECT * FROM TABLE(Foo(1)) ) . It may or may not work for you (say, if you have other PL/SQL procedures/functions that really benefit from using of REF CURSOR) .
Also, it may be worth trying implicit statement result ( http://docs.oracle.com/database/121/LNPLS/release_changes.htm#LNPLS118 ) -

As of Oracle Database 12c, a PL/SQL stored subprogram can return query results to its client implicitly, using the PL/SQL package DBMS_SQL instead of OUT REF CURSOR parameters. This technique makes it easy to migrate applications that rely on the implicit return of query results from stored subprograms from third-party databases to Oracle Database

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM