Skip to content

Commit

Permalink
DB: create an index for builds_build table to improve Addons API (#…
Browse files Browse the repository at this point in the history
…10840)

* DB: create an index for `builds_build` table to improve Addons API

After doing some research on #10831, I think that adding an index to this table
will help a lot to improve queries performance on Addons API.

This PR also fixes the `version.builds.last()` query to get the `Build` object
that is finished and successfull. Otherwise, we will be returning a build that
does not match with the HTML on S3.

I think we should close #10831 and revert the commit on `rel*` branches.

* Update readthedocs/proxito/views/hosting.py

* Update readthedocs/proxito/views/hosting.py
  • Loading branch information
humitos authored Oct 23, 2023
1 parent b5e4d99 commit 687f96e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
20 changes: 20 additions & 0 deletions readthedocs/builds/migrations/0054_add_builds_index_for_addons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.2.6 on 2023-10-18 11:43

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("builds", "0053_alter_version_build_data"),
]

operations = [
migrations.AlterIndexTogether(
name="build",
index_together={
("date", "id"),
("version", "state", "type"),
("version", "state", "date", "success"),
},
),
]
7 changes: 5 additions & 2 deletions readthedocs/builds/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,8 +847,11 @@ class Meta:
ordering = ['-date']
get_latest_by = 'date'
index_together = [
['version', 'state', 'type'],
['date', 'id'],
# Useful for `/_/addons/` API endpoint.
# Query: ``version.builds.filter(success=True, state=BUILD_STATE_FINISHED)``
["version", "state", "date", "success"],
["version", "state", "type"],
["date", "id"],
]
indexes = [
models.Index(fields=['project', 'date']),
Expand Down
10 changes: 8 additions & 2 deletions readthedocs/proxito/views/hosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
ProjectSerializer,
VersionSerializer,
)
from readthedocs.builds.constants import EXTERNAL, LATEST
from readthedocs.builds.constants import BUILD_STATE_FINISHED, EXTERNAL, LATEST
from readthedocs.builds.models import Version
from readthedocs.core.resolver import resolver
from readthedocs.core.unresolver import UnresolverError, unresolver
Expand Down Expand Up @@ -77,7 +77,13 @@ def _resolve_resources(self):
project = unresolved_url.project
version = unresolved_url.version
filename = unresolved_url.filename
build = version.builds.last()
# This query should use a particular index:
# ``builds_build_version_id_state_date_success_12dfb214_idx``.
# Otherwise, if the index is not used, the query gets too slow.
build = version.builds.filter(
success=True,
state=BUILD_STATE_FINISHED,
).last()

except UnresolverError as exc:
# If an exception is raised and there is a ``project`` in the
Expand Down

0 comments on commit 687f96e

Please sign in to comment.