Java Database Connectivity (JDBC) - Optimising JDBC Performance

I am newbie in JDBC and i am having a performance issue in deleting some data from tables,

this is the code block which i am using for selecting a perticular field from one table and than i go to other tables and delete data related to a perticular record

public void deleteRecord()

{

Statement stmt = null;

PreparedStatement qld_cal_seq = null;

PreparedStatement neg_fr_sl = null;

PreparedStatement cat_xpr = null;

PreparedStatement non_cmb_ru = null;

int commitlevel = 100 ;

ResultSet rs = null;

try

{

int qld=0;

int neg=0;

int cat=0;

int non=0;

stmt=AtseDBConnection.atseConn.createStatement();

qld_cal_seq = AtseDBConnection.atseConn.prepareStatement("DELETE FROM qld_cal_seq WHERE neg_fr_sl_obj_id = ? ");

neg_fr_sl = AtseDBConnection.atseConn.prepareStatement("DELETE FROM neg_fr_sl WHERE non_cmb_ru_obj_id = ? ");

cat_xpr = AtseDBConnection.atseConn.prepareStatement("DELETE FROM cat_xpr WHERE ncr_alt_obj_id = ? ");

non_cmb_ru = AtseDBConnection.atseConn.prepareStatement( "DELETE FROM non_cmb_ru WHERE obj_id = ? ");

String purgeMonths = properties.getProperty("atse.db.purgeMonths");

if (purgeMonths == null || purgeMonths.equals(""))

{

logger.error("atse.db.purgeDate property is not specified,"+ " exiting");

System.exit(Constants.INITIALIZATION_FAILURE);

}

// final String SELECT_non_cmb_ru = "SELECT obj_id FROM non_cmb_ru where exp_dt < today - " + purgeMonths + " UNITS MONTH ";

final String SELECT_non_cmb_ru = "SELECT obj_id from non_cmb_ru where ( exp_dt < today - 15 units month ) and ( exp_dt > today - 17 units month )" ;

rs=stmt.executeQuery( SELECT_non_cmb_ru );

long rows = 0 ;

while (rs.next())

{

int ctr=0;

String objid=rs.getString("obj_id");

qld_cal_seq.setString(1, objid);

neg_fr_sl.setString(1, objid);

cat_xpr.setString(1, objid);

non_cmb_ru.setString(1, objid);

ctr= qld_cal_seq.executeUpdate();

qld+=ctr ;

ctr=neg_fr_sl.executeUpdate();

neg+=ctr ;

ctr=cat_xpr.executeUpdate();

cat+=ctr ;

ctr=non_cmb_ru.executeUpdate();

non+=ctr ;

rows++;

System.out.println("rows % commitlevel: - " +rows % commitlevel);

if ( rows % commitlevel == 0)

{

AtseDBConnection.atseConn.commit();

rows=1;

}

}

AtseDBConnection.atseConn.commit();

System.out.println("Total Rows deleted from QLD_CAL_SEQ table:" + qld);

System.out.println("Total Rows deleted from NEG_FR_SL Table:" + neg);

System.out.println("Total Rows deleted from CAT_XPR Table:" + cat);

System.out.println("Total Rows deleted from NON_CMB_RU Table:" + non);

//System.out.println("After deleteing the records of objid:"+ objid);

//AtseDBConnection.atseConn.commit();

}

catch (SQLException e)

{

System.out.println ( e.getMessage ());

e.printStackTrace ();

}

finally

{

// Close SQL Objects

try

{

if (rs != null)

{

rs.close();

}

if (qld_cal_seq != null)

{

qld_cal_seq.close();

}

if (neg_fr_sl!=null)

{

neg_fr_sl.close();

}

if (cat_xpr != null)

{

cat_xpr.close();

}

if (non_cmb_ru != null)

{

non_cmb_ru.close();

}

if (stmt != null)

{

stmt.close();

}

AtseDBConnection.atseConn.close();

}

catch(SQLException e)

{

System.out.println (e.getMessage ());

e.printStackTrace ();

}

}

}

this code performs very slowly as the tables which this is refering to has huge amount of data,

