This repository has been archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Backport 5.1] internal/event_logs: optimize ping cte (#55714)
The aggregatedCodyUsageEventsQuery is timing out for dotcom. These changes are cte optimization steps. ## Test plan Extract query from most expensive ping-supporting query, [aggregatedCodyUsageEventsQuery](https://sourcegraph.sourcegraph.com/github.com/sourcegraph/sourcegraph/-/blob/internal/database/event_logs.go?L1594), analyze original query: ``` GroupAggregate (cost=143042.53..143382.23 rows=270 width=158) (actual time=492.653..608.924 rows=87 loops=1) Group Key: event_logs.name, (date_trunc('month'::text, '2023-08-01 17:00:00-07'::timestamp with time zone)), ((date_trunc('week'::text, ('2023-08-01 17:00:00-07'::timestamp with time zone + '1 day'::interval)) - '1 day'::interval)), (date_trunc('day'::text, '2023-08-01 17:00:00-07'::timestamp with time zone)) CTE code_generation_keys -> Function Scan on unnest key (cost=0.00..0.09 rows=9 width=32) (actual time=0.005..0.007 rows=9 loops=1) CTE explanation_keys -> Function Scan on unnest key_1 (cost=0.00..0.05 rows=5 width=32) (actual time=0.012..0.013 rows=5 loops=1) -> Sort (cost=143042.38..143046.21 rows=1530 width=62) (actual time=492.526..496.803 rows=33511 loops=1) Sort Key: event_logs.name Sort Method: external merge Disk: 3056kB -> Gather (cost=1000.16..142961.45 rows=1530 width=62) (actual time=12.381..418.370 rows=33511 loops=1) Workers Planned: 2 Workers Launched: 2 -> Parallel Seq Scan on event_logs (cost=0.17..141808.45 rows=638 width=62) (actual time=10.540..417.177 rows=11170 loops=3) Filter: ((name !~~ '%completion:suggested%'::text) AND (name !~~ '%completion:started%'::text) AND (name !~~ '%CTA%'::text) AND (name !~~ '%Cta%'::text) AND (NOT (hashed SubPlan 9)) AND (lower(name) ~~ '%cody%'::text) AND ("timestamp" >= (date_trunc('month'::text, '2023-08-01 17:00:00-07'::timestamp with time zone) - '1 mon'::interval))) Rows Removed by Filter: 382174 SubPlan 9 -> Function Scan on unnest (cost=0.00..0.13 rows=13 width=32) (actual time=0.007..0.008 rows=13 loops=3) SubPlan 8 -> CTE Scan on explanation_keys explanation_keys_3 (cost=0.00..0.10 rows=5 width=32) (actual time=0.000..0.001 rows=5 loops=1) SubPlan 7 -> CTE Scan on explanation_keys explanation_keys_2 (cost=0.00..0.10 rows=5 width=32) (actual time=0.014..0.016 rows=5 loops=1) SubPlan 6 -> CTE Scan on explanation_keys explanation_keys_1 (cost=0.00..0.10 rows=5 width=32) (actual time=0.000..0.001 rows=5 loops=1) SubPlan 5 -> CTE Scan on code_generation_keys code_generation_keys_1 (cost=0.00..0.18 rows=9 width=32) (actual time=0.000..0.002 rows=9 loops=1) SubPlan 4 -> CTE Scan on explanation_keys (cost=0.00..0.10 rows=5 width=32) (actual time=0.000..0.001 rows=5 loops=1) SubPlan 3 -> CTE Scan on code_generation_keys (cost=0.00..0.18 rows=9 width=32) (actual time=0.007..0.010 rows=9 loops=1) Planning Time: 1.556 ms Execution Time: 610.143 ms ``` Add indexes and analyze again in comments. <br> Backport 41e5cf3 from #55538 --------- Co-authored-by: Nathan Downs <[email protected]>
- Loading branch information
1 parent
47f426c
commit 72d8422
Showing
10 changed files
with
244 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
migrations/frontend/1691043630_event_logs_indexing/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
DROP INDEX IF EXISTS event_logs_name_is_cody_explanation_event; | ||
|
||
DROP INDEX IF EXISTS event_logs_name_is_cody_generation_event; | ||
|
||
DROP INDEX IF EXISTS event_logs_name_is_cody_active_event; | ||
|
||
DROP FUNCTION IF EXISTS isCodyGenerationEvent(name text); | ||
|
||
DROP FUNCTION IF EXISTS isCodyExplanationEvent(name text); | ||
|
||
DROP FUNCTION IF EXISTS isCodyActiveEvent(name text); |
2 changes: 2 additions & 0 deletions
2
migrations/frontend/1691043630_event_logs_indexing/metadata.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
name: event_logs_indexing | ||
parents: [1690323910] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
CREATE OR REPLACE FUNCTION iscodyactiveevent(name text) RETURNS boolean | ||
LANGUAGE plpgsql IMMUTABLE | ||
AS $$ | ||
BEGIN | ||
RETURN | ||
(name LIKE '%%cody%%' OR name LIKE '%%Cody%%') | ||
AND name NOT IN ( | ||
'%completion:started%', | ||
'%completion:suggested%', | ||
'%cta%', | ||
'%Cta%', | ||
'CodyVSCodeExtension:CodySavedLogin:executed', | ||
'web:codyChat:tryOnPublicCode', | ||
'web:codyEditorWidget:viewed', | ||
'web:codyChat:pageViewed', | ||
'CodyConfigurationPageViewed', | ||
'ClickedOnTryCodySearchCTA', | ||
'TryCodyWebOnboardingDisplayed', | ||
'AboutGetCodyPopover', | ||
'TryCodyWeb', | ||
'CodySurveyToastViewed', | ||
'SiteAdminCodyPageViewed', | ||
'CodyUninstalled', | ||
'SpeakToACodyEngineerCTA' | ||
); | ||
END; | ||
$$; | ||
|
||
CREATE OR REPLACE FUNCTION iscodyexplanationevent(name text) RETURNS boolean | ||
LANGUAGE plpgsql IMMUTABLE | ||
AS $$ | ||
BEGIN | ||
RETURN name = ANY(ARRAY[ | ||
'CodyVSCodeExtension:recipe:explain-code-high-level:executed', | ||
'CodyVSCodeExtension:recipe:explain-code-detailed:executed', | ||
'CodyVSCodeExtension:recipe:find-code-smells:executed', | ||
'CodyVSCodeExtension:recipe:git-history:executed', | ||
'CodyVSCodeExtension:recipe:rate-code:executed' | ||
]); | ||
END; | ||
$$; | ||
|
||
CREATE OR REPLACE FUNCTION iscodygenerationevent(name text) RETURNS boolean | ||
LANGUAGE plpgsql IMMUTABLE | ||
AS $$ | ||
BEGIN | ||
RETURN name = ANY(ARRAY[ | ||
'CodyVSCodeExtension:recipe:rewrite-to-functional:executed', | ||
'CodyVSCodeExtension:recipe:improve-variable-names:executed', | ||
'CodyVSCodeExtension:recipe:replace:executed', | ||
'CodyVSCodeExtension:recipe:generate-docstring:executed', | ||
'CodyVSCodeExtension:recipe:generate-unit-test:executed', | ||
'CodyVSCodeExtension:recipe:rewrite-functional:executed', | ||
'CodyVSCodeExtension:recipe:code-refactor:executed', | ||
'CodyVSCodeExtension:recipe:fixup:executed', | ||
'CodyVSCodeExtension:recipe:translate-to-language:executed' | ||
]); | ||
END; | ||
$$; | ||
|
||
CREATE INDEX IF NOT EXISTS event_logs_name_is_cody_explanation_event ON event_logs USING btree (iscodyexplanationevent(name)); | ||
|
||
CREATE INDEX IF NOT EXISTS event_logs_name_is_cody_generation_event ON event_logs USING btree (iscodygenerationevent(name)); | ||
|
||
CREATE INDEX IF NOT EXISTS event_logs_name_is_cody_active_event ON event_logs USING btree (iscodyactiveevent(name)); |
Oops, something went wrong.