【问题标题】:Foreign keys used as data to negate join operations for optimization statergy外键用作数据来否定优化策略的连接操作
【发布时间】:2015-12-23 12:26:09
【问题描述】:

如果我有一个位置数据表:

餐桌位置

PK:Location_ID ,国家,州,城市

  1           RSA     EC     GRH
  2           USA     NY     NY city
  3           RSA     WC     Cape Town

表用户配置文件

PK:User ,name ,surname, FK: Location_ID

  1    John,   Cp          1
  2    Luck,   dv          3
  3    pete,   hrt         2

我的困惑,如果外键仅用于参照完整性,这是否意味着:表用户配置文件:列 Location_ID 不包含数据......

出于性能原因,我想避免加入。因此我可以说:

用户在注册时提供国家、州和城市:因此在 Locations 表中查找 Location_ID。将 Locations 表中的 Location_ID 插入到 User Profiles 表中到 User Profile 表中:Location_ID。

然后,在查找具有相同位置的所有用户时,我只需要查询配置文件表 Location_ID 即可找到这些用户......那么我应该将表用户配置文件:Location_ID 改为不是外键的列。 .

场景是匹配一个国家的具有相同兴趣的用户,

它位于另一个名为 Interest 的表中

PK:用户、兴趣

 1      golf
 2      cricket
 3      swimming
 1      Rugby
 2      golf

因此使用代码,我会说用户资料表中 location_Id:1 的用户,使用用户查询兴趣表的兴趣。因此返回用户和兴趣以使用代码进行进一步处理。

我正在使用的一本教科书似乎使用外键作为上面设置的位置数据

PK = 主键,FK = 外键。 谢谢。

【问题讨论】:

  • 你有什么问题?
  • FK 用于参照完整性,但数据仍存储在 Location ID 列中。此外,使用 1 次调用数据库的连接比使用多次数据库调用更好。

标签: sql django


【解决方案1】:

在 Django 文档中有一个很好的外键解释: https://docs.djangoproject.com/en/1.8/ref/models/fields/#foreignkey

在此处对在模型关系中使用外键的说明: https://docs.djangoproject.com/en/1.8/topics/db/models/#relationships

外国人正是你想要的。默认不进行join,外键返回整数。

模型

from django.db import models

class Location(models.Model):
    country = models.CharField()
    state = models.CharField()
    city = models.CharField()


class User(models.Model):
    name = models.CharField()
    surname = models.CharField()
    location = models.ForeignKey(Location, related_name='users')


class Interest(models.Model):
    interest = models.CharField()
    user = models.ForeignKey(User, related_name='interests')

多个查询

# This is NOT the best way to do it
# but I think it is what you are asking for as it contains no JOIN
# Multiple database hits are almost always more intensive than 
# a single request with a few joins

 # choose location id
location_id = 1

# get a list of user ids for the location
user_ids = User.objects.filter(location=location_id) \
    .values_list('id', flat=True)

# get interests from the list of user ids
location_user_interests = Interest.objects \
    .filter(user__in=user_ids) \
    .values_list('interest', flat=True)
print(location_user_interests

# OUTPUT: ['golf', 'rugby']
# QUERY TIME: 2.970 ms

一个查询

# choose location id
location_id = 1

# I think you will find this to be cleaner and faster
location_user_interests = Location.objects \
    .filter(id=location_id) \
    .prefetch_related('users', 'users__interests') \
    .values_list('users__interests__interest', flat=True)
print(location_user_interests)

# OUTPUT: ['golf', 'rugby']
# QUERY TIME: 0.660 ms

【讨论】:

  • 谢谢你,这很有帮助...我很难理解:.values_list('users__interests__interest', flat=True)...这是如何构建的...我如何获得附加用户名,假设我希望它作为数组的数组返回
  • 不客气。 Django 文档中提供了 values_list 函数的说明:docs.djangoproject.com/en/1.8/ref/models/querysets/#values-list。为了更基本地了解 QuerySet API,您应该阅读整个页面,而不仅仅是部分。由于您最初的问题已经结束,请通过接受答案来帮助我。
猜你喜欢
  • 1970-01-01
  • 2020-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-15
  • 2011-06-13
  • 1970-01-01
相关资源
最近更新 更多