Telerik OpenAccess Classic

Telerik OpenAccess ORM Send comments on this topic.
Query Optimizations
Programmer's Guide > OpenAccess ORM Classic (Old API) > Programming With OpenAccess > Query a Data Model > Queries > Query Optimizations

Glossary Item Box

This documentation article is a legacy resource describing the functionality of the deprecated OpenAccess Classic only. The contemporary documentation of Telerik OpenAccess ORM is available here.

"Forwards Only" vs. Random Access Query Results

The IQuery.ForwardsOnly property determines how the query result can be accessed. Specifically, whether random access to query results is allowed or not. By default, the query result can be accessed "forwards only".

"Forwards Only" Behavior

If you are using OpenAccess ORM with ForwardsOnly = true (the default), you will see the following behavior. If you get an enumerator from the query result and get new instances by calling MoveNext() OpenAccess ORM keeps the result set open and rows are fetched in batches as needed (the number of rows fetched in each batch is configurable using the Prefetch property). This works well if you only want the first few results. The instances are only created when you ask for them. If you use the IList interface of the query result and call Get( index ), OpenAccess ORM will efficiently skip rows to arrive at the index. However, the index must be greater than or equal to the previously highest requested index.

If you call Count() on the query result OpenAccess ORM assumes you want all the results and will retrieve them in one call and close the result set. The results are stored for random access. You can then use the IList interface without any restrictions. For example, you can call the Get() method with an arbitrary index parameter. This also means that you can also create an ArrayList a = new ArrayList(q.execute()).

With a forward result set you can call Count() as the first action and everything will be resolved in one call or you can iterate, but it will not work if you start iterating and then call Count().

Random Access Behavior

If you are using OpenAccess ORM with ForwardsOnly = false (i.e., Random Access), you can use the IList interface without any restrictions. For example, you can call the Get() method with an arbitrary index parameter. You can also call Count() without resolving the entire list. OpenAccess ORM must use a scrollable result set to provide this functionality. This may use more database resources (cursors etc.) than a normal forwards only result set. This option is useful for paged results, i.e., you only want a few results from position n onwards.

Max Result Count

The IQuery.MaxResultCount property can be used to limit the number of instances to be returned by query execution.

This property can be used for performance optimizations, if not all instances matching the query are required. This method must be called before the query is compiled or executed as this setting changes the generated backend query.

Setting the Prefetch Size

The number of instances fetched per server round trip can be set. Use the IQuery.Prefetch property to minimize the number of client server calls.

IQuery.Prefetch(int) must be called before the query is compiled or executed.

This property controls the OpenAccess ORM batching and is also passed through to the database driver. If this property is not set, the value of MaxResultCount, if set, is used as the default.

Ignore Uncommitted

The IQuery.IgnoreUncommitted property determines whether the query execution is allowed to ignore the changes made in the current transaction, for e.g., return objects which have been deleted in the current transaction. By default IgnoreUncommitted is set to "True", i.e., uncommitted changes are ignored. However, if this is set to if it is set to "False", then the uncommitted changes become part of the query result.

IQuery.IgnoreUncommitted must be called before the query is compiled or executed, as this setting changes the generated backend query.

Parallel Fetch

The IQuery.ParallelFetch property controls whether parallel result fetching is allowed. Parallel fetching allows the use of multiple result sets in a combined way for an improved performance, at the possible expense of more data being fetched. If this property is set to "True", parallel fetching is allowed.

Getting the Root Type

The IQuery.RootType property gets the root type of the query result, when the query returns only instances of persistence-capable classes. The returned type is the common type of all returned persistence-capable instances (enhanced classes). If no common type exists (for example by use of a projection), null is returned.

Linq query optimizations

When a Linq query is used, the ForwardsOnly, IgnoreUncommitted, ParallelFetch and Debug bool values can be set by calling the corresponding method of the IQueryable instance returned by the IObjectContext.Extent<T>() method.

C#

Copy Code
var result = from c in Scope.Extent<Company>().ForwardsOnly(true).ParallelFetch(true).IgnoreUncommitted(true).Debug(false)
            where c.Name ==
"Telerik" select c;