Lazy Queries

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.

QueryModesExample.java: testLazyQueries
01private static void testLazyQueries() { 02 System.out 03 .println("Testing query performance on 10000 pilot objects in Lazy mode"); 04 fillUpDB(10000); 05 Configuration configuration = Db4o.newConfiguration(); 06 configuration.queries().evaluationMode(QueryEvaluationMode.LAZY); 07 ObjectContainer container = Db4o.openFile(configuration, DB4O_FILE_NAME); 08 try { 09 QueryStats stats = new QueryStats(); 10 stats.connect(container); 11 Query query = container.query(); 12 query.constrain(Pilot.class); 13 query.descend("points").constrain(99).greater(); 14 query.execute(); 15 long executionTime = stats.executionTime(); 16 System.out.println("Query execution time: " 17 + executionTime); 18 } finally { 19 container.close(); 20 } 21 }

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:

QueryModesExample.java: testLazyConcurrent
01private static void testLazyConcurrent() { 02 System.out 03 .println("Testing lazy mode with concurrent modifications"); 04 fillUpDB(10); 05 Configuration configuration = Db4o.newConfiguration(); 06 configuration.queries().evaluationMode(QueryEvaluationMode.LAZY); 07 ObjectContainer container = Db4o.openFile(configuration, DB4O_FILE_NAME); 08 try { 09 Query query1 = container.query(); 10 query1.constrain(Pilot.class); 11 query1.descend("points").constrain(5).smaller(); 12 ObjectSet result1 = query1.execute(); 13 14 Query query2 = container.query(); 15 query2.constrain(Pilot.class); 16 query2.descend("points").constrain(1); 17 ObjectSet result2 = query2.execute(); 18 Pilot pilotToDelete = (Pilot) result2.get(0); 19 System.out.println("Pilot to be deleted: " 20 + pilotToDelete); 21 container.delete(pilotToDelete); 22 Pilot pilot = new Pilot("Tester", 2); 23 System.out.println("Pilot to be added: " + pilot); 24 container.set(pilot); 25 26 System.out 27 .println("Query result after changing from the same transaction"); 28 listResult(result1); 29 } finally { 30 container.close(); 31 } 32 }

Pros and Cons for Lazy Queries

Pros:

Cons:

Lazy mode can be an excellent choice for single transaction read use, to keep memory consumption as low as possible.