Django,按字符串过滤是否比 SQL 关系更快?
Django, is filtering by string faster than SQL relationships?
如果我通过 user_id
(string
) 查询我的用户信息而不是创建 Profile
模型并使用 [=将它们链接到其他模型,这是否是一个主要缺陷=34=] 关系?
示例 1:(user_id
存储在 django 会话中。)
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.
示例2:(user_id
存储在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
.
在 Django 上,使用字符串而不是查询对象会影响检索项目的性能吗?
正如 Dirk 所说,这里的性能不是问题;一旦列被索引,与其他因素相比,数据类型之间的性能差异应该可以忽略不计。这里有一个 related SO question 以获得更多视角。
您应该注意的是防止重复数据,然后您必须自己处理这些数据的完整性,而不是依赖数据库中经过良好测试的完整性检查。
另一个方面是,如果您的数据之间确实存在关系,您绝对应该使用 Django 的 relationships 确保它们在您的模型中准确表示。否则,使用 Django 的 ORM 真的没有多大意义。祝你好运!
如果我通过 user_id
(string
) 查询我的用户信息而不是创建 Profile
模型并使用 [=将它们链接到其他模型,这是否是一个主要缺陷=34=] 关系?
示例 1:(user_id
存储在 django 会话中。)
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.
示例2:(user_id
存储在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
.
在 Django 上,使用字符串而不是查询对象会影响检索项目的性能吗?
正如 Dirk 所说,这里的性能不是问题;一旦列被索引,与其他因素相比,数据类型之间的性能差异应该可以忽略不计。这里有一个 related SO question 以获得更多视角。
您应该注意的是防止重复数据,然后您必须自己处理这些数据的完整性,而不是依赖数据库中经过良好测试的完整性检查。
另一个方面是,如果您的数据之间确实存在关系,您绝对应该使用 Django 的 relationships 确保它们在您的模型中准确表示。否则,使用 Django 的 ORM 真的没有多大意义。祝你好运!