In Lazy Querying mode objects are not evaluated at all,
instead of this an iterator is created against the best index found. Further query processing (including all index
processing) will happen when the user application iterates through the
resulting ObjectSet
. This
allows you to get the first query results almost immediately.
01public static void TestLazyQueries() 02
{ 03
Console.WriteLine("Testing query performance on 10000 pilot objects in Lazy mode"); 04
FillUpDB(10000); 05
IObjectContainer db = Db4oFactory.OpenFile(YapFileName); 06
try 07
{ 08
db.Ext().Configure().Queries().EvaluationMode(QueryEvaluationMode.LAZY); 09
//QueryStats stats = new QueryStats(); 10
//stats.connect(db); 11
IQuery query = db.Query(); 12
query.Constrain(typeof(Pilot)); 13
query.Descend("_points").Constrain(99).Greater(); 14
DateTime dt1 = DateTime.UtcNow; 15
query.Execute(); 16
DateTime dt2 = DateTime.UtcNow; 17
TimeSpan diff = dt2 - dt1; 18
Console.WriteLine("Query execution time=" + diff.Milliseconds + " ms"); 19
} 20
finally 21
{ 22
db.Close(); 23
} 24
}
01Public Shared Sub TestLazyQueries() 02
Console.WriteLine("Testing query performance on 10000 pilot objects in Lazy mode") 03
FillUpDB(10000) 04
Dim db As IObjectContainer = Db4oFactory.OpenFile(YapFileName) 05
Try 06
db.Ext.Configure.Queries.EvaluationMode(QueryEvaluationMode.LAZY) 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
In addition to the very fast execution this method also ensures very small memory consumption, as lazy queries do not need an intermediate representation as a set of IDs in memory. With this approach a lazy query ObjectSet does not have to cache a single object or ID. The memory consumption for a query is practically zero, no matter how large the resultset is going to be.
There are some interesting effects appearing due to the fact that the objects are getting evaluated only on a request. It means that all the committed modifications from the other transactions and uncommitted modifications from the same transaction will be taken into account when delivering the result objects:
01public static void TestLazyConcurrent() 02
{ 03
Console.WriteLine("Testing lazy mode with concurrent modifications"); 04
FillUpDB(10); 05
IObjectContainer db = Db4oFactory.OpenFile(YapFileName); 06
try 07
{ 08
db.Ext().Configure().Queries().EvaluationMode(QueryEvaluationMode.LAZY); 09
IQuery query1 = db.Query(); 10
query1.Constrain(typeof(Pilot)); 11
query1.Descend("_points").Constrain(5).Smaller(); 12
IObjectSet result1 = query1.Execute(); 13
14
IQuery query2 = db.Query(); 15
query2.Constrain(typeof(Pilot)); 16
query2.Descend("_points").Constrain(1); 17
IObjectSet result2 = query2.Execute(); 18
Pilot pilotToDelete = (Pilot)result2[0]; 19
Console.WriteLine("Pilot to be deleted: " + pilotToDelete); 20
db.Delete(pilotToDelete); 21
Pilot pilot = new Pilot("Tester", 2); 22
Console.WriteLine("Pilot to be added: " + pilot); 23
db.Set(pilot); 24
25
Console.WriteLine("Query result after changing from the same transaction"); 26
ListResult(result1); 27
} 28
finally 29
{ 30
db.Close(); 31
} 32
}
01Public Shared Sub TestLazyConcurrent() 02
Console.WriteLine("Testing lazy mode with concurrent modifications") 03
FillUpDB(10) 04
Dim db As IObjectContainer = Db4oFactory.OpenFile(YapFileName) 05
Try 06
db.Ext.Configure.Queries.EvaluationMode(QueryEvaluationMode.LAZY) 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(1) 14
Dim result2 As IObjectSet = query2.Execute 15
Dim pilotToDelete As Pilot = CType(result2(0), Pilot) 16
Console.WriteLine("Pilot to be deleted: " + pilotToDelete.ToString()) 17
db.Delete(pilotToDelete) 18
Dim pilot As Pilot = New Pilot("Tester", 2) 19
Console.WriteLine("Pilot to be added: " + pilot.ToString()) 20
db.Set(pilot) 21
Console.WriteLine("Query result after changing from the same transaction") 22
ListResult(result1) 23
Finally 24
db.Close() 25
End Try 26
End Sub
Pros:
Query.execute()
will return
very fast. First results can be made available to the application before the
query is fully processed.Cons:
ObjectSet
. In doing so the query processor takes
changes into account that may have happened since the Query.execute() call:
committed changes from other transactions, and uncommitted changes from the
calling transaction. There is a wide range of possible side effects:ObjectSet
and changes each object in a way that it is
placed at the end of the index, the same objects can be revisited over and
over. ObjectSet
first and store all the objects to a temporary collection representation before changing objects and storing them back
to db4o.
ObjectSet
will require the query processor to create a
snapshot or to evaluate the query fully. An example of such a call is ObjectSet.size()
.Lazy mode can be an excellent choice for single transaction read use, to keep memory consumption as low as possible.