问题:
背景信息:
简而言之:
诊断:
Django调试工具栏(用于在dev专用框上最慢的最慢操作调用)
用户CPU时间1926.979毫秒
系统CPU时间27.074毫秒
总CPU时间1954.053毫秒
经历的时间1980.884毫秒
上下文切换71自愿,44非自愿
的httpd.conf
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule dir_module modules/mod_dir.so
LoadModule env_module modules/mod_env.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule mime_module modules/mod_mime.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule setenvif_module modules/mod_setenvif.so
LoadModule wsgi_module modules/mod_wsgi.so
LoadModule unixd_module modules/mod_unixd.so
...
KeepAlive Off
SetEnvIf X-Forwarded-SSL on HTTPS=1
ServerLimit 1
StartServers 1
MaxRequestWorkers 5
MinSpareThreads 1
MaxSpareThreads 3
ThreadsPerChild 5
...
WSGIApplicationGroup %{GLOBAL}
WSGIDaemonProcess djangoapp processes=2 threads=8 python-path=...
WSGIProcessGroup djangoapp
WSGIRestrictEmbedded On
WSGILazyInitialization On
活动服务器上的Opbeat (这个抓取有更多/更长的数据库查询,我正在测试删除selecte_related()/ prefetch_related() - 它们帮助了数据库查询时间,但没有多少关闭总时间):
Wtf,10张发布图片的声誉?
最后的想法:
答案 0 :(得分:0)
在我的调查中,var leaseList = (from l in leases.tblfLeaseDetails
join a in leases.tblfAuthorizations on l.Lease_Detail_ID equals a.Lease_Detail_ID into la
from jla in
(from aj in leases.tblfAuthorizations
where aj.Lease_Detail_ID == l.Lease_Detail_ID
orderby aj.Authorized_Date descending
select aj).Take(1).DefaultIfEmpty()
join p in leases.tblfPayments on l.Lease_Detail_ID equals p.Lease_Detail_ID into lp
from jlp in
(from pj in leases.tblfPayments
where pj.Lease_Detail_ID == l.Lease_Detail_ID
orderby pj.Payment_Date descending
select pj).Take(1).DefaultIfEmpty()
join v in leases.tblvVendors on l.Vendor_ID equals v.Vendor_ID into lv
from jlv in lv.DefaultIfEmpty()
join c in leases.tblvCounties on l.County_ID equals c.County_ID into lc
from jlc in lc.DefaultIfEmpty()
select new LeaseViewModel()
{
Lease_Detail_ID = l.Lease_Detail_ID,
Vendor_Name = jlv.Vendor_Name,
County = jlc.County,
Authorization = jla.Authorized,
Payment_Date = jlp.Payment_Date
}).Distinct()
速度很慢,因此功能Tastypie
很慢。
我以这种方式从django.core.urlresolvers.reverse
辞职并修补resource_uri
:
Tastypie
from __future__ import absolute_import
from django.core.exceptions import ObjectDoesNotExist
from tastypie.bundle import Bundle
from tastypie.exceptions import ApiFieldError
def dehydrate(self, bundle, for_list=True):
if not bundle.obj or not bundle.obj.pk:
if not self.null:
raise ApiFieldError(
"The model '%r' does not have a primary key and can not be used in a ToMany context." % bundle.obj)
return []
the_m2ms = None
previous_obj = bundle.obj
attr = self.attribute
if isinstance(self.attribute, basestring):
attrs = self.attribute.split('__')
the_m2ms = bundle.obj
for attr in attrs:
previous_obj = the_m2ms
try:
the_m2ms = getattr(the_m2ms, attr, None)
except ObjectDoesNotExist:
the_m2ms = None
if not the_m2ms:
break
elif callable(self.attribute):
the_m2ms = self.attribute(bundle)
if not the_m2ms:
if not self.null:
raise ApiFieldError(
"The model '%r' has an empty attribute '%s' and doesn't allow a null value." % (previous_obj, attr))
return []
self.m2m_resources = []
m2m_dehydrated = []
# TODO: Also model-specific and leaky. Relies on there being a
# ``Manager`` there.
m2ms = the_m2ms.all() if for_list else the_m2ms.get_query_set().all()
for m2m in m2ms:
m2m_resource = self.get_related_resource(m2m)
m2m_bundle = Bundle(obj=m2m, request=bundle.request)
self.m2m_resources.append(m2m_resource)
m2m_dehydrated.append(self.dehydrate_related(m2m_bundle, m2m_resource, for_list=for_list))
return m2m_dehydrated
def _build_reverse_url(self, name, args=None, kwargs=None):
return kwargs.get('pk')
def get_via_uri(self, uri, request=None):
bundle = self.build_bundle(request=request)
return self.obj_get(bundle=bundle, pk=uri)
def build_related_resource(self, value, request=None, related_obj=None, related_name=None):
"""
Returns a bundle of data built by the related resource, usually via
``hydrate`` with the data provided.
Accepts either a URI, a data dictionary (or dictionary-like structure)
or an object with a ``pk``.
"""
self.fk_resource = self.to_class()
kwargs = {
'request': request,
'related_obj': related_obj,
'related_name': related_name,
}
if isinstance(value, Bundle):
# Already hydrated, probably nested bundles. Just return.
return value
elif isinstance(value, (basestring, int)):
# We got a URI. Load the object and assign it.
return self.resource_from_uri(self.fk_resource, value, **kwargs)
elif isinstance(value, Bundle):
# We got a valid bundle object, the RelatedField had full=True
return value
elif hasattr(value, 'items'):
# We've got a data dictionary.
# Since this leads to creation, this is the only one of these
# methods that might care about "parent" data.
return self.resource_from_data(self.fk_resource, value, **kwargs)
elif hasattr(value, 'pk'):
# We've got an object with a primary key.
return self.resource_from_pk(self.fk_resource, value, **kwargs)
else:
raise ApiFieldError("The '%s' field was given data that was not a URI, not a dictionary-alike and does not have a 'pk' attribute: %s." % (self.instance_name, value))
def patch_tastypie():
from tastypie.fields import ToManyField, RelatedField
from tastypie.resources import Resource, ResourceOptions
from monkey.tastypie import dehydrate, _build_reverse_url, get_via_uri, build_related_resource
setattr(ToManyField, 'dehydrate', dehydrate)
setattr(Resource, '_build_reverse_url', _build_reverse_url)
setattr(Resource, 'get_via_uri', get_via_uri)
setattr(ResourceOptions, 'include_resource_uri', False)
setattr(RelatedField, 'build_related_resource', build_related_resource)
它并不完美,但加速from __future__ import absolute_import
from .monkey import patch_tastypie
patch_tastypie()
10-20%