我希望每次数据库搜索都能访问数据库。
用户可以查询数据库,然后对结果进行分页和排序。
数据库包含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 MS:
SELECT
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
)在哪里companies_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)GROUP BYcompanies_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
订购 BYcompanies_company
。sector
ASC LIMIT 20 OFFSET 160
11142 MS:
SELECT
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
)在哪里companies_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)GROUP BYcompanies_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
订购 BYcompanies_company
。sector
ASC LIMIT 20 OFFSET 340
答案 0 :(得分:0)
对于700条记录,这样一个简单的查询不应该花费几个ms。 请告诉我们两个请求生成的原始SQL查询(页面== 1和页面> 1)及其时间。
您的数据库索引是否正常?您是否已使用数据库客户端检查了执行两个原始SQL查询的时间?