[odb-users] query results not being cached?

Boris Kolpackov boris at codesynthesis.com
Wed Jun 6 03:15:28 EDT 2012


Hi Craig,

Burton, Craig <crburton at tnsi.com> writes:

> I am not able to call "size()" on query results even if I try to use
> the "cache()" method on the results template.

For some databases (right now Oracle, SQLite, and MS SQL Server),
caching (and therefore size()) is not supported. This is documented
in the database-specific chapters. For Oracle that would be Section
16.5.2, "Query Result Caching":

http://www.codesynthesis.com/products/odb/doc/manual.xhtml#16.5.2

If you are interested, in the case or Oracle, supporting size() would
require switching the query result cursor to the scrollable mode. This
results in a much worse performance compared to the forward-only mode.
There is also a long and complicated list of limitations of scrollable
cursors that stipulate the kind of situations that are not supported
(specifically, support for BLOB/LONG types is very limited).

So it is better to try not to rely on knowing in advance the number
of entries in the result. Specifically, any optimizations that you
may want to make (e.g., reserve the space in the vector, etc), will
be inconsequential compared to the cost of supporting size(). If
you really need to know the size prior to the iteration, then the
best you can do is probably run a separate query that returns the
count, for example:

#pragma db view object(MyPersistentClass)
struct MyPersistentClassCount
{
  #pragma db column("count(*)")
  std::size_t count;
};

size_t count = ora_db->query<MyPersistentClassCount> (pred)->begin ()->count;

Boris



More information about the odb-users mailing list