Reputation: 969
When using stored procedure is it faster to return a cursor back? or is it faster to use the out data parameter of the data type. or it really won't have any performance difference.
PROCEDURE SPUSINGCURSOR ( input1 INT,
retcursor out sys_refcursor ) IS
BEGIN
OPEN retcursor FOR
SELECT var1,
var2,
var3
FROM Table_Vlaues
WHERE var1 = input1
ORDER BY var1;
END;
Upvotes: 3
Views: 285
Reputation: 48121
It depends, on lots of things.
If you execute the query within the SP and return a nested table type containing the results, the SP will take time to run and consume memory required to hold the data. It will also need to send all that data back to the client in one go, which will take some time. But if you need to access all the data before going on to do anything else, and the memory requirement is not too large, this is a reasonable way to do things.
If you instead open and return a REF CURSOR, the actual SP execution time and the memory required will be less; but you are just deferring the execution time to later, when you fetch from the cursor. If there is a good chance you won't need to fetch all the rows; or if the data set is likely to be very large; or if fetching rows in small batches allows you to do some other meaningful work in-between (such as pipe them to another thread or process that will actually do something with them), then this might be a superior way to go. The main downside of returning a cursor is that fetching from it is then likely to require more network round-trips which could be slower overall than just returning the data all at once.
A non-performance advantage of returning an open REF CURSOR is that it is easier to work with in some client environments (e.g. in JDBC it maps directly to a ResultSet, whereas for a nested table type you need to do extra work to provide Java with the type information).
Upvotes: 7