Skip to content
This repository has been archived by the owner on Dec 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #214 from OpenDataPolicingNC/TRH-2688-improve-sear…
Browse files Browse the repository at this point in the history
…ch-query-time

[TRH-2688] Improve Search Query Time
  • Loading branch information
mlavin committed Jan 15, 2018
2 parents 5629559 + 2811b06 commit ec6ef81
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions nc/data/copy.sql
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ CREATE INDEX nc_searchbasis_5fad4402 ON nc_searchbasis USING btree (search_id);
CREATE INDEX nc_searchbasis_91455da7 ON nc_searchbasis USING btree (stop_id);
CREATE INDEX nc_searchbasis_a8452ca7 ON nc_searchbasis USING btree (person_id);
CREATE INDEX nc_stop_169fc544 ON nc_stop USING btree (agency_id);
CREATE INDEX nc_stop_date_7d643c8a9c590bf7_uniq ON nc_stop USING btree (date);

ANALYZE;
COMMIT;
19 changes: 19 additions & 0 deletions nc/migrations/0003_auto_20180115_1141.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('nc', '0002_agency_census_profile_id'),
]

operations = [
migrations.AlterField(
model_name='stop',
name='date',
field=models.DateTimeField(db_index=True),
),
]
2 changes: 1 addition & 1 deletion nc/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Stop(CachingMixin, models.Model):
stop_id = models.PositiveIntegerField(primary_key=True)
agency_description = models.CharField(max_length=100)
agency = models.ForeignKey('Agency', null=True, related_name='stops')
date = models.DateTimeField()
date = models.DateTimeField(db_index=True)
purpose = models.PositiveSmallIntegerField(choices=PURPOSE_CHOICES)
action = models.PositiveSmallIntegerField(choices=ACTION_CHOICES)
driver_arrest = models.BooleanField(default=False)
Expand Down
11 changes: 11 additions & 0 deletions nc/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.db import connections
from django.shortcuts import render
from .models import Stop, Agency, Person
from . import forms
Expand Down Expand Up @@ -31,6 +32,16 @@ def search(request):
else:
people = Person.objects.none()
people = people.select_related('stop').order_by('stop__date')
connection = connections[people.db]
with connection.cursor() as cursor:
# Disable seq scanning when generating this count
# Override the count to produce this result for the pagination
# Might not be necessary if we adjusted the random_page_cost
# See https://stackoverflow.com/questions/10643215/
cursor.execute('SET enable_seqscan = OFF;')
total = people.count()
people.count = lambda: total
cursor.execute('SET enable_seqscan = ON;')
context = {
'form': form,
'people': people,
Expand Down

0 comments on commit ec6ef81

Please sign in to comment.