在单个查询中获取django模型中ManyToMany字段的优化方法

时间:2011-01-05 08:09:58

标签: sql django performance django-models

我在django中有以下模型结构。

Category:
    - name
    - description

Tag:
    - name

Article:
    - guid
    - title
    - detail
    - tags (Many2Many with Tag)
    - cats (Many2Many with Category)

现在,在我的批量(高达100,000)文章更新作业中,我必须执行以下操作。

- for each article:
     - to check if this article has already appeared in another category
         - if yes, associate article with current category
         - if no, create article along current category
     - to check if any of associated tags are exists or not,
          - if not exists create tag, and associate with current article
          - if exists, just associated with current article

执行此操作时,我已经拥有从数据库中提取的现有文章的字典。

由于ManyToMany字段对django中的select_related()等提取操作没有任何影响,有没有其他方法可以帮助我加载现有文章以及类别和标签?

1 个答案:

答案 0 :(得分:0)

如果您为多对多关系添加intermediary table,例如ArticleTag,您可以通过django的ORM查询此表并在其上使用select_related

# Get all 'tag-relations' WITH associated articles AND tags
articles_tags = ArticleTag.objects.all().select_related()

你可以在结果上迭代然后检查我是否与标签相关联的特定文章......我想django中没有其他方法可以直接查询代表关系的表而不指定through - 模型,否则您仍然可以使用原始SQL查询来查询它。如果你有太多的数据,这可能会产生大量的ram使用,但是你只会打一次数据库!