django paginate并对大型查询集进行排序

时间:2013-01-06 13:08:24

标签: 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_companyidcompanies_companyname,         companies_companysectorcompanies_companysub_sectorcompanies_companynum_of_employeescompanies_companyaddress,     companies_companyhome_pagecompanies_companycareer_url,     companies_companycitycompanies_companyupdating_date,     COUNT(companies_selectedusercompaniesuser_id)AS num_users FROM     companies_company LEFT OUTER JOIN companies_selectedusercompanies     ON(companies_companyid =     companies_selectedusercompaniescompany_id)在哪里     companies_companyid IN(SELECT U0。id FROM companies_company     U0 LEFT OUTER JOIN companies_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 BY     companies_companyidcompanies_companyname,     companies_companysectorcompanies_companysub_sector,     companies_companynum_of_employeescompanies_companyaddress,     companies_companyhome_pagecompanies_companycareer_url,     companies_companycitycompanies_companyupdating_date订购     BY companies_companysector ASC LIMIT 20 OFFSET 160

  

11142 MS:

  

SELECT companies_companyidcompanies_companyname,   companies_companysectorcompanies_companysub_sector,   companies_companynum_of_employeescompanies_companyaddress,   companies_companyhome_pagecompanies_companycareer_url,   companies_companycitycompanies_companyupdating_date,   COUNT(companies_selectedusercompaniesuser_id)AS num_users FROM   companies_company LEFT OUTER JOIN companies_selectedusercompanies   ON(companies_companyid =   companies_selectedusercompaniescompany_id)在哪里   companies_companyid IN(SELECT U0。id FROM companies_company   U0 LEFT OUTER JOIN companies_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 BY   companies_companyidcompanies_companyname,   companies_companysectorcompanies_companysub_sector,   companies_companynum_of_employeescompanies_companyaddress,   companies_companyhome_pagecompanies_companycareer_url,   companies_companycitycompanies_companyupdating_date订购   BY companies_companysector ASC LIMIT 20 OFFSET 340

1 个答案:

答案 0 :(得分:0)

对于700条记录,这样一个简单的查询不应该花费几个ms。 请告诉我们两个请求生成的原始SQL查询(页面== 1和页面> 1)及其时间。

您的数据库索引是否正常?您是否已使用数据库客户端检查了执行两个原始SQL查询的时间?