0

I have a table as below,

process_name |  startTime             | endTime               | parent_id
-------------------------------------------------------------------------
chrome       |  2019-03-06 00:48:27   | 2019-03-06 00:48:58   | 111
chrome       |  2019-03-07 00:48:27   | 2019-03-07 00:48:58   | 112
firefox      |  2019-03-08 00:48:27   | 2019-03-08 00:48:58   | 113
IE           |  2019-03-09 00:48:27   | 2019-03-09 00:48:58   | 114
firefox      |  2019-03-10 00:48:27   | 2019-03-10 00:48:58   | 115
chrome       |  2019-03-11 00:48:27   | 2019-03-11 00:48:58   | 116

Some points from the table,

  1. Same process_name can be found under different parent_ids.

  2. I already know the process_name and have an ArrayList of parent_ids where it exists.

For a given process_name, I need to find startTime and endTime for all of the parent_ids present in my ArrayList.

All I can think of is using a for loop and execute the below query.

select startTime, endTime from myTable where parent_id = ? and process_name=?

Can I avoid executing this query in a loop and follow some other better approach?

I use Derby at this moment but the DB may get changed to Oracle.

2
  • Is parent_id stored in a table? If so you can perform a JOIN. Commented Mar 21, 2019 at 8:00
  • why you need loop? you can just filter by process_name Commented Mar 21, 2019 at 8:17

4 Answers 4

2

There are multiple approaches to solve this problem :

  1. As Svend Suggested, Since you already have process_name, You can get all the start and end times for that process name.

  2. Another approach is to prepare a string of parent_ids first, and then query it to the database using a single query:

Code:

String s = "(";
for(int i = 0; i < list.length; i++)
{
    if(i<list.length-1) s+=list[i] + ",";
    else s+= ""+ list[i] + ")";
}

String query = " select startTime, endTime from myTable where parent_id IN " + s + " and process_name=?";
Sign up to request clarification or add additional context in comments.

3 Comments

op wants to avoid loop
@ZaynulAbadinTuhin, OP doesn't meant to say that he doesn't want to do a loop at all, he just doesn't want to do a loop of SQL queries
I guess this would be better using Christian's ? formatting, as it would be less injection attack prone... though that shouldn't matter too much in this specific case, as we're probably in control of what's in list...
0

It is not good practice to execute a query in a loop. Instead, why don't you fetch all the records belonging to a given process at once. Maybe using GROUPBY clause for the process_name along with aggregate functions on other columns [if you need to perform anything upon those other columns and avoid doing via code by yourself] Once you have all the records in hand, then the startTime and endTime can be used for your further processing.

Hope that helps.

Comments

0

Wouldn't it be possible to just do:

select parent_id, startTime, endTime from myTable where process_name=?

And get the values for all parent_id values, and then filter those on your ArrayList afterwards?

Comments

0

As a slight variation of the solution of @prakhar-londhe you might also use parameters in the in clause as in

String query = " select startTime, endTime from myTable where parent_id IN (?, ?, ?) and process_name = ?";

and then add a parameter for each parent_id. Of course you'd still have to assemble the query (to put a ? inside the parentheses for each parent_id) as well as the parameter list in a loop.

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.