| # 22. Native Query Optimization Native Queries will run out of the box in any environment. If optimization is turned on, Native Queries will be converted to SODA queries whenever possible, allowing db4o to use indexes and optimized internal comparison algorithms. If optimization is turned off or not possible for some reason, a Native Query will be executed by instantiating all objects, using SODA Evaluations. Naturally performance will not be as good in this case. The Native Query optimizer is still under development to eventually "understand" all valid C# constructs. Current optimization supports the following constructs well: - compile-time constants - simple member access - primitive comparisons - equality operator - #Contains()/#StartsWith()/#EndsWith() for Strings - boolean expressions - arbitrary method calls (including property accessors) on predicate fields (without any arguments) - candidate methods composed of the above - chained combinations of the above This list will constantly grow with the latest versions of db4o. Note that the current implementation doesn't support polymorphism yet. ## 22.1. Enabling Native Query optimization on the CompactFramework 2.0 Due to some platform limitations, CompactFramework 2.0 users using the more convenient delegate based Native Query syntax that want their queries to be optimized are required to run the Db4oAdmin.exe command line utility on their assemblies prior to deploying them. The utility which can be found in the /bin folder of this distribution is required because the CompactFramework API does not expose any of the delegate metadata needed by the Native Query optimizer. The tool works by augmenting the bytecode with the necessary delegate metadata and replacing ObjectContainer#Query invocations with invocations to a lower level method that makes use of the additional information. The tool can be easily integrated inside Visual Studio.NET 2005 as a Post Build tool by following the simple steps below: - Right click the project you want to enable Native Query optimization for - Select'Properties' - In the Properties Page select the'Build Events'tab - In the'Post-build event command line'text box insert the following text "/bin/Db4oAdmin.exe -cf2-delegates $(TargetPath)" without the quotes and replacing to the correct value for your system. A complete example can be found in the /src/instrumentation/Db4oAdmin.Example directory of this distribution. ## 22.2. Build Time Optimization for Native Queries Db4oAdmin.exe can also be used to pre optimize the Native Queries in a given assembly. This makes it possible to deploy an application without Db4oTools.dll (the assembly where the Native Query runtime optimizer lives) while also possibly reducing Native Query execution time by dropping runtime analysis completely. Execute the Db4oAdmin.exe command line utility without any arguments and check out the help information. IMPORTANT: the tool is still in constant development and it currently does not support pre optimization of Native Queries expressed as delegates. ## 22.3. Monitoring optimization This feature still is quite basic but it will soon be improved. Currently you can only attach event handlers to the ObjectContainer:
`NativeQueryHandler handler = ((YapStream)db).GetNativeQueryHandler(); NativeQueryHandler handler = ((YapStream)container).GetNativeQueryHandler(); handler.QueryExecution += OnQueryExecution; handler.QueryOptimizationFailure += OnQueryOptimizationFailure;`
-- generated by Doctor courtesy of db4objects Inc.