Horai Nuri
Horai Nuri

Reputation: 5578

Django, is filtering by string faster than SQL relationships?

Is it a major flaw if I'm querying my user's information by their user_id (string) rather than creating a Profile model and linking them to other models using SQL relationships?

Example 1: (user_id is stored in django sessions.)

class Information(models.Model):
    user_id = models.CharField(...)
    ...

# also applies for .filter() operations.
information = Information.objects.get(user_id=request.getUser['user_id'])

note: I am storing the user's profile informations on Auth0.

Example 2: (user_id is stored in Profile.)

class Profile(models.Model):
    user_id = models.CharField(...)

class Information(models.Model):
    profile = models.ForeginKey(Profile, ...)
    ...

information = Information.objects.get(profile=request.getProfile)

note: With this method Profile will only have one field, user_id.

On Django, will using a string instead of a query object affect performances to retrieve items?

Upvotes: 3

Views: 281

Answers (1)

Endre Both
Endre Both

Reputation: 5730

Performance is not an issue here as noted by Dirk; as soon as a column is indexed, the performance difference between data types should be negligible when compared to other factors. Here's a related SO question for more perspective.

What you should take care of is to prevent the duplication of data whose integrity you then would have to take care of on your own instead of relying on well-tested integrity checks in the database.

Another aspect is that if you do have relations between your data, you absolutely should make sure that they are accurately represented in your models using Django's relationships. Otherwise there's really not much point in using Django's ORM at all. Good luck!

Upvotes: 4

Related Questions