Performance differences between entity and row queries

The query builder API supports two types of queries:

  • Entity queries – Result objects contain a list of references to instances of the primary entity type for the query. You set up an entity query with the select method that takes no arguments. For example:
    query.select()
  • Row queries – Result objects contain a set of row-like structures with values fetched from or computed by the relational database. You set up a row query by passing a Gosu array of column selections to the select method. For example:
    query.select({
      QuerySelectColumns.path(Paths.make(Person#Subtype)),
      QuerySelectColumns.pathWithAlias("FName", Paths.make(Person#FirstName)),
      QuerySelectColumns.pathWithAlias("LName", Paths.make(Person#LastName))
    })

Some situations require entity queries. For example, in page configuration, you must use entity queries as the data sources for list views and detail panels. Other situations require row queries. For example, you must use row queries to produce the results of SQL aggregate queries or outer joins. In yet other cases, you can use either type of query. For example, you can use either entity or row queries in Gosu code used in batch processing types.

Use entity queries for code readability and maintenance

Generally, use an entity query unless performance is not adequate. The query builder code for entity queries is more readable and maintainable than for row queries, especially if you access the query builder APIs from Java rather than Gosu.

Use row queries to improve performance

Generally, use a row query instead of an entity query if the performance of the entity query is inadequate. Entity queries sometimes fetch unused data columns or execute additional queries in code that processes the results. Row queries can improve performance in these cases. Navigating object path expressions that span arrays and foreign keys to access the data that you need can cause additional, implicit database queries to fetch the data. Row queries fetch only the data that you need and typically avoid these additional queries.

Row queries can select fields

You can reduce the number of queries implicitly executed by row queries if you select specific fields rather than entity instances. If you specify fields rather than instances, the relational database fetches and computes values in response to the SQL query that the query builder expression submits to the database.

See also