Search code examples
performancespring-data-jpajpqlquerydsl

Why does query optimization affect performance?


I am a student who is learning spring and jpa recently. While developing 'get api' with conditions, I came to think about which method is advantageous in terms of performance.

When it is necessary to query data based on conditions, jpql or querydsl are usually used to generate dynamic queries. Can you tell me why generating a dynamic query like this and looking up only the necessary data is better than using the java stream filter() function after looking up the entire data?

Also, can you tell me why generating fewer queries is advantageous in terms of performance?

I know that generating fewer queries has a performance advantage, but I lack an understanding of why I say it has a performance advantage.


Solution

  • Can you tell me why generating a dynamic query like this and looking up only the necessary data is better than using the java stream filter() function after looking up the entire data?

    In general addressing the database or any other external storage is much more expensive than most of operations on Java side because of networking latency. If you query all the data and use e.g. list.stream().filter() than the significant amount of data is transferred over the network. And if one vice versa queries only some data filtered on the DB side the transferred amount in lower.

    Pay attention, that while this is true in general there might be a cases when filtering on Java side could be more effective. This is highly dependent on several things:

    • query complexity
    • amount of data
    • database structure (schema, indices, column types etc.)

    As of number of queries here we have the same considerations: query execution costs, data transfer costs, so the less queries you have - the better. And again, this is not an axiom: in some cases having multiple lightweight queries with grouping/filtering on Java side might be faster, than one huge and complicated SQL-query.