Skip to content

Commit

Permalink
perf: Speed up create_hogql_database (#27451)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
timgl and github-actions[bot] authored Jan 15, 2025
1 parent 0ac0d84 commit 97af0ac
Show file tree
Hide file tree
Showing 4 changed files with 2,271 additions and 2,270 deletions.
3 changes: 3 additions & 0 deletions posthog/hogql/database/schema/channel_type.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from dataclasses import dataclass
from typing import Optional, Union
from datetime import timedelta
from posthog.cache_utils import cache_for

from posthog.hogql import ast
from posthog.hogql.database.models import ExpressionField
Expand Down Expand Up @@ -60,6 +62,7 @@ def create_initial_domain_type(name: str):
)


@cache_for(timedelta(minutes=30))
def create_initial_channel_type(name: str, custom_rules: Optional[list[CustomChannelRule]] = None):
return ExpressionField(
name=name,
Expand Down
5 changes: 3 additions & 2 deletions posthog/hogql/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ def prepare_ast_for_printing(
stack: Optional[list[ast.SelectQuery]] = None,
settings: Optional[HogQLGlobalSettings] = None,
) -> _T_AST | None:
with context.timings.measure("create_hogql_database"):
context.database = context.database or create_hogql_database(context.team_id, context.modifiers, context.team)
if context.database is None:
with context.timings.measure("create_hogql_database"):
context.database = create_hogql_database(context.team_id, context.modifiers, context.team)

context.modifiers = set_default_in_cohort_via(context.modifiers)

Expand Down
3 changes: 3 additions & 0 deletions posthog/hogql/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from posthog.hogql.resolver_utils import extract_select_queries
from posthog.models.team import Team
from posthog.clickhouse.query_tagging import tag_queries
from posthog.hogql.database.database import create_hogql_database
from posthog.client import sync_execute
from posthog.schema import (
HogQLQueryResponse,
Expand Down Expand Up @@ -56,6 +57,8 @@ def execute_hogql_query(

if context is None:
context = HogQLContext(team_id=team.pk)
with context.timings.measure("create_hogql_database"):
context.database = create_hogql_database(team.pk, modifiers, team_arg=team)

query_modifiers = create_default_modifiers_for_team(team, modifiers)
debug = modifiers is not None and modifiers.debug
Expand Down
Loading

0 comments on commit 97af0ac

Please sign in to comment.