我想在每個數據庫搜索中擊中DB一次。django分頁並對大型查詢集進行排序
用戶可以查詢數據庫,然後對結果進行分頁和排序。
該數據庫包含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
)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)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
ORDER 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
)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)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
ORDER BYcompanies_company
。sector
ASC LIMIT 20 OFFSET 340
如果您的數據庫只有700個項目,並且每次查詢頁面時都需要很長時間才能查詢,那麼查詢就會出現嚴重錯誤。 –
結果可以在幾秒鐘內返回,幾秒鐘後返回。 我可以;噸數是什麼使這種變化。 – DjangoPy
這種情況發生在我分頁到頁面= 1時。 queryset相等。 不同的是:paginator。頁面(頁) – DjangoPy