Skip to content

Commit

Permalink
perf: Add index for EventProperty.property (#22157)
Browse files Browse the repository at this point in the history
* perf: Add index for `EventProperty.property`

* Include `team_id` in index

* Simplify definitions SQL

This will make it simpler for Postgres to plan efficiently.

* Revert "Simplify definitions SQL"

We still need the `DISTINCT`. Hopefully Postgres still plans well.
  • Loading branch information
Twixes authored May 7, 2024
1 parent a266ffc commit f5342b7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion latest_migrations.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ contenttypes: 0002_remove_content_type_name
ee: 0016_rolemembership_organization_member
otp_static: 0002_throttling
otp_totp: 0002_auto_20190420_0723
posthog: 0410_action_steps_population
posthog: 0411_eventproperty_indexes
sessions: 0001_initial
social_django: 0010_uid_db_index
two_factor: 0007_auto_20201201_1019
2 changes: 1 addition & 1 deletion posthog/api/property_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def with_event_property_filter(
event_names = json.loads(event_names)

if event_names and len(event_names) > 0:
event_property_field = f"{self.posthog_eventproperty_table_join_alias}.property is not null"
event_property_field = f"{self.posthog_eventproperty_table_join_alias}.property IS NOT NULL"
event_name_join_filter = "AND event = ANY(%(event_names)s)"

return dataclasses.replace(
Expand Down
17 changes: 17 additions & 0 deletions posthog/migrations/0411_eventproperty_indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Created manually on 2024-04-10 18:46

from django.db import migrations


class Migration(migrations.Migration):
atomic = False
dependencies = [
("posthog", "0410_action_steps_population"),
]

operations = [
migrations.RunSQL(
"CREATE INDEX CONCURRENTLY posthog_eventproperty_team_id_and_property_r32khd9s ON posthog_eventproperty(team_id, property)",
reverse_sql='DROP INDEX "posthog_eventproperty_team_id_and_property_r32khd9s"',
),
]

0 comments on commit f5342b7

Please sign in to comment.