Django优化大数据过滤代码
当我搜索 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是一个答案,而只是一个使您的代码更易于阅读和使用的提示:
This is not an answer, but just a tip to make your code easier to read and work with:
您绝对应该使用 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".