Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Don't use uuid field to count #23049

Merged
merged 17 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions posthog/hogql_queries/insights/trends/aggregation_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from posthog.hogql_queries.utils.query_date_range import QueryDateRange
from posthog.models.team.team import Team
from posthog.schema import BaseMathType, ChartDisplayType, EventsNode, ActionsNode, DataWarehouseNode
from posthog.models.filters.mixins.utils import cached_property
from posthog.hogql_queries.insights.data_warehouse_mixin import DataWarehouseInsightQueryMixin


Expand Down Expand Up @@ -71,18 +70,11 @@ def __init__(
self.query_date_range = query_date_range
self.is_total_value = is_total_value

@cached_property
def _id_field(self) -> ast.Expr:
if isinstance(self.series, DataWarehouseNode):
return ast.Field(chain=["e", self.series.id_field])

return ast.Field(chain=["e", "uuid"])

def select_aggregation(self) -> ast.Expr:
if self.series.math == "hogql" and self.series.math_hogql is not None:
return parse_expr(self.series.math_hogql)
elif self.series.math == "total":
return parse_expr("count({id_field})", placeholders={"id_field": self._id_field})
return parse_expr("count()")
elif self.series.math == "dau":
actor = "e.distinct_id" if self.team.aggregate_users_by_distinct_id else "e.person_id"
return parse_expr(f"count(DISTINCT {actor})")
Expand Down Expand Up @@ -112,9 +104,7 @@ def select_aggregation(self) -> ast.Expr:
elif self.series.math == "p99":
return self._math_quantile(0.99, None)

return parse_expr(
"count({id_field})", placeholders={"id_field": self._id_field}
) # All "count per actor" get replaced during query orchestration
return parse_expr("count()") # All "count per actor" get replaced during query orchestration

def actor_id(self) -> ast.Expr:
if self.series.math == "unique_group" and self.series.math_group_type_index is not None:
Expand Down Expand Up @@ -367,23 +357,22 @@ def _events_query(
(
"""
SELECT
count({id_field}) AS total
count() AS total
FROM {table} AS e
WHERE {events_where_clause}
GROUP BY {person_field}
"""
if isinstance(self.series, DataWarehouseNode)
else """
SELECT
count({id_field}) AS total
count() AS total
FROM events AS e
SAMPLE {sample}
WHERE {events_where_clause}
GROUP BY {person_field}
"""
),
placeholders={
"id_field": self._id_field,
"table": self._table_expr,
"events_where_clause": where_clause_combined,
"sample": sample_value,
Expand Down
Loading
Loading