0

我想在每次数据库搜索时访问一次数据库。

用户可以查询数据库,然后对结果进行分页和排序。

该数据库包含 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_companyid, 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) AS num_usersFROM companies_company LEFT OUTER JOIN companies_selectedusercompanies ON ( companies_company. id= companies_selectedusercompanies. company_id) WHERE companies_company. idIN (SELECT U0. idFROM companies_company U0 LEFT OUTER JOIN companies_selectedusercompaniesU1 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_dateORDER BY NULL) 分组依据 companies_companyid, 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_companysectorASC 限制 20 偏移 160

11142 女士:

选择companies_companyid, 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) AS num_usersFROM companies_companyLEFT OUTER JOIN companies_selectedusercompanies ON ( companies_company. id= companies_selectedusercompanies. company_id) WHERE companies_company. idIN (SELECT U0. idFROM companies_company U0 LEFT OUTER JOIN companies_selectedusercompaniesU1 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_dateORDER BY NULL) 分组依据 companies_companyid, 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_companysectorASC 限制 20 偏移 340

4

2 回答 2

0

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

你的数据库索引好吗?您是否检查过使用数据库客户端执行两个原始 sql 查询的时间?

于 2013-01-06T15:56:03.443 回答
0

似乎这是一个已知问题。我将采用 Facebook 路线,仅显示下一页和上一页。

于 2013-01-07T08:56:18.287 回答