[ZODB-Dev] Finding objects by attribute value?

Tim Peters tim at zope.com
Tue Jan 4 16:54:25 EST 2005


[Leif K-Brooks]
> If my dbroot['foo'] contains an OOBTree where the values are object which
> have an attribute called "attr", what's the best way to get a list of the
> objects whose "attr" attribute is equal to a certain value, along the
> lines of SQL "SELECT * FROM foo WHERE attr=123"? The most obvious method
> is [obj for obj in dbroot['foo'].iteritems() if obj.attr == 123], which
> gets decent performance for FileStorage, but the performance is terrible
> with ZEO (most likely due to transferring all of those objects over the
> network). What's the correct way to achieve the result I want?

As Christian Reis implied, unless you also build your own indexing objects
to speed such searches, brute force is the only approach.  Each object is
stored individually, and the only index FileStorage maintains is a mapping
from object id to the file offset at which the current revision of the
object's pickle lives.  It essentially doesn't even know your objects *have*
an attribute named "attr", let alone what the values of that attribute may
be.

An example of an indexing structure you could build is an OOBTree, mapping
each value "attr" may have to an OOSet of the objects where "attr" has that
value.  If we call that OOBTree "attr2objs", then the answer to your
question is just

    list(attr2objs[123])

and will be very fast.  But the burden is on you to create and update such
things, if you want them, or to use a package (like Christian's
IndexedCatalog) that tries to automate it for you.  There's no free lunch in
any case.



More information about the ZODB-Dev mailing list