<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="2" MadCap:lastHeight="120" MadCap:lastWidth="624" MadCap:disableMasterStylesheet="true" MadCap:tocPath="Basics Operations & Concepts|Querying|Native Queries" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem"> <head><title>Native Query Optimization</title> <script type="text/javascript">/* <![CDATA[ */ window.onload = function(){ var pathToFlash = $('html').attr('MadCap:PathToHelpSystem') + 'Content/Resources/Code/ZeroClipboard.swf'; ZeroClipboard.setMoviePath(pathToFlash); function bindToClipBord(element,content){ var clip = new ZeroClipboard.Client(); clip.setText(content); clip.glue(element); }; if(location.protocol==='file:'){ $('.copylink-marker').remove(); } else{ $('.copylink-marker').each(function(){ var text = $(this).parent().parent().children('.prettyprint').html(); $(this).hover(function(){ bindToClipBord(this,text); }, function(){}); }); } prettyPrint(); }; /* ]]> */</script> <link href="../../../SkinSupport/MadCap.css" rel="stylesheet" /> <link href="../../../Resources/Stylesheets/OnlineStyle.css" rel="stylesheet" /> <script src="../../../Resources/Code/prettify.js"> </script> <script src="../../../Resources/Code/lang-vb.js"> </script> <script src="../../../Resources/Code/jquery.min.js"> </script> <script src="../../../Resources/Code/ZeroClipboard.js"> </script> <script src="../../../SkinSupport/MadCapAll.js" type="text/javascript"> </script> </head> <body> <p class="MCWebHelpFramesetLink" style="display: none;"><a href="../../../../index_CSH.html#basics/querying/native_queries/native_query_optimization.htm" style="">Open topic with navigation</a> </p> <div class="MCBreadcrumbsBox"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="../../../basics.htm">Basics Operations & Concepts</a><span class="MCBreadcrumbsDivider"> > </span><a class="MCBreadcrumbsLink" href="../../querying.htm">Querying</a><span class="MCBreadcrumbsDivider"> > </span><a class="MCBreadcrumbsLink" href="../native_queries.htm">Native Queries</a><span class="MCBreadcrumbsDivider"> > </span><span class="MCBreadcrumbs">Native Query Optimization</span> </div> <p> <script type="text/javascript">/*<![CDATA[*/document.write('<a href="' + location.href +'">'); document.write("Direct Link"); document.write('</a>');/*]]>*/</script> </p> <p> </p> <h1><a name="kanchor13"></a>Native Query Optimization</h1> <p>Native queries will run out of the box in any environment. This optimization is turned on by default. Native queries will be converted to <a href="../soda_query.htm">SODA</a> 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 <a href="../soda/soda_evaluations.htm">SODA evaluations</a>. Naturally performance will not be as good in this case.</p> <H2>Optimization Theory </H2> <p>For Native Query the <span class="PrimaryByteCode">bytecode</span> 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.</p> <p>For example:</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">IList<Pilot> result = container.Query( delegate(Pilot pilot) { return pilot.Name == "John"; });</pre> <div class="codesnippet-meta">NativeQueryExamples.cs: Check for equality of the name <div class="codedownload"><a href="../../../CodeExamples/query/nq/Example-query-nq-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim result As IList(Of Pilot) = container.Query(Of Pilot)(AddressOf QueryJohns)</pre> <div class="codesnippet-meta">NativeQueryExamples.vb: Check for equality of the name <div class="codedownload"><a href="../../../CodeExamples/query/nq/Example-query-nq-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Private Shared Function QueryJohns(ByVal pilot As Pilot) Return pilot.Name = "John" End Function</pre> <div class="codesnippet-meta">NativeQueryExamples.vb: Query for John <div class="codedownload"><a href="../../../CodeExamples/query/nq/Example-query-nq-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <p>First the signature of the given <span MadCap:conditions="Primary..NET">delegate</span> is analyzed to find out the types. This is used to constrain the type in the SODA-query. Then the <span class="PrimaryByteCode">bytecode</span> 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:</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">IQuery query = container.Query(); query.Constrain(typeof (Pilot)); query.Descend("name").Constrain("John"); IObjectSet result = query.Execute();</pre> <div class="codesnippet-meta">SodaQueryExamples.cs: A simple constrain on a field <div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim query As IQuery = container.Query() query.Constrain(GetType(Pilot)) query.Descend("name").Constrain("John") Dim result As IObjectSet = query.Execute()</pre> <div class="codesnippet-meta">SodaQueryExamples.vb: A simple constrain on a field <div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <p MadCap:conditions="Primary..NET"> <H2>Native Query Optimization</H2> <p>Native Query optimizer for .NET requires the following libraries to work: <a href="../../../dependency_overview.htm" target="" title="" alt="" class="MCXref" xrefformat="See "{paratext}"">See "Dependency Overview"</a></p> <ul> <LI value="1">Db4objects.Db4o.Instrumentation.dll</LI> <LI value="2">Db4objects.Db4o.NativeQueries.dll </LI> <LI value="3">Mono.Cecil.dll </LI> <LI value="4">Cecil.FlowAnalysis.dll </LI> <LI value="5"> </LI> </ul> <p>These assemblies must be available in your project for successful native query optimization. </p> <p>Current optimization supports the following constructs well:</p> <UL> <LI value="1">Compile-time constants.</LI> <LI value="2">Simple member access.</LI> <LI value="3">Primitive comparisons.</LI> <LI value="4">Equality operator.</LI> <LI value="5">Contains, StartsWith- and EndsWith-methods for strings.</LI> <LI value="6">Boolean expressions.</LI> <LI value="7">Arbitrary method calls,including property accessors, on predicate fields (without any arguments).</LI> <LI value="8">Candidate methods composed of the above.</LI> <LI value="9">Chained combinations of the above.</LI> </UL> <p>Note that the current implementation doesn't support polymorphism yet.</p> <p>The specifics of Compact Framework platform are explained in <a href="native_query_optimization/nq_optimization_on_cf2.0.htm">NQ Optimization On CF2.0</a>. </p> <p>An alternative optimization practice can be found in <a href="native_query_optimization/nq_build_time_optimisation.htm">Build-time Optimization For .NET</a> article.</p> </p> <p>For more information on native queries optimization see <a href="native_query_optimization/monitoring_optimization.htm">Monitoring Optimization</a>.</p> <script type="text/javascript" src="../../../SkinSupport/MadCapBodyEnd.js"> </script> </body> </html>