'Improve db performance in DetailView

I want to improve speed and performance of my blog and avoid the duplicate queries. I'm using the Django debug toolbar for that btw.

Note: I'm indexing my models.

Here are the models I have:

class Post(Model):
    name ...
    ...
    author = ForeignKey(Author..)
    comments = ManyToManyField(Comment...)
    ...

class Comment(Model):
    content...
    ...
    author = ForeignKey(Author...)
    replies = ManyToManyField(Reply...)

class Reply(Model):
    content...
    ...
    author = ForeignKey(Author...)
    ...

Is there any way to improve the speed of the post detail page, it's above 2 seconds before caching. and 1 second after caching.

Here is my try:

class PostDetailView(HitCountDetailView):
    ...
    def get_queryset(self):
        return super(PostDetailView, self).get_queryset().select_related().prefetch_related()
    
    ....

Also I'm using the django-hitcount to track visitors, but it's quite slowy, any fix for that too.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source