Identify and eliminate redundant expensive operations by implementing caching, memoization, or conditional execution. Look for repeated database queries, complex calculations, object creation, or data processing that can be cached or avoided entirely when not needed.

Common patterns to optimize:

Example of query optimization:

# Before: Duplicated query logic
if cache_enabled:
    tables = list(
        DataWarehouseTable.objects.filter(team_id=team.pk)
        .exclude(deleted=True)
        .select_related("credential", "external_data_source")
        .fetch_cached(team_id=team_id or team.pk, key_prefix=CACHE_KEY_PREFIX)
    )
else:
    tables = list(
        DataWarehouseTable.objects.filter(team_id=team.pk)
        .exclude(deleted=True)
        .select_related("credential", "external_data_source")
    )

# After: Extract common logic, apply caching conditionally
tables = DataWarehouseTable.objects.filter(team_id=team.pk)
    .exclude(deleted=True)
    .select_related("credential", "external_data_source")
if cache_enabled:
    tables = tables.fetch_cached(team_id=team_id or team.pk, key_prefix=CACHE_KEY_PREFIX)

Example of lookup caching:

# Add caching to avoid O(n²) complexity
def _get_all_loaded_insight_ids(self) -> set[int]:
    """Get all insight IDs from loaded pages."""
    if not hasattr(self, '_cached_insight_ids'):
        all_ids = set()
        for page_insights in self._loaded_pages.values():
            for insight in page_insights:
                all_ids.add(insight.id)
        self._cached_insight_ids = all_ids
    return self._cached_insight_ids