oracle - Doing two different batch queries and a delete query in one transaction (Java) -


i want ask best way of optimizing these 3 queries. function going used millions of users in national project , want optimized(fast) possible.

please add helpful comment or answer :) , thank in advance.

 try {     //so don't execute empty batches     boolean flag=false;     conn = datasource.getconnection();     autocommitvalue = conn.getautocommit();     conn.setautocommit(false);     stmt = conn.preparestatement("delete table1 input = ?");     stmt.setlong(1, input);     stmt.executeupdate();     stmt = conn             .preparestatement("insert table1 (c1)  values (?)");      (object info : info list) {         if (info .gettype() == 0) {             flag=true;             stmt.setint(1, info.getc1());             stmt.addbatch();         }     }     if(flag){     result = stmt.executebatch();     flag=false;     }     stmt = conn.preparestatement("insert table1 (c1,c2,c3) values (?,?,?)");      (object info : info list) {         if (info .gettype() != 0) {             flag=true;             stmt.setlong(1, input);             stmt.setint(2, info.getc1());             stmt.setint(3, resinfo.getc2());             stmt.addbatch();          }     }     if(flag){     result = stmt.executebatch();     }     conn.commit();  } catch (sqlexception e) {      conn.rollback();     throw new sqlexception(); } {     if (conn != null) {         // return connection original state         conn.setautocommit(autocommitvalue);     }     closedbobjects(conn, stmt); } 

the first thing do, before trying optimize code, make compiling. following lines don't compile:

for (object info : info list) {     if (info .gettype() != 0) {     ... 

then make code cleaner respecting java naming conventions, splitting long method in several ones, , using better names flag, example.

then measure, test , see if have performance problem. , if have one, try find comes from. , if i'm sure comes method (which unlikely), try optimize it.

let's suppose method needs optimized. first thing make sure appropriate index exists, speed delete statement.

then avoid preparing 2 statements if list empty. avoid execution using flag variable, don't avoid preparation.

then try see if using batch necessary, or if using several ones useful, depending on size of list, on database recommends, , on measurements , tests say.

but said. doing premature optimization, root of evil. try make code correct, clean , maintainable before.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -