gpt4 book ai didi

python - Django 数据库优化?这是缓慢的还是正常的?

转载 作者:行者123 更新时间:2023-11-28 20:25:47 24 4
gpt4 key购买 nike

我有几个与数据库性能相关的问题。我的应用程序中有以下 Django 模型及其相应的管理类。数据库是 MySQL,托管在 Amazon RDS 上。

  1. 通过 for 循环在通知表中添加大约 45000 条记录花费了 20 多分钟。这次是慢还是正常?
  2. 此表的 Django 管理界面太慢了。在没有 dB 负载的情况下加载大约需要 30 秒。并且数据库在执行任何作业时,通常需要 2 分钟以上的时间来加载。该表通常每周或每两周添加一百万条记录。有没有办法提高数据库和/或系统的性能,或者当前的加载时间是否正常?

模型

class Notification(models.Model):
id = models.AutoField(primary_key=True)
token = models.ForeignKey(Token, blank=False, null=False)
alert = models.ForeignKey(Alert, blank=False, null=False)
created_at = models.DateTimeField(auto_now=True)
is_sent = models.BooleanField(default=False)
is_processed = models.BooleanField(default=False)
error_sending = models.BooleanField(default=False)
# ...
def __unicode__(self):
return u'%s' % (self.alert )

管理员

class AppNotification(admin.ModelAdmin):
fields = ['token','alert','is_sent','is_processed','error_sending']

#
list_display = ('token','alert','created_at','is_sent','is_processed','error_sending')

#
search_fields = ('app__app_name','token__token')

#
list_select_related = True

#
list_per_page = 25

admin.site.register(Notification,AppNotification)

最佳答案

It took more than 20 minutes to add around 45000 records in the Notifications table, via a for loop. Is this time slow or normal?

在这种设置中并不少见。 Django ORM 速度不快,如果速度很重要,则不是添加数千个条目的好方法。 RDS 的速度取决于实例类型,但通常低端的也不快。

备选方案是使用低级 SQL 或使用 bulk_create ,但是您必须记住,这些方法都不会调用对象 .save() 也不会发送 pre_savepost_save 信号。在这些包含大量业务逻辑的情况下,这可能是一个问题。

The django admin interface for this table is too slow. It takes around 30 seconds to load under no dB load. And it usually takes more than 2 minutes to load when the database is performing any job. This table usually adds one million records every week or two. Is there a way to improve the performance of the database and/or system, or is this current load time normal?

您的模型管理器中有 list_select_related = True,这似乎是唯一可以使其变慢的东西。它确实与 TokensAlerts 结合。

关于python - Django 数据库优化?这是缓慢的还是正常的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13960386/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com