You are here: Basics Operations & Concepts > Querying > Native Queries > Native Query Optimization

Native Query Optimization

Native queries will run out of the box in any environment. This optimization is turned on by default. Native queries will be converted to SODA where this is possible. This allows db4o to use indexes and optimized internal comparison algorithms. Otherwise native query may be executed by instantiating all objects, using SODA evaluations. Naturally performance will not be as good in this case.

Optimization Theory

For Native Query the bytecode is analyzed to create an AST-like expression tree. Then the flow graph of the expression tree is analyzed and converted to a SODA query graph.

For example:

IList<Pilot> result = container.Query(
    delegate(Pilot pilot) { return pilot.Name == "John"; });
NativeQueryExamples.cs: Check for equality of the name
Dim result As IList(Of Pilot) = container.Query(Of Pilot)(AddressOf QueryJohns)
NativeQueryExamples.vb: Check for equality of the name
Private Shared Function QueryJohns(ByVal pilot As Pilot)
    Return pilot.Name = "John"
End Function
NativeQueryExamples.vb: Query for John

First the signature of the given delegate is analyzed to find out the types. This is used to constrain the type in the SODA-query. Then the bytecode of query is analyzed to find out was it does. When the operations a simple and easy to convert, it will be transformed to complete SODA query:

IQuery query = container.Query();
query.Constrain(typeof (Pilot));
query.Descend("name").Constrain("John");

IObjectSet result = query.Execute();
SodaQueryExamples.cs: A simple constrain on a field
Dim query As IQuery = container.Query()
query.Constrain(GetType(Pilot))
query.Descend("name").Constrain("John")

Dim result As IObjectSet = query.Execute()
SodaQueryExamples.vb: A simple constrain on a field

Native Query Optimization

Native Query optimizer for .NET requires the following libraries to work: See "Dependency Overview"

These assemblies must be available in your project for successful native query optimization.

Current optimization supports the following constructs well:

Note that the current implementation doesn't support polymorphism yet.

The specifics of Compact Framework platform are explained in NQ Optimization On CF2.0.

An alternative optimization practice can be found in Build-time Optimization For .NET article.

For more information on native queries optimization see Monitoring Optimization.