Chapter 11.4.4. Don’t Go to the DB Multiple Times for the Same Objects

Another common performance problem you can run into when using SQLObject is redundant access to the DB. SQLObject caches object state by default. That means that if you access multiple attributes of the same object, SQLObject will not go to the DB for every access. However, if you access a particular object or slice of a SearchResults object, SQLObject will execute a query for each access. The following code sample demonstrates this point:

robots = Bigwell.select()

# Ten (5 x 2)queries of 1 row
for i in range (0,5):
    id    = robots[i].id
    robot = robots[i].robot
    print id, robot

# One query of 5 rows
for r in robots[:5]:
    id    = r.id
    robot = r.robot
    print id, robot

Both code snippets ...

Get Rapid Web Applications with TurboGears: Using Python to Create Ajax-Powered Sites now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.