为什么我的 django/psql 网站速度如此之慢?

发布于 2025-01-03 21:53:43 字数 1211 浏览 1 评论 0原文

我有一个 django 网站,它只有一个管理界面,因为它是内部的东西。

简单的页面几乎可以立即加载,例如包含 10 个项目的表格。

但包含大量内联等内容的页面将需要 5-10 秒才能加载,有时甚至更长。几乎无法使用了。

无论如何,这都不是一个巨大的数据库。我有几十个模型,没有一个表的条目超过 500 个。

在具有 12 GB RAM 的最新四核 Xeon 上运行 Ubuntu 10.04。

生产服务器 (mod_wsgi) 和开发服务器也会出现问题。一般来说,生产服务器的情况更糟,但两者都非常慢。

在延迟期间,apache(如果我使用生产服务器)或 python(如果我使用开发服务器)的一个 CPU 核心达到最大。

我尝试过使用 WSGIDaemonProcess,更改线程和进程的数量,但无济于事。

下面是我的 apache 配置文件,但就像我说的,我的开发服务器也有问题,所以这可能不是真正的 apache 问题。

感谢您的任何帮助。对任何不当使用术语等表示歉意。我是一名 C 和 MATLAB 程序员,对服务器、网络、数据库或 python 不太了解。另外,我确实意识到有几个关于 django 响应缓慢的线程,并且我已经阅读了所有这些线程,但我还没有找到任何对我的情况有帮助的内容。谢谢!

WSGIDaemonProcess MY_ACTUAL_IP_ADDRESS user=MY_USERNAME group=MY_USERNAME threads=4
WSGIProcessGroup MY_ACTUAL_IP_ADDRESS

WSGIScriptAlias /wsgi /neuroling/projects/neuroxy/neuroxy_project/neuroxy.wsgi
<Directory /neuroling/projects/neuroxy/neuroxy_project/>
  Order allow,deny
  Allow from all
</Directory>

Alias /media/ /var/www/media/
Alias /static/ /var/www/static/

<Directory /var/www/static>
Order deny,allow
Allow from all
</Directory>

<Directory /var/www/media>
Order deny,allow
Allow from all
</Directory>

I have a django site which has only an admin interface, since it's an internal thing.

Simple pages load almost instantly, like say a table with 10 items.

But a page with lots of inlines and so on will take 5-10 seconds to load, sometimes even longer. It's almost unusable.

This is NOT a huge database by any means. I have a few dozen models and no table has more than 500 entries.

Running Ubuntu 10.04 on a recent quad-core Xeon with 12 GB RAM.

The problems occur with the production server (mod_wsgi) and with the development server too. Generally worse with the production server, but both are seriously slow.

During the delay, one CPU core maxes out for apache (if I'm using the production server) or python (if I'm using the development server).

I have tried playing around with WSGIDaemonProcess, changing the number of threads and processes, but to no avail.

Below is my apache config file, but like I said I have problems with the dev server too so this may not really be an apache issue.

Thanks for any help. Apologies for any improper use of terminology, etc. I am a C and MATLAB programmer and don't really know much about servers, networks, databases or python. Also, I do realize there are several threads on slow django responses, and I have read them all, but I haven't found anything that helps my situation. Thanks!

WSGIDaemonProcess MY_ACTUAL_IP_ADDRESS user=MY_USERNAME group=MY_USERNAME threads=4
WSGIProcessGroup MY_ACTUAL_IP_ADDRESS

WSGIScriptAlias /wsgi /neuroling/projects/neuroxy/neuroxy_project/neuroxy.wsgi
<Directory /neuroling/projects/neuroxy/neuroxy_project/>
  Order allow,deny
  Allow from all
</Directory>

Alias /media/ /var/www/media/
Alias /static/ /var/www/static/

<Directory /var/www/static>
Order deny,allow
Allow from all
</Directory>

<Directory /var/www/media>
Order deny,allow
Allow from all
</Directory>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静若繁花 2025-01-10 21:53:43

我现在已经解决了这个问题。我复制了数据库并删除了大部分数据,看看这是否会影响速度。事实上,它不再缓慢了。这让我意识到问题出在数据库(或访问数据库)上,而不是 apache 或类似的东西。

然后我打开了 postgresql 的日志记录并查看了日志。实际上有数千条看似无关紧要的奇怪 SQL 语句被发送。仔细检查发现,它们都被发送去填写包含数千个条目的外键选择框。在任何给定情况下,实际上只有少数条目是可能的,但在 django 中过滤选择框似乎非常复杂,而且我还没有完成这一点。但我所做的是将这些字段指定为 raw_id_fields,这会阻止创建选择框。彻底解决了速度慢的问题。

感谢那些对我的问题提出建议的人。我希望这个答案对其他人有帮助。

I have solved this now. I made a copy of my database and deleted most of the data, to see whether that would affect the speed. Indeed, it stopped being slow. That clued me into the fact that the problem was something about the database (or accessing it), rather than apache or anything like that.

So then I turned on logging in postgresql and looked at the logs. There were literally thousands of weird SQL statements getting sent that seemed irrelevant. Closer inspection revealed they was all being sent to fill in a ForeignKey select-box which contained thousands of entries. Only a few of those entries are actually possible in any given case, but it seems really complicated to filter select-boxes in django and I haven't accomplished that yet. But what I did was specify these fields as raw_id_fields, which prevents a select-box being created. That completely solved the slowness problem.

Thank you to those who gave suggestions on my question. I hope this answer may be helpful to others.

只怪假的太真实 2025-01-10 21:53:43

您是否尝试过使用 Django 调试工具栏来尝试隔离慢点。

您还可以使用生产应用程序性能监控工具,例如 New Relic。

http://blog.newrelic.com/2011/11/ 08/new-relic-supports-python/

http://www.newrelic.com

新遗物有一个两周试用期,所有功能均可用。在此期间,充分利用性能故障和慢事务跟踪功能来解决问题。两周后,它将下降到免费订阅功能级别,您将失去缓慢的事务跟踪,但仍然会出现针对 Web 事务的性能故障。

Have you tried Django debug toolbar to try and isolate the slow spot.

You could also use production application performance monitoring tools such as New Relic.

http://blog.newrelic.com/2011/11/08/new-relic-supports-python/

http://www.newrelic.com

New Relic has a two week trial period with all features available. During that time make good use of the performance breakdown and slow transaction trace features to work out the problem. After two weeks it will drop down to free subscription feature level and you will loose the slow transaction traces but will still have performance breakdowns against web transactions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文