【问题标题】:Django website is loading slow on Heroku, it is not because of QuerySetsDjango 网站在 Heroku 上加载缓慢,这不是因为 QuerySets
【发布时间】:2025-12-09 05:35:01
【问题描述】:

拥有在简单的通用基于类的视图上运行的网站。首先我想 - 查询集是问题,但由于我所有的查询都低于 8 毫秒,所以这不是问题。在本地,网站运行速度很快,所有图像和所有内容都在 200/300 毫秒以下。当我将它推到 Heroku 时,它很慢。在检查镀铬条中 - 它显示 1 条直线 1-2 秒,然后加载其余部分 - 它等待 1-2 秒然后加载。所以我开始分解这个过程并得出一个结论——删除了数据库,它开始像 100 毫秒一样快速加载——当我插入 Postgres 数据库时——立即进入慢速​​模式。

也安装了Django-Toolbar,看看发生了什么。

Note: this is not a question about the load time when the dyno is sleeping. It is a question about every single refresh, request while browsing the site - the experience.

这是结果。我什至尝试将数据库和应用程序放在更高层上只是为了看看并没有区别。所以我所做的也是创建简单视图测试 - 使用 3 个图像,我看到图像没有导致它并加载它 - 也需要 1-2 秒才能开始加载 - 删除数据库 - 加载速度非常快。

然后我遇到了这个: Persistent Connections

如果我设置为 500 - 加载时间更长 - 设置为 None - 稍微快一点 - 但始终不关闭数据库连接并不好。如果没有该设置,小型网站没有理由加载那么慢。

我什至尝试将它放在 18.04 Ubuntu 上的 Digital Ocean 上 - 并安装了 Postgres - 它在那里速度稍快但结果相似。不知道我应该做什么更多。

views.py - 所有视图都非常简单,没有复杂的逻辑,大部分甚至没有get_context_data 方法。

class DocumentaryFullDetailView(DetailView):
    model = Documentary
    template_name = "documentary-full.html"

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super().get_context_data(**kwargs)
        context['all_documentary_photos'] = Photo.objects.filter(documentary=self.get_object()).prefetch_related('documentary')
        return context

依赖关系:

bleach==3.1.0
boto==2.49.0
boto3==1.9.130
botocore==1.12.130
Collectfast==0.6.2
coverage==4.5.3
dj-database-url==0.5.0
dj-static==0.0.6
Django==2.2.3
django-admin-sortable2==0.7.2
django-appconf==1.0.3
django-bleach==0.5.3
django-boto==0.3.12
django-cacheops==4.1
django-ckeditor==5.6.1
django-compressor==2.2
django-debug-toolbar==2.0
django-environ==0.4.5
django-js-asset==1.2.2
django-markdown-deux==1.0.5
django-markdownx==2.0.28
django-model-utils==3.2.0
django-nocaptcha-recaptcha==0.0.20
django-redis==4.10.0
django-sendgrid-v5==0.8.0
django-storages==1.7.1
docutils==0.14
entrypoints==0.3
flake8==3.7.7
funcy==1.12
future==0.17.1
gunicorn==19.9.0
jmespath==0.9.4
Markdown==3.1
markdown2==2.3.7
mccabe==0.6.1
olefile==0.44
Pillow==6.0.0
psycopg2-binary==2.8.2
pycodestyle==2.5.0
pyflakes==2.1.1
python-dateutil==2.8.0
python-http-client==3.1.0
pytz==2018.9
rcssmin==1.0.6
redis==3.2.1
rjsmin==1.0.12
s3transfer==0.2.0
sendgrid==6.0.5
six==1.12.0
sqlparse==0.3.0
static3==0.7.0
urllib3==1.25.3
webencodings==0.5.1

这是 2000 毫秒左右的直线

【问题讨论】:

    标签: django performance heroku pagespeed heroku-postgres


    【解决方案1】:

    建立与 Postgres 数据库的连接可能非常缓慢。我建议使用像 PgBouncer (https://pgbouncer.github.io/) 这样的连接池。您可以为 Heroku 获得一个构建包,它可以透明地为您完成大部分艰苦的工作:https://github.com/heroku/heroku-buildpack-pgbouncer

    这样,您的 Django 应用程序会在需要时创建与本地套接字一样多的连接,并且 PgBouncer 会保留与实际数据库的长期连接池,而您只需进行昂贵的 Postgres 连接握手必要时创建。

    【讨论】:

    • 已添加该 buildpack,但结果相同 - 它说 - pgbouncer app detected, Using pgbouncer version: 1.8.1-heroku, pgbouncer done - 所以它已正确安装,但没有效果。
    • 这里可能有几个因素在起作用:池的初始连接仍然是按需连接的,因此在池预热时对服务器的前几个请求仍然很慢。此外,如果您没有为数据库配置使用默认环境变量,那么您也需要手动配置 pgbouncer,否则 Django 仍将直接连接到数据库。最后,连接时间问题也是一个假设 - 您可以在 github.com/django/django/blob/master/django/db/backends/… 周围放置一些调试语句来验证。
    • 谢谢@Sam 提供的所有信息——实际上你不需要做 buildpacks——heroku 内置了它——需要尝试一下——它不适用于爱好层——但我可以扩展它起来看看它是如何工作的 - https://devcenter.heroku.com/articles/postgres-connection-pooling