Your browser may have trouble rendering this page. See supported browsers for more information.

|<<>>|153 of 274 Show listMobile Mode

Optimizing data access for high-latency networks: part III

Published by marco on

 In the previous article, we partially addressed a performance problem in the calendar of Encodo’s time-tracking product, Punchclock. While we managed to drastically reduce the amount of time taken by each query (>95% time saved), we were still executing more queries than strictly necessary.

The query that we’re trying to optimized further is shown below.

var people =
  Session.GetList<Person>().
  Where(p => Session.GetCount(p.TimeEntries.Query) > 0).
  ToList();

This query executes one query to get all the people and then one query per person to get the number of time entries per person. Each of these queries by itself is very fast. High latency will cause them to be slow. In order to optimize further, there’s really nothing for it but to reduce the number of queries being executed.

Let’s think back to what we’re actually trying to accomplish: We want to get all people who have at least one time entry. Can’t we get the database to do that for us? Some join or existence check or something? How about the code below?

var people = 
  Session.GetList<Person>(
    Session.CreateQuery<Person>().
    Join(Person.MetaRelations.TimeEntries, JoinType.WhereExists).
    Query
  );

What’s happening in the code above? We’re still getting a list of people but, instead of manipulating the related TimeEntries for each person locally, we’re joining the TimeEntries relation with the Quino query Join() method and changing the join type from the default All to the restrictive WhereExists. This sounds like exactly what we want to happen! There is no local evaluation or manipulation with Linq and, with luck, Quino will be able to map this to a single query on the database.

This is the best possible query: it’s purely declarative and will be executed as efficiently as the back-end knows how.

There’s just one problem: the WhereExists join type is broken in Quino 1.11.

Never fear, though! We can still get it to work, but we’ll have to do a bit of work until the bug is fixed in Quino 1.12. The code below builds on lessons learned in the earlier article, Mixing your own SQL into Quino queries: part 2 of 2 to use custom query text to create the restriction instead of letting Quino do it.

var accessToolkit = new PostgreSqlMetaDatabase().AccessToolkit;

var query = Session.CreateQuery<Person>();
query.CustomCommandText = new CustomCommandText();
query.CustomCommandText.SetSection(
  CommandTextSections.Where, 
  CommandTextAction.Replace,
  string.Format(
    "EXISTS (SELECT id FROM {0} WHERE {1} = {2})", 
    accessToolkit.GetName(TimeEntry.Metadata), 
    accessToolkit.GetField(TimeEntry.MetaProperties.PersonId), 
    accessToolkit.GetField(Person.MetaProperties.Id)
  )
);
var people = Session.GetList<Person>(query);

A look at the statistics is very encouraging:

 One query for people; one for time-entries

We’re down to one 29ms query for the people and an even quicker query for all the relevant time entries.[1] We can see our query text appears embedded in the SQL generated by Quino, just as we expected.

There are a few other security-related queries that execute very quickly and hardly need optimization.

We’ve come much farther in this article and we’re almost done. In the next article, we’ll quickly clean up a few other queries that are showing up in the statistics and that have been nagging us since the beginning.


[1] The time-entry query is not representative because my testing data set didn’t include time entries for the current day and I was too lazy to page around to older data.