Can some one please advice me how can i enhance performance of this code.

Thanks in Adv....

Vivek

[3941 byte] By [vikky_ca] at [2007-11-26 23:19:51]
# 1

You can probably do whatever you're trying to do with SQL DELETE statements. I didn't bother trying to read your code because you couldn't be bothered to use the code formatting.

You can issue a statement of the following form:

delete from FOO where ID in (SELECT ID FROM BAR WHERE ... )

dcmintera at 2007-7-10 14:22:58 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

Thanks for your response, Sorry i have not formatted code earlier...

public void deleteRecord()

{

Statement stmt = null;

PreparedStatement qld_cal_seq = null;

PreparedStatement neg_fr_sl = null;

PreparedStatement cat_xpr = null;

PreparedStatement non_cmb_ru = null;

int commitlevel = 100 ;

ResultSet rs = null;

try

{

int qld=0;

int neg=0;

int cat=0;

int non=0;

stmt=AtseDBConnection.atseConn.createStatement();

qld_cal_seq = AtseDBConnection.atseConn.prepareStatement("DELETE FROM qld_cal_seq WHERE neg_fr_sl_obj_id = ?");

neg_fr_sl = AtseDBConnection.atseConn.prepareStatement("DELETE FROM neg_fr_sl WHERE non_cmb_ru_obj_id = ?");

cat_xpr = AtseDBConnection.atseConn.prepareStatement("DELETE FROM cat_xpr WHERE ncr_alt_obj_id = ? ");

non_cmb_ru = AtseDBConnection.atseConn.prepareStatement("DELETE FROM non_cmb_ru WHERE obj_id = ? ");

String purgeMonths = properties.getProperty("atse.db.purgeMonths");

if (purgeMonths == null || purgeMonths.equals(""))

{

logger.error("atse.db.purgeDate property is not specified,"+ " exiting");

System.exit(Constants.INITIALIZATION_FAILURE);

}

final String SELECT_non_cmb_ru = "SELECT obj_id from non_cmb_ru where ( exp_dt < today - 15 units month ) and ( exp_dt > today - 17 units month )" ;

rs=stmt.executeQuery( SELECT_non_cmb_ru );

long rows = 0 ;

while (rs.next())

{

int ctr=0;

String objid=rs.getString("obj_id");

qld_cal_seq.setString(1, objid);

neg_fr_sl.setString(1, objid);

cat_xpr.setString(1, objid);

non_cmb_ru.setString(1, objid);

ctr= qld_cal_seq.executeUpdate();

qld +=ctr ;

ctr=neg_fr_sl.executeUpdate();

neg+=ctr ;

ctr=cat_xpr.executeUpdate();

cat +=ctr ;

ctr=non_cmb_ru.executeUpdate();

non +=ctr ;

rows++;

System.out.println("rows % commitlevel: - " +rows % commitlevel);

if ( rows % commitlevel == 0)

{

AtseDBConnection.atseConn.commit();

rows=1;

}

}

AtseDBConnection.atseConn.commit();

System.out.println("Total Rows deleted from QLD_CAL_SEQ table:" + qld);

System.out.println("Total Rows deleted from NEG_FR_SL Table:" + neg);

System.out.println("Total Rows deleted from CAT_XPR Table:" + cat);

System.out.println("Total Rows deleted from NON_CMB_RU Table:" + non);

//System.out.println("After deleteing the records of objid:"+ objid);

//AtseDBConnection.atseConn.commit();

}

catch (SQLException e)

{

System.out.println ( e.getMessage ());

e.printStackTrace ();

}

finally

{

// Close SQL Objects

try

{

if (rs != null)

{

rs.close();

}

if (qld_cal_seq != null)

{

qld_cal_seq.close();

}

if (neg_fr_sl!=null)

{

neg_fr_sl.close();

}

if (cat_xpr != null)

{

cat_xpr.close();

}

if (non_cmb_ru != null)

{

non_cmb_ru.close();

}

if (stmt != null)

{

stmt.close();

}

AtseDBConnection.atseConn.close();

}

catch(SQLException e)

{

System.out.println (e.getMessage ());

e.printStackTrace ();

}

}

}

