Re: [SQLObject] Indexed access to a SelectResults
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Ian B. <ia...@co...> - 2003-07-16 19:21:27
|
On Wed, 2003-07-16 at 08:37, Matt Goodall wrote: > If you have code like this > > all = Entity.select('all') > for i in range(len(all)): > print all[i] > > you get a whole stream of SQL calls. For a table with n rows you > actually get 2n+1 SQL calls: 1 initial COUNT(*) and then a COUNT(*) and > SELECT for each row. > > It makes sense for a SelectResults objects to cache the COUNT result on > the first len() so that future len() calls don't go back to the database? Huh... I don't know why it's creating lots of COUNT calls. Is it really? It would, though, create a bunch of SELECTs, like SELECT blah LIMIT 1 OFFSET 0, SELECT blah LIMIT 1 OFFSET 1, etc. The problem is the way you're using it. You should iterate directly over the result, or force getting everything at once by doing list(all). Like either: all = list(Entity.select()) for i in range(len(all)): print all[i] # or: all = Entity.select() for i, obj in enumerate(all): print obj Of course, the second is better as you won't pull all the objects into memory at once. enumerate isn't defined in Python 2.2, so you can use: from __future__ import generators def enumerate(iterable): i = 0 for obj in iterable: yield i, obj i += 1 |