Django优化大数据过滤代码

发布于 2024-12-29 01:25:46 字数 1262 浏览 0 评论 0原文

当我搜索 15000 个结果时,有什么方法可以优化处理速度吗?

在我的视图中,我按如下方式过滤搜索:

if form.is_valid():

      results = Screening.objects.filter(
         screening_date__range(form.cleaned_data['start_date'],form.cleaned_data['end_date']))

      if form.cleaned_data['company']:
          results = results.filter(company=form.cleaned_data['company'])
      if form.cleaned_data['company_job']:
          results = results.filter(company_job__in=form.cleaned_data['company_job'])
      if form.cleaned_data['company_job_type']:
          results = results.filter(company_job_type=form.cleaned_data['company_job_type'])
      if form.cleaned_data['reason']:
          results = results.filter(reason_for_testing__in=form.cleaned_data['reason'])`

在 TEMPLATE 中,传递的结果用作:

{% for result in results %}

      <td>{{ result.company.name}}</td>
      <td>{{ result.first_name }} {{ result.last_name }}</td>
      <td>{{ result.company_job.job_number }}</td>
      <td>{{ result.id }}</td>
      <td>{{ result.screening_date|date}}</td></tr>

有什么方法可以优化处理,或者在这种情况下我应该使用缓存或其他东西吗?

As I am searching through 15000 results, is there any way to optimize the processing speed?

In my VIEW I'm filtering search as this:

if form.is_valid():

      results = Screening.objects.filter(
         screening_date__range(form.cleaned_data['start_date'],form.cleaned_data['end_date']))

      if form.cleaned_data['company']:
          results = results.filter(company=form.cleaned_data['company'])
      if form.cleaned_data['company_job']:
          results = results.filter(company_job__in=form.cleaned_data['company_job'])
      if form.cleaned_data['company_job_type']:
          results = results.filter(company_job_type=form.cleaned_data['company_job_type'])
      if form.cleaned_data['reason']:
          results = results.filter(reason_for_testing__in=form.cleaned_data['reason'])`

And in TEMPLATE, the passed result is used as:

{% for result in results %}

      <td>{{ result.company.name}}</td>
      <td>{{ result.first_name }} {{ result.last_name }}</td>
      <td>{{ result.company_job.job_number }}</td>
      <td>{{ result.id }}</td>
      <td>{{ result.screening_date|date}}</td></tr>

Is there any way to optimize the processing or should I use cache or sth in this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你在看孤独的风景 2025-01-05 01:25:46

这不是一个答案,而只是一个使您的代码更易于阅读和使用的提示:

  filters = {
      'screening_date__range': (form.cleaned_data['start_date'],form.cleaned_data['end_date'])
  }

  if form.cleaned_data['company']:
      filters['company'] = form.cleaned_data['company']
  if form.cleaned_data['company_job']:
      filters['company_job__in'] = form.cleaned_data['company_job']
  if form.cleaned_data['company_job_type']:
      filters['company_job_type'] = form.cleaned_data['company_job_type']
  if form.cleaned_data['reason']:
      filters['reason_for_testing__in'] = form.cleaned_data['reason']

  Screening.objects.filter(**filters)

This is not an answer, but just a tip to make your code easier to read and work with:

  filters = {
      'screening_date__range': (form.cleaned_data['start_date'],form.cleaned_data['end_date'])
  }

  if form.cleaned_data['company']:
      filters['company'] = form.cleaned_data['company']
  if form.cleaned_data['company_job']:
      filters['company_job__in'] = form.cleaned_data['company_job']
  if form.cleaned_data['company_job_type']:
      filters['company_job_type'] = form.cleaned_data['company_job_type']
  if form.cleaned_data['reason']:
      filters['reason_for_testing__in'] = form.cleaned_data['reason']

  Screening.objects.filter(**filters)
够运 2025-01-05 01:25:46

您绝对应该使用 select_lated 来创建一个数据库击中(不是 N)。

然后分析代码,找到瓶颈并尝试优化它,而不是“一般优化”。

You should definitely use select_related to make ony one DB hit (not N).

Then profile the code, find the bottleneck and try to optimize it, not "optimize in general".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文