diff --git a/silk/migrations/0008_sqlquery_query_structure.py b/silk/migrations/0008_sqlquery_query_structure.py new file mode 100644 index 00000000..32dd73a6 --- /dev/null +++ b/silk/migrations/0008_sqlquery_query_structure.py @@ -0,0 +1,19 @@ +# Generated by Django 3.1 on 2020-08-21 19:47 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('silk', '0007_sqlquery_identifier'), + ] + + operations = [ + migrations.AddField( + model_name='sqlquery', + name='query_structure', + field=models.TextField(default=''), + preserve_default=False, + ), + ] diff --git a/silk/models.py b/silk/models.py index f69c7d4d..01581b23 100644 --- a/silk/models.py +++ b/silk/models.py @@ -232,6 +232,7 @@ def bulk_create(self, *args, **kwargs): class SQLQuery(models.Model): query = TextField() + query_structure = TextField() start_time = DateTimeField(null=True, blank=True, default=timezone.now) end_time = DateTimeField(null=True, blank=True) time_taken = FloatField(blank=True, null=True) diff --git a/silk/sql.py b/silk/sql.py index 576762e5..8903063d 100644 --- a/silk/sql.py +++ b/silk/sql.py @@ -41,6 +41,7 @@ def execute_sql(self, *args, **kwargs): if _should_wrap(sql_query): query_dict = { 'query': sql_query, + 'query_structure': q, 'start_time': timezone.now(), 'traceback': tb } diff --git a/silk/templates/silk/sql.html b/silk/templates/silk/sql.html index 2b5b970d..48deb161 100644 --- a/silk/templates/silk/sql.html +++ b/silk/templates/silk/sql.html @@ -108,6 +108,7 @@ Tables Num. Joins Execution Time (ms) + Num. Duplicates {% for sql_query in items %} @@ -124,6 +125,7 @@ {{ sql_query.tables_involved|join:", " }} {{ sql_query.num_joins }} {{ sql_query.time_taken | floatformat:6 }} + {{ sql_query.num_duplicate }} {% endfor %} diff --git a/silk/views/sql.py b/silk/views/sql.py index 2d85f113..5705fc24 100644 --- a/silk/views/sql.py +++ b/silk/views/sql.py @@ -21,8 +21,15 @@ def get(self, request, *_, **kwargs): if request_id: silk_request = Request.objects.get(id=request_id) query_set = SQLQuery.objects.filter(request=silk_request).order_by('-start_time') + query_structure_count = {} for q in query_set: q.start_time_relative = q.start_time - silk_request.start_time + if q.query_structure not in query_structure_count: + query_structure_count[q.query_structure] = -1 + query_structure_count[q.query_structure] += 1 + for q in query_set: + q.num_duplicate = query_structure_count[q.query_structure] + page = _page(request, query_set) context['silk_request'] = silk_request if profile_id: