Django网站在Heroku上的加载速度很慢,这不是因为QuerySets

时间:2019-07-29 18:05:36

标签: django performance heroku pagespeed heroku-postgres

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

还安装了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-速度更快-但是始终不关闭数据库连接是不好的。如果没有该设置,没有理由为什么小型站点的加载速度如此之慢。

我什至尝试将其放在Ubuntu 18.04上的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

这是2000ms左右的直线

Chrome Inspect

django-toolbar

sql querysets

dom loading

1 个答案:

答案 0 :(得分:0)

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

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

相关问题