Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / June 2005

Tip: Looking for answers? Try searching our database.

Optimization

Thread view: 
pk - 23 Jun 2005 14:49 GMT
Hello all,

My question stems purely from curiousity, and I'd be much obliged if
you'd help, but it's not imperative that I get an answer.  I wrote some
code that runs a whole bunch of queries for me and it works, but I'm
very put off by the extremely long time it takes.  I'm sure there is a
better way to do what I'm doing, so I'm would like someone to redirect
my efforts towards a better way of doing things.  In short, doing 540*2
queries and this obviously takes forever and a day.  Alright, lemme
hear your ideas.

Code Purpose:  Create a file for bulk insertion.

PreparedStatement pstmt = conn.prepareStatement("select IV00108.TOQTY,
IV00108.FROMQTY, IV00108.ITEMNMBR, IV00108.UOMPRICE FROM IV00108 WHERE
PRCLEVEL = 'LEVEL 01' AND ITEMNMBR = ? ORDER BY ITEMNMBR");
PreparedStatement pstmtList = conn.prepareStatement("select
IV00105.LISTPRCE FROM IV00105 WHERE ITEMNMBR = ? AND CURNCYID = 'Z-US$'
ORDER BY ITEMNMBR");
String temp3 = "";
temp3 = bp.readLine();
String[] qtyFROM, qtyTO, uomPRICE, itemNMBR;
String listPrce;
int rSets = 0;
while(temp3 != null){
    pstmt.clearParameters();
    pstmt.setString(1, temp3);
    pstmtList.clearParameters();
    pstmtList.setString(1, temp3);
    ResultSet bpRS = pstmt.executeQuery();
    ResultSet bpRSListPrc = pstmtList.executeQuery();
    qtyFROM = new String[12];
    qtyTO = new String[12];
    itemNMBR = new String[12];
    uomPRICE = new String[12];
int t = 0;
bpRSListPrc.next();
listPrce = bpRSListPrc.getString("LISTPRCE");
rSets++;
System.out.println(rSets);
    while (bpRS.next()){
        qtyFROM[t] = bpRS.getString("FROMQTY");
        qtyTO[t] = bpRS.getString("TOQTY");
        itemNMBR[t] = bpRS.getString("ITEMNMBR");
        uomPRICE[t] = bpRS.getString("UOMPRICE");
        Float uom = Float.valueOf(uomPRICE[t]).floatValue();
        Float list = Float.valueOf(listPrce).floatValue();
        Float adjustedPrice = (uom * .01f) * list;
        adjustedPrice = Math.round(adjustedPrice*100)/100f;
        bulk.println("NET\tI\t" + temp3.trim() + "\tEACH\t" + qtyFROM[t] +
"\t" + qtyTO[t] + "\t" + adjustedPrice + "\t1\t1\t" + 16384 * (t+1));
        t++;
    }
    temp3 = bp.readLine();
    bpRS.close();
    bpRSListPrc.close();
}
Robert Klemme - 23 Jun 2005 15:57 GMT
> Hello all,
>
[quoted text clipped - 8 lines]
>
> Code Purpose:  Create a file for bulk insertion.

Indending makes it much easier to read your code - unfortunately google
groups does not maintain leading white space.  Also, you don't mention the
db brand and what exactly takes long.  Is it the queries?  Is it somewhere
in your code?  I guess it's in the db, but in that case one would need to
know more details about the schema.  Maybe you can improve with proper
indexing.

Kind regards

   robert
joeNOSPAM@BEA.com - 23 Jun 2005 17:37 GMT
Hi. The two things I'd try are:
1 - Take in all the input lines first, then run your 2 queries, once
each. Ie:

StringBuffer variables = newStringBuffer("{ ");
boolean first = true;

while(temp3 != null)
{
 if (first)
 {
   variables.append( temp3 );
   first = false;
 }
 else
 {
   variables.append( temp3 + ", " );
 }
}

variables.append( " } ";

PreparedStatement pstmt = conn.prepareStatement(
   "select IV00108.TOQTY, IV00108.FROMQTY, IV00108.ITEMNMBR, "
   + " IV00108.UOMPRICE FROM IV00108 WHERE PRCLEVEL = 'LEVEL 01'
   + " AND ITEMNMBR IN " + variables.toString() + " ORDER BY
ITEMNMBR");

PreparedStatement pstmtList = conn.prepareStatement(
   "select IV00105.LISTPRCE FROM IV00105 WHERE ITEMNMBR IN "
   + variables.toString() + " AND CURNCYID = 'Z-US$' ORDER BY
ITEMNMBR");

2 - Make sure your ORDER BY clauses are supported by appropriate
indexes in the DBMS.

Let me know...
Joe Weinstein at BEA Systems
joeNOSPAM@BEA.com - 23 Jun 2005 18:28 GMT
Oh, and anything you could do to turn the individual line values into
a range would make it faster. Ie: "WHERE ITEMNMBR >= XXX AND
ITEMNBR <= YYY".
  Another thing, is that because the statements will not be re-used,
it might be faster to make them plain statements.
 Lastly, I might even do the queries with a range anyway, getting
some rows I don't care about, because it would be faster, and
then process every row, checking if it's ITEMNMBR is in the
current local list you've collected from input. If the ratio of
needed rows to not-needed rows is high, this might be fastest.
Joe Weinstein at BEA
Thomas Ganss - 26 Jun 2005 02:57 GMT
pk schrieb:
> In short, doing 540*2
> queries and this obviously takes forever and a day.  Alright, lemme
> hear your ideas.

How about filling all ITEMNMBR to be searched into a second table
and running one inner join to get the resultset for all your queries ?

regards

thomas


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.