[PonyORM-list] Retrieve 10 most recent related records
Alexander Kozlovsky
alexander.kozlovsky at gmail.com
Tue Nov 11 07:26:07 UTC 2014
Hi Dylan,
I think this is the most efficient way:
class Person(db.Entity):
id = PrimaryKey(int, auto=True)
checkouts = Set("Checkout")
@property
def recent_checkouts(self):
return Checkout.select(lambda c: c.person == self) \
.order_by(lambda c: desc(c.date))[:10]
class Checkout(db.Entity):
id = PrimaryKey(int, auto=True)
person = Required(Person)
date = Required(date)
composite_index(person, date)
Note that I defined composite non-unique index on person and date
attributes in Checkout entity. This way DBMS can do filtering and sorting
using just one index.
Regards,
Alexander
On Tue, Nov 11, 2014 at 3:13 AM, Dylan Staley <staley.dylan at gmail.com>
wrote:
> Hi all! I'm using these two models:
>
> class Person(db.Entity):
> id = PrimaryKey(int, auto=True)
> checkouts = Set("Checkout")
>
> @property
> def recent_checkouts(self):
> # return 10 most recent checkouts
>
>
> class Checkout(db.Entity):
> id = PrimaryKey(int, auto=True)
> person = Required(Person)
> date = Required(date)
>
> I'd like to be able to call Person.recent_checkouts to get a list of that
> person's ten most recent checkouts. How would I accomplish this in the most
> performant manner?
>
> Best,
> Dylan Staley
>
> _______________________________________________
> ponyorm-list mailing list
> ponyorm-list at ponyorm.com
> /ponyorm-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </ponyorm-list/attachments/20141111/c0700afe/attachment.html>
More information about the ponyorm-list
mailing list