tags:

views:

36

answers:

2

Any performance issues in the code below:

Goal: I want retrieve data from all the rows for a particular id in the list and then call a function

a = list of 2000 ids:

for (int i = 0; i < a.size; i++) {
    // Execute query "SELECT * FROM SOME_TABLE WHERE ID = I";
    // Use the data and call a function
}

Any other solution? Is it possible to have just one query with group by id and whenever group changes call a function.

Thanks

+2  A: 

You could use an ORDER BY clause to order the results by id and then as you said, simply keep track of the current id and call the function every time the next id doesn't match the current (and if you get to the end of the result set).

Amber
This will perform much better than selecting the groups one by one if there are only a few entries per group, because there are less database round-trips. If there are hundreds of rows per group it will have roughly the same performance as your Java loop (but not worse).
Thilo
I believe if your function takes time to process the data retrieved then its ok to execute multiple queries?
Vishal
Reducing network i/o will improve performance. If that improvement is dwarfed by what the function does to the extent that it is no longer noticeable, is another question.
Thilo
This approach also works when you do not know (or cannot easily enumerate) all the possible values for the group id.
Thilo
A: 

If you can express the function / grouping in SQL, that will be probably be faster. But that depends on what the function does.

It could look like

  select id, max(amount), sum(amount) from some_table group by id
Thilo
Thanks, but I wan to use data from all the rows for particular id.
Vishal
The max and sum functions aggregate across all the rows for a particular id. But maybe you are looking at something similar to Dav's answer. Select all the rows ordered by group id, buffer each batch and when the group changes pass the buffer to your function.
Thilo