Skip to content

Commit

Permalink
refactor: Optimise count by using estimate
Browse files Browse the repository at this point in the history
  • Loading branch information
microamp committed Aug 14, 2024
1 parent 6247c1a commit 46b5d1d
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from urllib.parse import unquote_plus
import json

from django.db import connection
from django.db.models.query import QuerySet
from django.urls import reverse
from django.http import QueryDict
Expand Down Expand Up @@ -51,12 +52,22 @@
"""Shared context passed to GUI templates."""


def _count_estimate(table_name: str) -> int:
with connection.cursor() as cursor:
cursor.execute(
'SELECT reltuples FROM pg_class WHERE relname = %s',
[table_name],
)
row = cursor.fetchone()
return int(row[0])


def home(request):
"""Serves main landing page."""

metrics.gui_home_page_hits.inc()

total_indexed_citations = RefData.objects.count()
total_indexed_citations = _count_estimate('api_ref_data') # Use estimate
units = ('', 'k', 'M', 'G', 'T', 'P')
factor = 1000.0
magnitude = int(floor(log_(max(abs(total_indexed_citations), 1), factor)))
Expand Down

0 comments on commit 46b5d1d

Please sign in to comment.