Showing posts with label optimization. Show all posts
Showing posts with label optimization. Show all posts

Wednesday, March 2, 2011

Disqus experience - cache of model's related fields

Very interesting tricks from Disqus: Scaling The World’s Largest Django Application

The main idea to retrieve related fields by two requests instead of 1 + 25 sql requests using post.user

Such situation caused in several projects simultaneously so I working on optimization things right now.

# cache
posts = Post.objects.all()[0:25]

users = dict(
    (u.pk, u) for u in \
    User.objects.filter(pk__in=set(p.user_id for p in posts))
)

for p in posts:
    p.__user_cache = users.get(p.user_id)


And don't forget about SQL indexes - in all cases when field used in ORDER or WHERE sql statements it will increase performance a lot