Are JDBC select queries through a single connection less efficient that a proc containing those queries?
If I open a single JDBC connection (for Oracle), and execute multiple select
queries, will it be less efficient than calling a procedure that executes those queries, and returns the result in cursors?
Edit: Sample queries are:
select id, name from animals;
select * from animal_reservoir where animal_id=id;
(The actual first query would be quite complicated, and the id returned would be used as an input multiple times in the second query. As such, the first query will be inefficient to use as subquery in the second query. Also, the queries can't be combined.)
java oracle jdbc
add a comment |
If I open a single JDBC connection (for Oracle), and execute multiple select
queries, will it be less efficient than calling a procedure that executes those queries, and returns the result in cursors?
Edit: Sample queries are:
select id, name from animals;
select * from animal_reservoir where animal_id=id;
(The actual first query would be quite complicated, and the id returned would be used as an input multiple times in the second query. As such, the first query will be inefficient to use as subquery in the second query. Also, the queries can't be combined.)
java oracle jdbc
What are these selects, and how would you even use a single cursor here?
– Tim Biegeleisen
Nov 15 '18 at 5:39
download.oracle.com/otn-pub/jcp/jdbc-4_1-mrel-spec/… doesn't say anything specific on the said matter. But in Chapter 14 it says "The batch update facility allows multiple SQL statements to be submitted to a data source for processing at once. Submitting multiple SQL statements, instead of individually, can greatly improve performance." So I presume that it would be better to execute it by proc (or batch as it say) rather than multiple single queries...
– Ketan
Nov 15 '18 at 6:02
Why not combine it to 1 SQL query? it looks possible
– user7294900
Nov 15 '18 at 6:41
Updated the question. @Ketan I want to execute multiple select queries, not update queries
– Daud
Nov 15 '18 at 6:43
add a comment |
If I open a single JDBC connection (for Oracle), and execute multiple select
queries, will it be less efficient than calling a procedure that executes those queries, and returns the result in cursors?
Edit: Sample queries are:
select id, name from animals;
select * from animal_reservoir where animal_id=id;
(The actual first query would be quite complicated, and the id returned would be used as an input multiple times in the second query. As such, the first query will be inefficient to use as subquery in the second query. Also, the queries can't be combined.)
java oracle jdbc
If I open a single JDBC connection (for Oracle), and execute multiple select
queries, will it be less efficient than calling a procedure that executes those queries, and returns the result in cursors?
Edit: Sample queries are:
select id, name from animals;
select * from animal_reservoir where animal_id=id;
(The actual first query would be quite complicated, and the id returned would be used as an input multiple times in the second query. As such, the first query will be inefficient to use as subquery in the second query. Also, the queries can't be combined.)
java oracle jdbc
java oracle jdbc
edited Nov 15 '18 at 6:43
Daud
asked Nov 15 '18 at 5:32
DaudDaud
2,594134496
2,594134496
What are these selects, and how would you even use a single cursor here?
– Tim Biegeleisen
Nov 15 '18 at 5:39
download.oracle.com/otn-pub/jcp/jdbc-4_1-mrel-spec/… doesn't say anything specific on the said matter. But in Chapter 14 it says "The batch update facility allows multiple SQL statements to be submitted to a data source for processing at once. Submitting multiple SQL statements, instead of individually, can greatly improve performance." So I presume that it would be better to execute it by proc (or batch as it say) rather than multiple single queries...
– Ketan
Nov 15 '18 at 6:02
Why not combine it to 1 SQL query? it looks possible
– user7294900
Nov 15 '18 at 6:41
Updated the question. @Ketan I want to execute multiple select queries, not update queries
– Daud
Nov 15 '18 at 6:43
add a comment |
What are these selects, and how would you even use a single cursor here?
– Tim Biegeleisen
Nov 15 '18 at 5:39
download.oracle.com/otn-pub/jcp/jdbc-4_1-mrel-spec/… doesn't say anything specific on the said matter. But in Chapter 14 it says "The batch update facility allows multiple SQL statements to be submitted to a data source for processing at once. Submitting multiple SQL statements, instead of individually, can greatly improve performance." So I presume that it would be better to execute it by proc (or batch as it say) rather than multiple single queries...
– Ketan
Nov 15 '18 at 6:02
Why not combine it to 1 SQL query? it looks possible
– user7294900
Nov 15 '18 at 6:41
Updated the question. @Ketan I want to execute multiple select queries, not update queries
– Daud
Nov 15 '18 at 6:43
What are these selects, and how would you even use a single cursor here?
– Tim Biegeleisen
Nov 15 '18 at 5:39
What are these selects, and how would you even use a single cursor here?
– Tim Biegeleisen
Nov 15 '18 at 5:39
download.oracle.com/otn-pub/jcp/jdbc-4_1-mrel-spec/… doesn't say anything specific on the said matter. But in Chapter 14 it says "The batch update facility allows multiple SQL statements to be submitted to a data source for processing at once. Submitting multiple SQL statements, instead of individually, can greatly improve performance." So I presume that it would be better to execute it by proc (or batch as it say) rather than multiple single queries...
– Ketan
Nov 15 '18 at 6:02
download.oracle.com/otn-pub/jcp/jdbc-4_1-mrel-spec/… doesn't say anything specific on the said matter. But in Chapter 14 it says "The batch update facility allows multiple SQL statements to be submitted to a data source for processing at once. Submitting multiple SQL statements, instead of individually, can greatly improve performance." So I presume that it would be better to execute it by proc (or batch as it say) rather than multiple single queries...
– Ketan
Nov 15 '18 at 6:02
Why not combine it to 1 SQL query? it looks possible
– user7294900
Nov 15 '18 at 6:41
Why not combine it to 1 SQL query? it looks possible
– user7294900
Nov 15 '18 at 6:41
Updated the question. @Ketan I want to execute multiple select queries, not update queries
– Daud
Nov 15 '18 at 6:43
Updated the question. @Ketan I want to execute multiple select queries, not update queries
– Daud
Nov 15 '18 at 6:43
add a comment |
2 Answers
2
active
oldest
votes
The two main differences are
- fewer roundtrips (important if there are many small queries, otherwise not so much)
- no need to send "intermediate" results (that are only needed for the next query, but not in the end) back to the client
How much of an impact this has completely depends on the application.
And often, there may be other alternatives (such as issuing different kind of queries in the first place; someone mentioned a JOIN in the comments -- or caching -- or indexing -- or data denormalization -- or ...
) to consider as well.
As usual, do what feels most natural first and optimize when you find there is an issue.
Since I'm reusing the same connection, calling multiple select queries should have the same impact as calling those same queries inside a procedure which is called just once, right?
– Daud
Nov 15 '18 at 7:40
1
If you call three queries it goes to the server and back three times. If you call one (server-side) stored procedure, it only does a single round-trip (unless the results need to be paged). If your application does not need the result from the first two queries (but only the result from the third one), then the procedure only needs to return the third result (and the first two do not leave the database server).
– Thilo
Nov 15 '18 at 8:25
add a comment |
You haven't provided SQL queries that must use procedure
You can do 1 SQL query with multiple "inner SQL" using with
clause for example:
with animals as (
select id, name from animals
)
select * from animal_reservoir,animals where animal_id=animals.id;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313020%2fare-jdbc-select-queries-through-a-single-connection-less-efficient-that-a-proc-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The two main differences are
- fewer roundtrips (important if there are many small queries, otherwise not so much)
- no need to send "intermediate" results (that are only needed for the next query, but not in the end) back to the client
How much of an impact this has completely depends on the application.
And often, there may be other alternatives (such as issuing different kind of queries in the first place; someone mentioned a JOIN in the comments -- or caching -- or indexing -- or data denormalization -- or ...
) to consider as well.
As usual, do what feels most natural first and optimize when you find there is an issue.
Since I'm reusing the same connection, calling multiple select queries should have the same impact as calling those same queries inside a procedure which is called just once, right?
– Daud
Nov 15 '18 at 7:40
1
If you call three queries it goes to the server and back three times. If you call one (server-side) stored procedure, it only does a single round-trip (unless the results need to be paged). If your application does not need the result from the first two queries (but only the result from the third one), then the procedure only needs to return the third result (and the first two do not leave the database server).
– Thilo
Nov 15 '18 at 8:25
add a comment |
The two main differences are
- fewer roundtrips (important if there are many small queries, otherwise not so much)
- no need to send "intermediate" results (that are only needed for the next query, but not in the end) back to the client
How much of an impact this has completely depends on the application.
And often, there may be other alternatives (such as issuing different kind of queries in the first place; someone mentioned a JOIN in the comments -- or caching -- or indexing -- or data denormalization -- or ...
) to consider as well.
As usual, do what feels most natural first and optimize when you find there is an issue.
Since I'm reusing the same connection, calling multiple select queries should have the same impact as calling those same queries inside a procedure which is called just once, right?
– Daud
Nov 15 '18 at 7:40
1
If you call three queries it goes to the server and back three times. If you call one (server-side) stored procedure, it only does a single round-trip (unless the results need to be paged). If your application does not need the result from the first two queries (but only the result from the third one), then the procedure only needs to return the third result (and the first two do not leave the database server).
– Thilo
Nov 15 '18 at 8:25
add a comment |
The two main differences are
- fewer roundtrips (important if there are many small queries, otherwise not so much)
- no need to send "intermediate" results (that are only needed for the next query, but not in the end) back to the client
How much of an impact this has completely depends on the application.
And often, there may be other alternatives (such as issuing different kind of queries in the first place; someone mentioned a JOIN in the comments -- or caching -- or indexing -- or data denormalization -- or ...
) to consider as well.
As usual, do what feels most natural first and optimize when you find there is an issue.
The two main differences are
- fewer roundtrips (important if there are many small queries, otherwise not so much)
- no need to send "intermediate" results (that are only needed for the next query, but not in the end) back to the client
How much of an impact this has completely depends on the application.
And often, there may be other alternatives (such as issuing different kind of queries in the first place; someone mentioned a JOIN in the comments -- or caching -- or indexing -- or data denormalization -- or ...
) to consider as well.
As usual, do what feels most natural first and optimize when you find there is an issue.
answered Nov 15 '18 at 7:29
ThiloThilo
196k78421575
196k78421575
Since I'm reusing the same connection, calling multiple select queries should have the same impact as calling those same queries inside a procedure which is called just once, right?
– Daud
Nov 15 '18 at 7:40
1
If you call three queries it goes to the server and back three times. If you call one (server-side) stored procedure, it only does a single round-trip (unless the results need to be paged). If your application does not need the result from the first two queries (but only the result from the third one), then the procedure only needs to return the third result (and the first two do not leave the database server).
– Thilo
Nov 15 '18 at 8:25
add a comment |
Since I'm reusing the same connection, calling multiple select queries should have the same impact as calling those same queries inside a procedure which is called just once, right?
– Daud
Nov 15 '18 at 7:40
1
If you call three queries it goes to the server and back three times. If you call one (server-side) stored procedure, it only does a single round-trip (unless the results need to be paged). If your application does not need the result from the first two queries (but only the result from the third one), then the procedure only needs to return the third result (and the first two do not leave the database server).
– Thilo
Nov 15 '18 at 8:25
Since I'm reusing the same connection, calling multiple select queries should have the same impact as calling those same queries inside a procedure which is called just once, right?
– Daud
Nov 15 '18 at 7:40
Since I'm reusing the same connection, calling multiple select queries should have the same impact as calling those same queries inside a procedure which is called just once, right?
– Daud
Nov 15 '18 at 7:40
1
1
If you call three queries it goes to the server and back three times. If you call one (server-side) stored procedure, it only does a single round-trip (unless the results need to be paged). If your application does not need the result from the first two queries (but only the result from the third one), then the procedure only needs to return the third result (and the first two do not leave the database server).
– Thilo
Nov 15 '18 at 8:25
If you call three queries it goes to the server and back three times. If you call one (server-side) stored procedure, it only does a single round-trip (unless the results need to be paged). If your application does not need the result from the first two queries (but only the result from the third one), then the procedure only needs to return the third result (and the first two do not leave the database server).
– Thilo
Nov 15 '18 at 8:25
add a comment |
You haven't provided SQL queries that must use procedure
You can do 1 SQL query with multiple "inner SQL" using with
clause for example:
with animals as (
select id, name from animals
)
select * from animal_reservoir,animals where animal_id=animals.id;
add a comment |
You haven't provided SQL queries that must use procedure
You can do 1 SQL query with multiple "inner SQL" using with
clause for example:
with animals as (
select id, name from animals
)
select * from animal_reservoir,animals where animal_id=animals.id;
add a comment |
You haven't provided SQL queries that must use procedure
You can do 1 SQL query with multiple "inner SQL" using with
clause for example:
with animals as (
select id, name from animals
)
select * from animal_reservoir,animals where animal_id=animals.id;
You haven't provided SQL queries that must use procedure
You can do 1 SQL query with multiple "inner SQL" using with
clause for example:
with animals as (
select id, name from animals
)
select * from animal_reservoir,animals where animal_id=animals.id;
answered Nov 15 '18 at 7:20
user7294900user7294900
22.8k113361
22.8k113361
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313020%2fare-jdbc-select-queries-through-a-single-connection-less-efficient-that-a-proc-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What are these selects, and how would you even use a single cursor here?
– Tim Biegeleisen
Nov 15 '18 at 5:39
download.oracle.com/otn-pub/jcp/jdbc-4_1-mrel-spec/… doesn't say anything specific on the said matter. But in Chapter 14 it says "The batch update facility allows multiple SQL statements to be submitted to a data source for processing at once. Submitting multiple SQL statements, instead of individually, can greatly improve performance." So I presume that it would be better to execute it by proc (or batch as it say) rather than multiple single queries...
– Ketan
Nov 15 '18 at 6:02
Why not combine it to 1 SQL query? it looks possible
– user7294900
Nov 15 '18 at 6:41
Updated the question. @Ketan I want to execute multiple select queries, not update queries
– Daud
Nov 15 '18 at 6:43