0

我们使用 Django 托管的网站在生产服务器上非常慢:数据库访问似乎很慢(Mysql),下载文件很慢(我尝试了 X-Sendfile,但没有影响)。

使用基于此代码段的内容进行分析,最重:

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
 11570    0.577    0.000    0.577    0.000 /var/lib/python-support/python2.6/MySQLdb/times.py:43(DateTime_or_None)
  5786    0.500    0.000    0.617    0.000 /usr/local/lib/python2.6/dist-packages/django/db/models/base.py:244(__init__)
  5796    0.205    0.000    0.576    0.000 /usr/local/lib/python2.6/dist-packages/django/forms/widgets.py:411(render_option)
     8    0.190    0.024    1.014    0.127 /var/lib/python-support/python2.6/MySQLdb/cursors.py:282(_fetch_row)

21.6%   0.577 /var/lib/python-support/python2.6/MySQLdb/times.py
19.4%   0.520 /usr/local/lib/python2.6/dist-packages/django/db/models/base.py
 9.5%   0.253 /usr/local/lib/python2.6/dist-packages/django/forms/widgets.py
 7.4%   0.199 /var/lib/python-support/python2.6/MySQLdb/cursors.py

33.0%   0.882 /var/lib/python-support/python2.6/MySQLdb
32.6%   0.873 /usr/local/lib/python2.6/dist-packages/django/db
15.4%   0.413 /usr/local/lib/python2.6/dist-packages/django/forms
10.5%   0.280 /usr/local/lib/python2.6/dist-packages/django/utils

上面的这个调用是在管理页面上运行的。

我能做什么: - 更新 mysql - 更新整个 django 框架(仍在 1.1 上运行)

但我认为,一定有其他原因导致速度如此缓慢。有任何想法吗?

4

2 回答 2

0

仅在生产服务器上第一次时它应该很慢,但是对于以下对页面的访问,例如 django memcached 的行为应该是不同的。

 install django-memcached on production server

 settings.py:

 CACHE_BACKEND ="memcached://127.0.0.1:11211/" # change the IP to the server IP
 CACHE_TIMEOUT =60*60     #to keep items in the cache it's up to you

 # if you wish to cache some data which are permanently in high demand 
 views.py :
 from django.core.cache import cache
 from project.settings import CACHE_TIMEOUT
 # example of how to use the cache in the methods
 def method(request):
    model_cache_key = request.path
c = cache.get(model_cache_key)
if not c:
    c=get_object_or_404(model,id=self.id)
    cache.set(model_cache_key,c,CACHE_TIMEOUT)
#END OF USE CACHE HERE
foo=c.modelname_set.all()
    # more code here
    return #whatever you want
于 2013-08-19T06:21:14.753 回答
0

您可以在您的配置文件输出中看到,大量时间花费在DateTime_or_NoneMySQLdb 的功能上。

我最近遇到了同样的问题,似乎DATETIME在 MySQL 中使用字段不是一个好主意,尤其是使用 python,因为它非常缓慢且通常不必要地转换为 pythondatetime类型。

我找到了一些关于它的文章:

好吧,我不确定丢弃DATETIME是否是正确的解决方案,也许从 MySQLdb 更改为其他一些 MySQL 适配器将有助于提高性能。

于 2014-03-28T11:27:42.517 回答