我想在每次数据库搜索时访问一次数据库。
用户可以查询数据库,然后对结果进行分页和排序。
该数据库包含 700 个项目。
django-debug-toolbar 显示有时查询时间太长。
目前我将初始搜索结果 id 保存在会话中,并使用它来构建一个查询集来对结果进行排序和分页。
我想避免这个数据库查询。
我正在考虑缓存查询集,以便其他用户使用缓存的查询集而不是访问数据库。
您如何看待这种方法?有什么我想念的吗?
companiesID = request.session['selectedCompaniesIDs']
companies = Company.objects.filter(pk__in=companiesID).order_by(type).annotate(num_users=Count('users'))
cd = create_pagination(companies, page)
PAGINATION_RESULTS_PER_PAGE = 20
def create_pagination(results, page):
paginator = Paginator(results, PAGINATION_RESULTS_PER_PAGE)
try:
res = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
res = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
res = paginator.page(paginator.num_pages)
return res
1 毫秒:
选择
companies_company
。id
,companies_company
.name
,companies_company
.sector
,companies_company
.sub_sector
,companies_company
.num_of_employees
,companies_company
.address
,companies_company
.home_page
,companies_company
.career_url
,companies_company
.city
,companies_company
.updating_date
, COUNT(companies_selectedusercompanies
.user_id
) ASnum_users
FROMcompanies_company
LEFT OUTER JOINcompanies_selectedusercompanies
ON (companies_company
.id
=companies_selectedusercompanies
.company_id
) WHEREcompanies_company
.id
IN (SELECT U0.id
FROMcompanies_company
U0 LEFT OUTER JOINcompanies_selectedusercompanies
U1 ON (U0.id
= U1.company_id
) GROUP BY U0.id
, U0.name
, U0.sector
, U0.sub_sector
, U0.num_of_employees
, U0.address
, U0.home_page
, U0.career_url
, U0.city
, U0。updating_date
ORDER BY NULL) 分组依据companies_company
。id
,companies_company
.name
,companies_company
.sector
,companies_company
.sub_sector
,companies_company
.num_of_employees
,companies_company
.address
,companies_company
.home_page
,companies_company
.career_url
,companies_company
.city
,companies_company
.updating_date
订购方式companies_company
。sector
ASC 限制 20 偏移 160
11142 女士:
选择
companies_company
。id
,companies_company
.name
,companies_company
.sector
,companies_company
.sub_sector
,companies_company
.num_of_employees
,companies_company
.address
,companies_company
.home_page
,companies_company
.career_url
,companies_company
.city
,companies_company
.updating_date
, COUNT(companies_selectedusercompanies
.user_id
) ASnum_users
FROMcompanies_company
LEFT OUTER JOINcompanies_selectedusercompanies
ON (companies_company
.id
=companies_selectedusercompanies
.company_id
) WHEREcompanies_company
.id
IN (SELECT U0.id
FROMcompanies_company
U0 LEFT OUTER JOINcompanies_selectedusercompanies
U1 ON (U0.id
= U1.company_id
) GROUP BY U0.id
, U0.name
, U0.sector
, U0.sub_sector
, U0.num_of_employees
, U0.address
, U0.home_page
, U0.career_url
, U0.city
, U0.updating_date
ORDER BY NULL) 分组依据companies_company
。id
,companies_company
.name
,companies_company
.sector
,companies_company
.sub_sector
,companies_company
.num_of_employees
,companies_company
.address
,companies_company
.home_page
,companies_company
.career_url
,companies_company
.city
,companies_company
.updating_date
订购方式companies_company
。sector
ASC 限制 20 偏移 340