diff --git a/backend/src/baserow/contrib/database/views/models.py b/backend/src/baserow/contrib/database/views/models.py index 5cc8d46524..072ec08dbc 100644 --- a/backend/src/baserow/contrib/database/views/models.py +++ b/backend/src/baserow/contrib/database/views/models.py @@ -9,7 +9,7 @@ from django.contrib.contenttypes.models import ContentType from django.contrib.postgres.fields import ArrayField from django.db import models from django.db.models import Q -from django.db.models.query import Prefetch +from django.db.models.query import Prefetch, prefetch_related_objects from django.utils.functional import lazy from baserow.contrib.database.fields.field_filters import ( @@ -339,14 +339,29 @@ class View( # in-efficient because this rarely happens. The most important part # is that the check is fast. existing_field_ids = [options.field_id for options in existing_field_options] - new_field_options = through_model.objects.bulk_create( - [ - view_type.prepare_field_options(self, field.id) - for field in fields - if field.id not in existing_field_ids - ], - ignore_conflicts=True, + + # `prepare_field_options` reads all the field options of the view for every + # missing field. Nothing is created until the end, so fetch them once. + related_name = f"{through_model._meta.model_name}_set" + already_prefetched = related_name in getattr( + self, "_prefetched_objects_cache", {} ) + if not already_prefetched: + prefetch_related_objects([self], related_name) + + try: + new_field_options = through_model.objects.bulk_create( + [ + view_type.prepare_field_options(self, field.id) + for field in fields + if field.id not in existing_field_ids + ], + ignore_conflicts=True, + ) + finally: + # The fetched options don't include the new ones anymore. + if not already_prefetched: + self._prefetched_objects_cache.pop(related_name, None) return new_field_options diff --git a/backend/tests/baserow/contrib/database/view/test_view_models.py b/backend/tests/baserow/contrib/database/view/test_view_models.py index d6aa9023ef..027bd4c28b 100644 --- a/backend/tests/baserow/contrib/database/view/test_view_models.py +++ b/backend/tests/baserow/contrib/database/view/test_view_models.py @@ -1,6 +1,7 @@ from unittest.mock import patch -from django.db import IntegrityError +from django.db import IntegrityError, connection +from django.test.utils import CaptureQueriesContext import pytest @@ -415,3 +416,27 @@ def test_migration_remove_stale_fieldoptions( # Only the stale field option should be deleted. assert GridViewFieldOptions.objects.filter(grid_view=grid_view).count() == 1 + + +@pytest.mark.django_db +def test_creating_missing_field_options_does_not_query_per_field(data_fixture): + def create_missing(num_fields): + table = data_fixture.create_database_table() + hidden_field = data_fixture.create_text_field(table=table) + grid_view = data_fixture.create_grid_view(table=table) + data_fixture.create_grid_view_field_option(grid_view, hidden_field, hidden=True) + for _ in range(num_fields): + data_fixture.create_text_field(table=table) + + with CaptureQueriesContext(connection) as captured: + field_options = grid_view.get_field_options(create_if_missing=True) + return grid_view, field_options, len(captured) + + _, _, few_fields_num_queries = create_missing(2) + grid_view, field_options, many_fields_num_queries = create_missing(6) + + assert many_fields_num_queries == few_fields_num_queries + # New options are hidden because another field is hidden in the view. + assert [o.hidden for o in field_options] == [True] * 7 + # The options fetched while creating them aren't kept on the view. + assert len(grid_view.gridviewfieldoptions_set.all()) == 7 diff --git a/changelog/entries/unreleased/bug/speeds_up_opening_a_view_after_fields_were_added.json b/changelog/entries/unreleased/bug/speeds_up_opening_a_view_after_fields_were_added.json new file mode 100644 index 0000000000..e791039ade --- /dev/null +++ b/changelog/entries/unreleased/bug/speeds_up_opening_a_view_after_fields_were_added.json @@ -0,0 +1,9 @@ +{ + "type": "bug", + "message": "Speeds up opening a view for the first time after fields were added", + "issue_origin": "github", + "issue_number": null, + "domain": "database", + "bullet_points": [], + "created_at": "2026-12-01" +}