-
-
Notifications
You must be signed in to change notification settings - Fork 99
Reduce query counts #810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Reduce query counts #810
Changes from all commits
4cfad1e
d16a4f7
6b02d66
f09da91
6b32ad3
9ee498c
c5b17e4
8f7f842
63d7e03
107a8f0
df5c2be
15a95a1
eddce88
b83d928
ed6881d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # Generated by Django 4.2.29 on 2026-04-20 15:06 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('reports', '0005_alter_report_options'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AlterModelOptions( | ||
| name='report', | ||
| options={'ordering': ['-created'], 'verbose_name': 'Report', 'verbose_name_plural': 'Reports'}, | ||
| ), | ||
| migrations.AlterField( | ||
| model_name='report', | ||
| name='created', | ||
| field=models.DateTimeField(auto_now_add=True, db_index=True), | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,7 @@ | |
| # along with Patchman. If not, see <http://www.gnu.org/licenses/> | ||
|
|
||
| from django.contrib.auth.decorators import login_required | ||
| from django.db.models import Q | ||
| from django.db.models import Count, Q | ||
| from django.shortcuts import get_object_or_404, render | ||
| from django_tables2 import RequestConfig | ||
| from rest_framework import viewsets | ||
|
|
@@ -32,7 +32,7 @@ | |
|
|
||
| @login_required | ||
| def cwe_list(request): | ||
| cwes = CWE.objects.all() | ||
| cwes = CWE.objects.all().annotate(cve_count=Count('cve', distinct=True)).order_by('cwe_id') | ||
|
|
||
| if 'search' in request.GET: | ||
| terms = request.GET['search'].lower() | ||
|
|
@@ -65,7 +65,9 @@ def cwe_detail(request, cwe_id): | |
|
|
||
| @login_required | ||
| def cve_list(request): | ||
| cves = CVE.objects.all() | ||
| cves = CVE.objects.all() \ | ||
| .prefetch_related('cvss_scores', 'cwes', 'erratum_set') \ | ||
| .order_by('-cve_id') | ||
|
|
||
| if 'erratum_id' in request.GET: | ||
| cves = cves.filter(erratum=request.GET['erratum_id']) | ||
|
|
@@ -117,10 +119,10 @@ def cve_detail(request, cve_id): | |
|
|
||
| @login_required | ||
| def reference_list(request): | ||
| refs = Reference.objects.all().order_by('ref_type') | ||
| refs = Reference.objects.all().prefetch_related('erratum_set').order_by('ref_type') | ||
|
|
||
| if 'ref_type' in request.GET: | ||
| refs = refs.filter(ref_type=request.GET['ref_type']).distinct() | ||
| refs = refs.filter(ref_type=request.GET['ref_type']) | ||
|
|
||
| if 'erratum_id' in request.GET: | ||
| refs = refs.filter(erratum__id=request.GET['erratum_id']) | ||
|
|
@@ -137,7 +139,7 @@ def reference_list(request): | |
|
|
||
| filter_list = [] | ||
| filter_list.append(Filter(request, 'Reference Type', 'ref_type', | ||
| Reference.objects.values_list('ref_type', flat=True).distinct())) | ||
| Reference.objects.values_list('ref_type', flat=True))) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or here?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, I was expecting distinct() here to be useful indeed. Since ref_type would be shared by many References with different urls. I'm not sure where it is deduplicated but doing it without distinct() saves quite a bit of time and the Filter by on the side does not show any duplicates. With .distinct() it takes 325ms: SELECT DISTINCT "security_reference"."ref_type",
"security_reference"."url"
FROM "security_reference"
ORDER BY "security_reference"."ref_type" ASC,
"security_reference"."url" ASCWithout .distinct() it's only 17ms: SELECT "security_reference"."ref_type"
FROM "security_reference"
ORDER BY "security_reference"."ref_type" ASC,
"security_reference"."url" ASCRunning it in ./manage.py dbshell will reveal it returns many more rows (ie. all of them); it is just computational cheaper it seems. I can also revert this if you want me to |
||
| filter_bar = FilterBar(request, filter_list) | ||
|
|
||
| table = ReferenceTable(refs) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also need to restore
distinct()here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty certain distinct() has no effect here in the resultset other than slowing down the query. 'ref_type' is a local field unlike the 'package__' filter in packages/views.py.