- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在模型中使用 mptt 来管理标记系统(每个标记都有一个可选的 TreeForeignKey 到“父”标记)
每当我需要保存标签模型时,以下查询运行得异常缓慢(超过 45 秒)
UPDATE "taxonomy_taxonomy" SET "tree_id" = ("taxonomy_taxonomy"."tree_id" + %s) WHERE "taxonomy_taxonomy"."tree_id" > %s
我通过自动标记系统发送文章内容,该系统可以生成 20 多个标记。显然,那不会飞:)
我添加了 db_index=False 希望改变写入时间(读取似乎不是问题)但问题仍然存在。
这是有问题的模型:
class Taxonomy(MPTTModel):
parent = TreeForeignKey('self',blank=True,null=True,related_name='children',verbose_name='Parent', db_index=False)
parent_name = models.CharField(max_length=64, blank=True, null=True, editable=False)
name = models.CharField(verbose_name='Title', max_length=100, db_index=True)
slug = models.SlugField(verbose_name='Slug', blank=True)
primary = models.BooleanField(
verbose_name='Is Primary',
default=False,
db_index=True,
)
type = models.CharField(max_length=30, db_index=True)
created_date = models.DateTimeField(auto_now_add=True, null=True)
updated_date = models.DateTimeField(auto_now=True, null=True)
publication_date = models.DateTimeField(null=True, blank=True)
scheduled_date = models.DateTimeField(null=True, blank=True)
workflowstate = models.CharField(max_length=30, default='draft')
created_by = models.ForeignKey(User, null=True)
paid_content = models.BooleanField(verbose_name='Is Behind the Paywall', default=False, blank=True)
publish_now = True
show_preview = False
temporary = models.BooleanField(default=False)
def save(self, *args, **kwargs):
if self.slug is None:
self.slug = self.name
if not self.slug:
self.slug = slugify(self.name)[:50]
if self.parent:
self.parent_name = self.parent.name
self.slug = slugify(self.slug)
self.workflowstate = "published"
super(Taxonomy, self).save(*args, **kwargs)
store_to_backend_mongo(self)
publish_to_frontend(self)
以及查询计划(由 New Relic 报告):
1) Update on taxonomy_taxonomy (cost=0.00..133833.19 rows=90515 width=139)
2) -> Seq Scan on taxonomy_taxonomy (cost=0.00..133833.19 rows=90515 width=139)
3) Filter: ?
最后,来自这样一个查询的回溯:
Traceback (most recent call last):
File "/data/www/nj-cms/venv/lib/python3.4/site-packages/newrelic-2.54.0.41/newrelic/api/web_transaction.py", line 711, in __iter__
File "/data/www/nj-cms/venv/lib/python3.4/site-packages/newrelic-2.54.0.41/newrelic/api/web_transaction.py", line 1087, in __call__
File "/data/www/nj-cms/venv/lib/python3.4/site-packages/django/core/handlers/wsgi.py", line 189, in __call__
File "/data/www/nj-cms/venv/lib/python3.4/site-packages/django/core/handlers/base.py", line 132, in get_response
File "/data/www/nj-cms/venv/lib/python3.4/site-packages/newrelic-2.54.0.41/newrelic/hooks/framework_django.py", line 499, in wrapper
File "/data/www/nj-cms/venv/lib/python3.4/site-packages/django/contrib/auth/decorators.py", line 22, in _wrapped_view
File "./editorial/views.py", line 242, in calculate_queryly
File "./editorial/views.py", line 292, in queryly_function
File "/data/www/nj-cms/venv/lib/python3.4/site-packages/django/db/models/manager.py", line 127, in manager_method
File "/data/www/nj-cms/venv/lib/python3.4/site-packages/django/db/models/query.py", line 348, in create
File "./taxonomy/models.py", line 179, in save
File "./taxonomy/models.py", line 58, in save
File "/data/www/nj-cms/venv/lib/python3.4/site-packages/mptt/models.py", line 946, in save
File "/data/www/nj-cms/venv/lib/python3.4/site-packages/mptt/models.py", line 702, in insert_at
File "/data/www/nj-cms/venv/lib/python3.4/site-packages/mptt/managers.py", line 467, in insert_node
File "/data/www/nj-cms/venv/lib/python3.4/site-packages/mptt/managers.py", line 491, in insert_node
File "/data/www/nj-cms/venv/lib/python3.4/site-packages/mptt/managers.py", line 726, in _create_tree_space
File "/data/www/nj-cms/venv/lib/python3.4/site-packages/mptt/managers.py", line 364, in _mptt_update
File "/data/www/nj-cms/venv/lib/python3.4/site-packages/django/db/models/query.py", line 563, in update
File "/data/www/nj-cms/venv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 1062, in execute_sql
File "/data/www/nj-cms/venv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 840, in execute_sql
File "/data/www/nj-cms/venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute
File "/data/www/nj-cms/venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
File "/data/www/nj-cms/venv/lib/python3.4/site-packages/newrelic-2.54.0.41/newrelic/hooks/database_dbapi2.py", line 22, in execute
知道如何才能更快地保存这些模型吗?
编辑以获取更多信息:这是在 Postgres 中,使用 psycopg2 引擎'ENGINE': 'django.db.backends.postgresql_psycopg2',
第二次编辑:根据要求,我使用 EXPLAIN ANALYZE 运行查询。结果如下:
nj=# EXPLAIN ANALYZE UPDATE "taxonomy_taxonomy" SET "tree_id" = ("taxonomy_taxonomy"."tree_id" + 1) WHERE "taxonomy_taxonomy"."tree_id" > 1;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------
Update on taxonomy_taxonomy (cost=0.00..9588.75 rows=24582 width=132) (actual time=258718.550..258718.550 rows=0 loops=1)
-> Seq Scan on taxonomy_taxonomy (cost=0.00..9588.75 rows=24582 width=132) (actual time=59.956..8271.209 rows=24582 loops=1)
Filter: (tree_id > 1)
Rows Removed by Filter: 2
Planning time: 28.763 ms
Execution time: 258718.661 ms
(6 rows)
最佳答案
django mptt 使用 Nested Set Model
所以如果你的save方法导致insert操作django-mptt要重新计算很多数据。它只是不适用于大 table 。
你必须拒绝使用 django-mptt 并发明你自己的数据库模式。
关于Django MPTT Postgres 更新查询运行缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34496878/
我已经成功完成了 django-mptt 教程。我无法弄清楚如何做的是创建一个 child 的 child 。 child 的 child ,我的意思是更深的第三级。看下面的例子,我想创建 1.3.1
我想要一棵树来模仿带有文件夹和文件的文件系统。文件夹和文件将由具有不同字段属性的不同模型定义。 楷模: from mptt.models import MPTTModel, TreeForeignKe
在网上商店中,我使用 django-mptt 来处理多个级别的产品类别。我也有各自属于一个类别的产品。 现在我想像这样可视化类别树: all(18) - edible (10) -- food (4)
我正在尝试使用django-mptt运气微乎其微。这是来自 svn 的最新 Python2.5、windows、sqlite3、Django 1.2pre、django-mptt。 代码: 型号: c
我在我的 Django 应用程序中使用 django-mptt 作为文章模型。如果我想获取所有设置为例如隐藏的文章,我可以这样做 Article.objects.filter(hidden=False
我只是使用改进的预序树遍历 (MPTT) 构建一个表来存储分层数据——你知道的:每个节点存储 left 和 right ID 以查找其后代。我使用的是 CakePHP 建议的模型,它与标准方式不同,每
题 给定下面的模型,我想获得所有页面的查询集,用与页面关联的线程中的评论总数进行注释,包括与页面关联的评论线程树中的所有评论。 我正在使用 django-mptt存储评论树。 我可以使用 commen
我尝试了一段时间在我的项目中设置 django-mptt。我从 tutorial 中取样并相应地更改了模型,如下所示: class Genre(MPTTModel): pk = models.Auto
几个星期以来,我一直在用头撞在 table 上解决这个问题,所以我认为可能是时候寻求一些帮助了。 我正在尝试实现一个数据库结构,它具有组件零件的分层数据。我的主要问题在于尝试将一个“子程序集”分配给另
我正在尝试将 mptt 库用于简单的嵌套评论系统。 我的模特 class Comment(MPTTModel): event = models.ForeignKey(Event) au
我是 Django 和 MPTT 的新手,很难弄清楚如何获取所有叶节点并将它们直接发送到 Form 类。 例如,我创建了 MPTT 类别模型,并具有如下层次结构: 类别1 类别2 类别3 类别4 类别
我在尝试让导入导出适用于 mptt 模型时遇到问题。这是我的代码: models.py from django.db import models from mptt.models import MPT
情况是这样的。我正在 Django 中利用 MPTT 模型来创建音乐流派的层次结构(摇滚、硬摇滚等)。我将此层次结构的节点之一分配给相册。假设我创建了一个具有硬摇滚风格的专辑对象。我如何查询我的专辑中
我正在尝试弄清楚如何编写返回多维数组的函数。我知道如何使用“category_parent”值编写函数。但我只是想编写一个函数,仅使用左右键即可创建多维数组。 array(71) { [0]=>
.move_to() 方法对我不起作用。我看过其他人建议保存实例以及重新加载节点实例的帖子。但这些解决方案都不适用于此测试用例。我也尝试过 ._tree_manager.rebuild(),它也不起作
情况是这样的。我正在 Django 中利用 MPTT 模型来创建音乐流派的层次结构(摇滚、硬摇滚等)。我将此层次结构的节点之一分配给相册。假设我创建了一个具有硬摇滚风格的专辑对象。我如何查询我的专辑中
我在模型中使用 mptt 来管理标记系统(每个标记都有一个可选的 TreeForeignKey 到“父”标记) 每当我需要保存标签模型时,以下查询运行得异常缓慢(超过 45 秒) UPDATE "t
因此,我使用 MySQL 并将基于嵌套集/修改后的预序树遍历模型的类别结构存储在表名“nested_category”中,该表具有以下字段:category_id,name,lft,rgt,publi
我有一个线程评论系统,在 99.9% 的时间里都可以正常工作,但偶尔树会崩溃,左/右值会重复。 我发现当两个帖子同时发生时(彼此相隔不到一秒)会发生这种情况,并且大概发生的情况是第二个帖子在第一个之前
我在我的 Django 项目中使用 MPTT 树结构来组织评论。我只有 2 个级别:评论和评论的评论除了排序之外,一切都完美无缺。 我想按创建日期上升(“-creation_date”)对所有没有父项
我是一名优秀的程序员,十分优秀!