Immediate Queries

This is the default query mode: the whole query result is evaluated upon query execution and object IDs list is produced as a result.

QueryModesExample.cs: TestImmediateQueries
01public static void TestImmediateQueries() 02 { 03 Console.WriteLine("Testing query performance on 10000 pilot objects in Immediate mode"); 04 FillUpDB(10000); 05 IObjectContainer db = Db4oFactory.OpenFile(YapFileName); 06 try 07 { 08 db.Ext().Configure().Queries().EvaluationMode(QueryEvaluationMode.IMMEDIATE); 09 IQuery query = db.Query(); 10 query.Constrain(typeof(Pilot)); 11 query.Descend("_points").Constrain(99).Greater(); 12 DateTime dt1 = DateTime.UtcNow; 13 query.Execute(); 14 DateTime dt2 = DateTime.UtcNow; 15 TimeSpan diff = dt2 - dt1; 16 Console.WriteLine("Query execution time=" + diff.Milliseconds + " ms"); 17 } 18 finally 19 { 20 db.Close(); 21 } 22 }

QueryModesExample.vb: TestImmediateQueries
01Public Shared Sub TestImmediateQueries() 02 Console.WriteLine("Testing query performance on 10000 pilot objects in Immediate mode") 03 FillUpDB(10000) 04 Dim db As IObjectContainer = Db4oFactory.OpenFile(YapFileName) 05 Try 06 db.Ext.Configure.Queries.EvaluationMode(QueryEvaluationMode.IMMEDIATE) 07 Dim query As IQuery = db.Query 08 query.Constrain(GetType(Pilot)) 09 query.Descend("_points").Constrain(99).Greater() 10 Dim dt1 As DateTime = DateTime.UtcNow 11 query.Execute() 12 Dim dt2 As DateTime = DateTime.UtcNow 13 Dim diff As TimeSpan = dt2 - dt1 14 Console.WriteLine("Query execution time=" + diff.Milliseconds.ToString() + " ms") 15 Finally 16 db.Close() 17 End Try 18 End Sub

Obviously object evaluation takes some time and in a case of big resultsets you will have to wait for a long time before the first result will be returned. This is especially unpleasant in a client-server setup, when query processing can block the server for seconds or even minutes.

This mode makes the whole objects result set available at once - ID list is built based on the committed state in the database. As soon as a result is delivered it won't be changed neither by changes in current transaction neither by committed changes from another transactions.

Note, that resultset contains only references to objects, you were querying for, which means that if an object field has changed by the time of the actual object retrieval from the object set - you will get the new field value:

QueryModesExample.cs: TestImmediateChanged
01public static void TestImmediateChanged() 02 { 03 Console.WriteLine("Testing immediate mode with field changes"); 04 FillUpDB(10); 05 IObjectContainer db = Db4oFactory.OpenFile(YapFileName); 06 try 07 { 08 db.Ext().Configure().Queries().EvaluationMode(QueryEvaluationMode.IMMEDIATE); 09 IQuery query1 = db.Query(); 10 query1.Constrain(typeof(Pilot)); 11 query1.Descend("_points").Constrain(5).Smaller(); 12 IObjectSet result1 = query1.Execute(); 13 14 // change field 15 IQuery query2 = db.Query(); 16 query2.Constrain(typeof(Pilot)); 17 query2.Descend("_points").Constrain(2); 18 IObjectSet result2 = query2.Execute(); 19 Pilot pilot2 = (Pilot)result2[0]; 20 pilot2.AddPoints(22); 21 db.Set(pilot2); 22 ListResult(result1); 23 } 24 finally 25 { 26 db.Close(); 27 } 28 }

QueryModesExample.vb: TestImmediateChanged
01Public Shared Sub TestImmediateChanged() 02 Console.WriteLine("Testing immediate mode with field changes") 03 FillUpDB(10) 04 Dim db As IObjectContainer = Db4oFactory.OpenFile(YapFileName) 05 Try 06 db.Ext.Configure.Queries.EvaluationMode(QueryEvaluationMode.IMMEDIATE) 07 Dim query1 As IQuery = db.Query 08 query1.Constrain(GetType(Pilot)) 09 query1.Descend("_points").Constrain(5).Smaller() 10 Dim result1 As IObjectSet = query1.Execute 11 Dim query2 As IQuery = db.Query 12 query2.Constrain(GetType(Pilot)) 13 query2.Descend("_points").Constrain(2) 14 Dim result2 As IObjectSet = query2.Execute 15 Dim pilot2 As Pilot = CType(result2(0), Pilot) 16 pilot2.AddPoints(22) 17 db.Set(pilot2) 18 ListResult(result1) 19 Finally 20 db.Close() 21 End Try 22 End Sub

Immediate Mode Pros And Cons

Pros:

  • If the query is intended to iterate through the entire resulting ObjectSet, this mode will be slightly faster than the others.
  • The query will process without intermediate side effects from changed objects (by the caller or by other transactions).

Cons:

  • Query processing can block the server for a long time.
  • In comparison to the other modes it will take longest until the first results are returned.
  • The ObjectSet will require a considerate amount of memory to hold the IDs of all found objects.