Search code examples
javamysqldatabaseperformancepersistence

Is using query.getResultList(); method affect badly on performance?


I'm using Java persistence API to develop a standalone software. I need to get single results as well as multiple results from database. I have several questions also,

First one is,

I'm using query.getResultList(); method for retrieving both single result and multiple results. I want to know weather this method retrieve the entire result set or some part of the result set (like when executing a limit query, eg: *SELECT * FROM some_db.some_table LIMIT 100 OFFSET 120;*). If not, does it affect badly on performance? (In this case I'm thinking about a database which contain around 10000 records in some tables)

Second one,

What the impact between using query.getResultList(); and query.getSingleResult();


Solution

  • You should separate DB-side and server-side parts.

    1. If you get a 100 rows from DB-side to server-side, it does not matter for latter server-side execution how huge you table is.

    2. So, you are trying to understand an overhead of query.getResultList() using with just one row I think. IMHO, you should not bother about that.

    3. Based on docs it seems that you have small difference between query.getResultList() and query.getSingleResult() when you are getting 0 rows as a result.

    query.getSingleResult()

    ... if there are no results at all a NoResultException is thrown.

    UPD

    For Persistance API try to use this way to create a query:

    getEntityManager().createQuery(query) .setFirstResult(offset) // offset .setMaxResults(limit) // limit .getResultList();