0

So I have an application that I start on 2PC. Each instance launches an HSQLDB in server mode.

I'm trying to get the sales of different products.

So I query the local database and fill an arraylist with the name of the product and the number of sales.

Then I execute the same query on the other database on the other PC.

For 1 product, I so have two lines (each one correspond to one database). Here, results are false but the execution time is ok.

In order to manage that, I did the following :

ResultSet rs2 = state2.executeQuery(produitsQuery);
while (rs2.next()) {
   for (int i = 0; i < produits.size(); i++) {
       obj = ((Object[]) produits.get(i));
       idpdt = (Integer) obj[1];

       if (idpdt == rs2.getInt(1)) {
           nb = (Integer) obj[3];
           valo = (Double) obj[4];

           nb += rs2.getDouble(4);
           valo += rs2.getDouble(5);
           produits.set(i, new Object[]{
               rs2.getString("famille"),
               rs2.getInt("id_pdt"),
               rs2.getString("nom_pdt"),
               nb,
               valo,
               s2.getString("sous_famille")});
           k = 1;
       }
    }
    if (k == 0) 
        produits.add(new Object[]{
            rs2.getString("famille"),
            rs2.getInt("id_pdt"),
            rs2.getString("nom_pdt"),
            rs2.getInt("nb"),
            rs2.getDouble("valo"),
            rs2.getString("sous_famille")});

}

Results are perfect but the execution time is very very slow and that's a problem. I think it's because I loop the entire arraylist at every row of the resultset.

What others solutions might I use to make the execution time faster?

2
  • Use a map with the key being the field you're comparing. Commented Oct 18, 2012 at 14:03
  • ` I think it's because i loop the entire arraylist at every row of the resultset.` Yup, you are right. Commented Oct 18, 2012 at 14:05

3 Answers 3

2

Why not put your first result set into a HashMap, keyed on idpdt (since that's what you're indexing by) ? A HashMap will provide O(1) lookups, rather than O(n).

Your code would look like:

while (r2.next()) {
   Object[] result = map.get(rs2.getInt(1));
   if (result != null) {
      ...
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Put the data in a set instead, keyed off the product (or product id). Then, your look-up from one product to the other would be o(1), basically costing only one cycle. If the data needs to be sorted, you could sort the resulting merged set at the end of the run by putting the set into a List and using the Collections.sort() on it. I think that would greatly improve the merging of the two result sets.

Comments

0

If you know for sure that each product will be in both databases, then you can add an ORDER BY product_id in your query and then loop through both resultsets at the same time :

while (rs1.next() &&rs2.next()) {

// your code here. No need to loop again.
// .............. 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.