Message was edited by:

vikky_c

vikky_ca at 2007-7-10 14:22:58 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

Use the where column in ( query ) syntax to carry out the deletions.

For example:

DELETE FROM

qld_cal_seq

WHERE

neg_fr_sl_obj_id

IN (SELECT

obj_id

FROM

non_cmb_ru

WHERE

(exp_dt < today - 15 units month )

AND

( exp_dt > today - 17 units month )

)

dcmintera at 2007-7-10 14:22:58 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
Oh, and then go and kick whoever chose your table and column names :-)
dcmintera at 2007-7-10 14:22:58 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

I have updated my code as per your suggestion now it looks like this :

public void deleteRecord()

{

Statement stmt = null;

String qld_cal_seq = null;

String neg_fr_sl = null;

String cat_xpr = null;

String non_cmb_ru = null;

int commitlevel = 100 ;

ResultSet rs = null;

int [] updateCount = null;

try

{

int qld=0;

int neg=0;

int cat=0;

int non=0;

stmt=AtseDBConnection.atseConn.createStatement();

qld_cal_seq = "DELETE FROM qld_cal_seq WHERE neg_fr_sl_obj_id in (SELECT obj_id from non_cmb_ru where ( exp_dt < today - 15 units month ) and ( exp_dt > today - 17 units month ))";

neg_fr_sl = "DELETE FROM neg_fr_sl WHERE non_cmb_ru_obj_id in (SELECT obj_id from non_cmb_ru where ( exp_dt < today - 15 units month ) and ( exp_dt > today - 17 units month ))";

cat_xpr = "DELETE FROM cat_xpr WHERE ncr_alt_obj_id in (SELECT obj_id from non_cmb_ru where ( exp_dt < today - 15 units month ) and ( exp_dt > today - 17 units month ))";

non_cmb_ru = "DELETE FROM non_cmb_ru WHERE obj_id in (SELECT obj_id from non_cmb_ru where ( exp_dt < today - 15 units month ) and ( exp_dt > today - 17 units month ))";

String purgeMonths = properties.getProperty("atse.db.purgeMonths");

if (purgeMonths == null || purgeMonths.equals(""))

{

logger.error("atse.db.purgeDate property is not specified,"+ " exiting");

System.exit(Constants.INITIALIZATION_FAILURE);

}

System.out.println("Calling ExecuteUpdate");

stmt.addBatch(qld_cal_seq);

stmt.addBatch(neg_fr_sl);

stmt.addBatch(cat_xpr);

stmt.addBatch(non_cmb_ru);

updateCount = stmt.executeBatch();

AtseDBConnection.atseConn.commit();

System.out.println("Total Rows deleted :" + updateCount);

}

catch (SQLException e)

{

System.out.println ( e.getMessage ());

e.printStackTrace ();

}

finally

{

// Close SQL Objects

try

{

if (stmt != null)

{

stmt.close();

}

AtseDBConnection.atseConn.close();

}

catch(SQLException e)

{

System.out.println (e.getMessage ());

e.printStackTrace ();

}

}

}

please advice me if this is the best way to optimize this code.

Thanks Again.

Vikky

vikky_ca at 2007-7-10 14:22:58 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6
Well, you tell us... how does it perform? I'd be astonished if it wasn't a hell of a lot quicker than round-tripping the PK data.
dcmintera at 2007-7-10 14:22:58 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7
Thanks dcminter for showing interest in my problem, due to some technical problems in database i am not able to run it to check its performance. I will let you know about its performance as soon as db will come up. Vikky
vikky_ca at 2007-7-10 14:22:58 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8
Ok. By the way, I haven't checked, but your logic to check the number of rows deleted looks wrong to me - I think that'll tell you how many statements in the batch were executed which will always be 4.
dcmintera at 2007-7-10 14:22:58 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 9
yeah i can see the performance is very much enhanced :).As far as the updateCount is concerned executeBatch returns an array of commands executed and elements of this array refer to the commands added to the batch and number of rows effected by the query.Thanks,Vivek
vikky_ca at 2007-7-10 14:22:58 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...