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

Monitoring Optimization

In order to optimize native queries the bytecode is analyzed and converted into SODA queries. This task isn't easy. If there's any doubt in the correctness of the conversion db4o won't do it. In such cases db4o falls back and instantiates all objects and runs it against the query. This is a order of magnitude slower than optimized queries. Therefore you probably want to monitor the query optimization and be warned when a query isn't optimized. This is possible with the diagnostic listeners.

configuration.Common.Diagnostic.AddListener(new NativeQueryListener());
NativeQueryDiagnostics.cs: Use diagnostics to find unoptimized queries
configuration.Common.Diagnostic.AddListener(New NativeQueryListener())
NativeQueryDiagnostics.vb: Use diagnostics to find unoptimized queries
class NativeQueryListener :IDiagnosticListener
{
    public void OnDiagnostic(IDiagnostic diagnostic)
    {
        if(diagnostic is NativeQueryNotOptimized){
            Console.WriteLine("Query not optimized"+diagnostic);
        } else if(diagnostic is NativeQueryOptimizerNotLoaded){
            Console.WriteLine("Missing native query optimisation assemblies" + diagnostic);
        }
    }
}
NativeQueryDiagnostics.cs: The native query listener
Class NativeQueryListener
    Implements IDiagnosticListener
    Public Sub OnDiagnostic(ByVal diagnostic As IDiagnostic) Implements IDiagnosticListener.OnDiagnostic

        If TypeOf diagnostic Is NativeQueryNotOptimized Then
            Console.WriteLine("Query not optimized" & Convert.ToString(diagnostic))
        ElseIf TypeOf diagnostic Is NativeQueryOptimizerNotLoaded Then
            Console.WriteLine("Missing native query optimisation assemblies" & Convert.ToString(diagnostic))
        End If
    End Sub
End Class
NativeQueryDiagnostics.vb: The native query listener

You can register a diagnostic listener and check for certain messages. There are two messages related to the native query optimization. The first is the NativeQueryNotOptimized-message. This tells you that a query couldn't be optimized. Consider simplifying the query. The second is the NativeQueryOptimizerNotLoaded-message. This message tells you that db4o couldn't find the libraries needed for the native query optimization. Check that you've included the assemblies-files you need.