【发布时间】: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