Assuming I have a Django/PostgreSQL powered video sharing site like YouTube - with an additional blog included: What's the better approach concerning performance when implementing comments?
a) Having one comments app/model that is used for both, videos and blog posts. In this case, Django creates a single comments table, which holds an additional content type field, to determine whether a comment belongs to the blog or the video app.
b) Having two separate comment models, one that lives inside the blog app, the other one lives in the video app. In this case, Django creates two separate database tables - and no content type field.
Assuming we have millions of comments in each app, is there an advantage in using two separate database tables when retrieving all comments for a certain video? Or is using a database index on the content type fields just as efficient?
Using multiple tables will be likely to be faster, since it's a simpler lookup, and avoids the additional queries to the Django content_type table. However, that approach could make for more difficulty in code maintenance. So that's the tradeoff you need to consider.
What you could do is use a comments abstract base model, something along the lines of:
from django.db import models
class BaseComment(models.Model):
author = ...
title = ...
body = ...
class Meta:
abstract = True
class VideoComment(BaseComment):
video = models.ForeignKey(...)
That way, you get the more performant db lookups, with less of the code maintenance overhead.