0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-28 09:16:49 +01:00
posthog/latest_migrations.manifest
Karl-Aksel Puulmann e03477b7d4
Index session recording events by team_id, timestamp (#2853)
* Index session recording events by team_id, timestamp

This speeds main session recording query up significantly. Solves #2739

----

Measurements

Tested this on a semi-large self-hosted instance.

Pre-index:

```
| QUERY PLAN                                                                                                                                                                                                    |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Subquery Scan on p  (cost=207673.55..207673.60 rows=1 width=97) (actual time=22200.985..22200.985 rows=0 loops=1)                                                                                             |
|   ->  GroupAggregate  (cost=207673.55..207673.59 rows=1 width=105) (actual time=22200.984..22200.984 rows=0 loops=1)                                                                                          |
|         Group Key: posthog_sessionrecordingevent.distinct_id, posthog_sessionrecordingevent.session_id                                                                                                        |
|         Filter: (count(*) FILTER (WHERE ((posthog_sessionrecordingevent.snapshot_data ->> 'type'::text) = '2'::text)) > 0)                                                                                    |
|         Rows Removed by Filter: 20                                                                                                                                                                            |
|         ->  Sort  (cost=207673.55..207673.56 rows=1 width=468) (actual time=22185.713..22186.010 rows=4937 loops=1)                                                                                           |
|               Sort Key: posthog_sessionrecordingevent.distinct_id, posthog_sessionrecordingevent.session_id                                                                                                   |
|               Sort Method: quicksort  Memory: 2932kB                                                                                                                                                          |
|               ->  Index Scan using posthog_ses_team_id_46392f_idx21 on posthog_sessionrecordingevent  (cost=0.56..207673.54 rows=1 width=468) (actual time=127.291..22181.190 rows=4937 loops=1)              |
|                     Index Cond: ((team_id = 1) AND ("timestamp" >= '2020-12-01 10:37:21.726776+00'::timestamp with time zone) AND ("timestamp" <= '2021-01-05 11:30:47.010794+00'::timestamp with time zone)) |
| Planning Time: 0.195 ms                                                                                                                                                                                       |
| Execution Time: 22201.031 ms                                                                                                                                                                                  |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
```

After index:
```
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| QUERY PLAN                                                                                                                                                                                                    |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Subquery Scan on p  (cost=8.46..8.51 rows=1 width=97) (actual time=20.420..20.420 rows=0 loops=1)                                                                                                             |
|   ->  GroupAggregate  (cost=8.46..8.50 rows=1 width=105) (actual time=20.419..20.419 rows=0 loops=1)                                                                                                          |
|         Group Key: posthog_sessionrecordingevent.distinct_id, posthog_sessionrecordingevent.session_id                                                                                                        |
|         Filter: (count(*) FILTER (WHERE ((posthog_sessionrecordingevent.snapshot_data ->> 'type'::text) = '2'::text)) > 0)                                                                                    |
|         Rows Removed by Filter: 20                                                                                                                                                                            |
|         ->  Sort  (cost=8.46..8.47 rows=1 width=468) (actual time=13.695..14.078 rows=4937 loops=1)                                                                                                           |
|               Sort Key: posthog_sessionrecordingevent.distinct_id, posthog_sessionrecordingevent.session_id                                                                                                   |
|               Sort Method: quicksort  Memory: 2921kB                                                                                                                                                          |
|               ->  Index Scan using posthog_ses_team_id_46392f_idx24 on posthog_sessionrecordingevent  (cost=0.43..8.45 rows=1 width=468) (actual time=0.020..2.295 rows=4937 loops=1)                         |
|                     Index Cond: ((team_id = 1) AND ("timestamp" >= '2020-12-01 10:37:21.726776+00'::timestamp with time zone) AND ("timestamp" <= '2021-01-05 11:30:47.010794+00'::timestamp with time zone)) |
| Planning Time: 15.432 ms                                                                                                                                                                                      |
| Execution Time: 22.521 ms                                                                                                                                                                                     |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
```

* make linter happy
2021-01-05 17:10:21 +02:00

10 lines
328 B
Plaintext

admin: 0003_logentry_add_action_flag_choices
auth: 0011_update_proxy_permissions
axes: 0006_remove_accesslog_trusted
contenttypes: 0002_remove_content_type_name
ee: 0002_hook
posthog: 0110_sessionrecordingeventbyteamandtimestamp
rest_hooks: 0002_swappable_hook_model
sessions: 0001_initial
social_django: 0008_partial_timestamp