Use comparison methods to filter queries

The relational database that supports PolicyCenter filters the results of a query much more efficiently than your own Gosu code, because it avoids loading unnecessary data into the application server. As a performance best practice, Guidewire recommends that you filter your queries with comparisons methods rather than filter the results with your own code.

The following sample Gosu code suffers a performance problem. It inadvertently loads most of the claims, along with their policies, from the relational database. Then, the code iterates the loaded claims and their policies and process only those few that match a specific policy. In other words, the code loads an excessive amount of data unnecessarily and takes an excessive amount of time to search for a few instances to process.

var targetPolicy : Policy
var claimQuery = Query.make(Claim)  // Performance suffers because the query loads all claims. 

 for (claim in claimQuery.select()) {                 
  if (claim.Policy == targetPolicy) {                 // Local Gosu code filters claims.      
    // Do something on claims of targeted policies. 
    ...
  }
}

The following modified sample Gosu code improves performance. It finds only relevant policies to process with the compare method:

var targetPolicy : Policy
var claimQuery = Query.make(Claim)  // Performance improves because the query loads few claims.

 claimQuery.compare("policy", Equals, targetPolicy)  // Query comparison method filters claims. 
 
for (claim.Policy in query.select()) {
  //  Do something on claims of targeted policies.
}