0

So I am trying to make a social network on Django. Like any other social network users get the option to like a post, and each of these likes are stored in a model that is different from the model used for posts that show up in the news feed. Now I have tried two choices to get the like status on the go.

1.Least database hits:

Make one sql query and get the like entry for every post id if they exist.Now I use a custom django template tag to see if the like entry for the current post exist in the Queryset by searching an array that contains like statuses of all posts. This way I use the database to get all values and search for a particular value from the list using python.

2.Separate Database Query for each query:

Here i use the same custom template tag but rather that searching through a Queryset I use the mysql database for most of the heavy lifting. I use model.objects.get() for each entry.

Which is a more efficient algorithm. Also I was planning on getting another database server, can this change the choice if network latency is only around 0.1 ms.

Is there anyway that I can get these like statuses on the go as boolean values along with all the posts in a single db query.

An example query for the first method can be like

Let post_list be the post QuerySet

models.likes.objects.filter(user=current_user,post__in = post_list)
Community
  • 1
  • 1
krishnan
  • 263
  • 1
  • 2
  • 7
  • 2
    I have one word for you: `benchmarking`. There is no simple answer, all will depend on your models, database structure, server configuration and even hardware that you're using. In some cases, python will be faster, in other it will be better choice to filter in on database - level. – GwynBleidD Aug 30 '15 at 07:45
  • It is a bit hard to read your question because it has so many typos and omissions. `"Make one sql query and get like entry every post id if they exist."` I am really not sure what you are trying to say here. – Konstantin Schubert Aug 30 '15 at 07:53
  • @krishnan I am not able to understand what you write. There are too many grammatical mistakes. My comment was referring to this, it was not giving advice on the topic. – Konstantin Schubert Aug 30 '15 at 08:11

1 Answers1

0

This is not a direct answer to your question, but I hope it is useful nonetheless.

and each of these likes are stored in a model that is different from the model used for news feed

I think you have a design issue here. It is better if you create a model that describes a post, and then add a field users_that_liked_it as a many-to-many relationship to your user model. Then, you can do something like post.users_that_liked_it and get a query set of all users that liked your page.

In my eyes you should also avoid putting logic in templates as much as possible. They are simply not made for it. Logic belongs into the model class, or, if it is dependent on the page visited, in the view. (As a rule of thumb).

Lastly, if performance is your main worry, you probably shouldn't be using Django anyway. It is just not that fast. What Django gives you is the ability to write clean, concise code. This is much more important for a new project than performance. Ask yourself: How many (personal) projects fail because their performance is bad? And how many fail because the creator gets caught in messy code?

Here is my advice: Favor clarity over performance. Especially in a young project.

Konstantin Schubert
  • 3,242
  • 1
  • 31
  • 46
  • Thanks a lot @Konstantin. Adding a many-to-many relation really did give me a new insight. But I don't want a list of everyone who liked the post **won't this be unnecessary overhead for the database**? .Rather, similar to using a many to many relationship is there a way to get only the entry corresponding to the current logged in user. – krishnan Aug 30 '15 at 08:19
  • You could also add the many-to-many field to the user model instead, (like: `user.posts_the_user_liked`) but that's a bit complicated because you would need to modify the user model. Alternatively, with the design I suggested you can get all posts that are liked by a user via: `user.post_set.all()` Please read http://stackoverflow.com/questions/9352662/how-to-use-the-reverse-of-a-django-manytomany-relationship for more information. As I said, I would worry more about code clarity than performance. – Konstantin Schubert Aug 30 '15 at 08:50
  • I have implemented a UserProfile model that follows a one-to-one relationship with the auth_user model .I have already made a working social network, so now I believe it's time for me to optimize it.If I get all the posts a user has liked, I believe that I will still be forced to search the whole list for only those likes concerning the posts in the context. Would this be any different from `models.likes.objects.filter(user=current_user, post__in=posts_list)` – krishnan Aug 30 '15 at 09:07
  • @krishnan I don't know. Sorry. – Konstantin Schubert Aug 30 '15 at 09:34