Exforsys

Home arrow Reviews arrow Oracle Coherence

Oracle Coherence: Paging Over Query Results

Author: Packt Publishing     Published on: 4th Jul 2010

The LimitFilter is somewhat special and deserves a separate discussion. Unlike other filters, which are used to compose query criteria, the LimitFilter is used to control how many result items are returned at a time. Basically, it allows you to page through query results n items at a time.

Ads

This also implies that unlike other filters, which are constructed, executed, and discarded, an instance of a LimitFilter is something you might need to hold on to for an extended period of time, as it is a mutable object that keeps track of the current page number, top and bottom anchor objects, and other state that is necessary to support paging.

Let's look at a simple example to better demonstrate the proper usage of a LimitFilter:

Sample Code
  1.     NamedCache countries = CacheFactory.getCache("countries")
  2.  
  3.     LimitFilter filter = new LimitFilter(
  4.                               new LikeFilter("getName", "B%"), 5)
  5.  
  6.     Set<map.entry></map.entry> entries = countries.entrySet(filter, null)
  7.     // contains countries 1-5 whose name starts with a letter 'B'
  8.  
  9.     filter.nextPage()
  10.     entries = countries.entrySet(filter, null)
  11.     // contains countries 6-10 whose name starts with a letter 'B'
  12.  
  13.  
  14.    filter.setPage(4)
  15.    entries = countries.entrySet(filter, null)
  16.    // contains countries 21-25 whose name starts with 'B'
Copyright exforsys.com


As you can see, you can page through the result by executing the same query over and over again and modifying the current page of the LimitFilter between the query executions by calling the nextPage, previousPage, or setPage method.

The LimitFilter is extremely powerful as it allows you to execute the main query only once and then obtain the results in chunks of the size you specify. It maps very nicely to a common requirement for results paging within a web application, allowing you to bring the web server only the data it needs to generate the current page, thus reducing network traffic and improving application performance and scalability. You can safely store an instance of a LimitFilter within a user's HTTP session and reuse it later when the user navigates to another page of the results.

One thing to note in the preceding example is that we are using the entrySet method to retrieve the results, contrary to what we have discussed in the previous section. The reason for that is that we want to return countries sorted by name (natural order), and as I mentioned earlier, if we need to support paging over the sorted results we have no other option but to sort them within the cluster using an overload of the entrySet method that accepts a comparator.

However, this is really not an issue, as the amount of data sent over the wire will be naturally limited by the LimitFilter itself and will typically be very small, so we don't need to optimize the query for the near caching scenario.

Using indexes to improve query performance

Just as you can use indexes to improve query performance against a relational database, you can use them to improve the performance of a Coherence query. That is not to say that Coherence indexes are the same as database indexes--in fact, they are very different, and we'll discuss how indexes are implemented in Coherence shortly.

Ads

However, they are similar in the way they work, as they allow query processor to optimize queries by:

  1. Limiting the number of entries that have to be evaluated by the filter
  2. Avoiding the need for object deserialization by providing the necessary information within the index itself

Both of these features are very important and can have a significant impact on query performance. For that reason, it is recommended that you always create indexes for the attributes that you query on.



 
This tutorial is part of a Oracle Coherence tutorial series. Read it from the beginning and learn yourself.

Oracle Coherence

 

Comments