Are JDBC select queries through a single connection less efficient that a proc containing those queries?












0















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.)










share|improve this question

























  • 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
















0















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.)










share|improve this question

























  • 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














0












0








0


1






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.)










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












2 Answers
2






active

oldest

votes


















1














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.






share|improve this answer
























  • 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





















1














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;





share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1














    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.






    share|improve this answer
























    • 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


















    1














    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.






    share|improve this answer
























    • 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
















    1












    1








    1







    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.






    share|improve this answer













    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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





















    • 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















    1














    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;





    share|improve this answer




























      1














      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;





      share|improve this answer


























        1












        1








        1







        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;





        share|improve this answer













        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;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 7:20









        user7294900user7294900

        22.8k113361




        22.8k113361






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues