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
System.out.println("Testing query performance on 10000 pilot objects in Lazy mode"); 03
fillUpDB(10000); 04
ObjectContainer db = Db4o.openFile(YAPFILENAME); 05
try { 06
db.ext().configure().queries().evaluationMode(QueryEvaluationMode.LAZY); 07
QueryStats stats = new QueryStats(); 08
stats.connect(db); 09
Query query = db.query(); 10
query.constrain(Pilot.class); 11
query.descend("points").constrain(99).greater(); 12
query.execute(); 13
long executionTime = stats.executionTime(); 14
System.out.println("Query execution time: " + executionTime); 15
} finally { 16
db.close(); 17
} 18
}
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
System.out.println("Testing lazy mode with concurrent modifications"); 03
fillUpDB(10); 04
ObjectContainer db = Db4o.openFile(YAPFILENAME); 05
try { 06
db.ext().configure().queries().evaluationMode(QueryEvaluationMode.LAZY); 07
Query query1 = db.query(); 08
query1.constrain(Pilot.class); 09
query1.descend("points").constrain(5).smaller(); 10
ObjectSet result1 = query1.execute(); 11
12
Query query2 = db.query(); 13
query2.constrain(Pilot.class); 14
query2.descend("points").constrain(1); 15
ObjectSet result2 = query2.execute(); 16
Pilot pilotToDelete = (Pilot)result2.get(0); 17
System.out.println("Pilot to be deleted: " + pilotToDelete); 18
db.delete(pilotToDelete); 19
Pilot pilot = new Pilot("Tester",2); 20
System.out.println("Pilot to be added: " + pilot); 21
db.set(pilot); 22
23
System.out.println("Query result after changing from the same transaction"); 24
listResult(result1); 25
} finally { 26
db.close(); 27
} 28
}
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.