Good Practices in Java - Performance and Iterations

in java •  7 years ago 

Good morning,

This time I have decided to talk about the impact of calls to JPA within iterations in the performance of a REST API

Particularly the calls through JPA to the database are common in Java, but a common error that sometimes we can not avoid committing is to repeat a call within an iteration, although this allows us to manipulate a lot of data quickly , the "fast" is really a lie, a call within a cycle can increase the response time in more than 60 seconds

To solve this it is advisable to use methods that can manipulate lists, arrays or collections of elements instead of using unitary elements, this can apply in many cases and the advantage is that a method that is used for many, can also be used for one.

DQmc4ErieKWm9oZShVbW9hqdbkZRV1D96gSxcwmTjBxvjCT_1680x8400.png

Even though the difference does not seem much, it is much faster for JPA to execute:

SELECT bar FROM foo bar WHERE bar.id IN (1, 2, 3, 4, 5, 6, 7, 8, 9)

to execute:

SELECT bar FROM foo bar WHERE bar.id = 1;
SELECT bar FROM foo bar WHERE bar.id = 2;
SELECT bar FROM foo bar WHERE bar.id = 3;
SELECT bar FROM foo bar WHERE bar.id = 4;
SELECT bar FROM foo bar WHERE bar.id = 5;

My work as a backend developer for REST API's, has taken me multiple times through this practice, even though it seems that it is difficult to decrease the response time of a method, a great% of the time it is because of this.

For the moment I say goodbye but next time I'll be dealing with the subject of http methods and how is the ideal use of these with respect to the action to be taken and the data to be sent.

Drey

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!