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

[front] Split query to fetch agent_configurations #8078

Merged
merged 2 commits into from
Oct 17, 2024
Merged

[front] Split query to fetch agent_configurations #8078

merged 2 commits into from
Oct 17, 2024

Conversation

tdraier
Copy link
Contributor

@tdraier tdraier commented Oct 16, 2024

Description

followup #8050

Retrieving all assistants is very slow because of a complex query plan, due to an "or" in the query. Splitting in 2 queries which only have an index scan may be more efficient (especially in terms of memory)

Current query :

EXPLAIN SELECT * FROM "agent_configurations" AS "agent_configuration"
WHERE
  ("agent_configuration"."scope" in ('workspace', 'published')
    OR "agent_configuration"."authorId" = 6334)
  AND "agent_configuration"."workspaceId" = 4525
  AND "agent_configuration"."status" = 'active'

QUERY PLAN
Bitmap Heap Scan on agent_configurations agent_configuration (cost=22.22..322.72 rows=77 width=802)
Recheck Cond: ((("workspaceId" = 4525) AND ((scope)::text = ANY ('{workspace,published}'::text[])) AND ((status)::text = 'active'::text)) OR (("workspaceId" = 4525) AND ("authorId" = 6334) AND ((status)::text = 'active'::text)))
-> BitmapOr (cost=22.22..22.22 rows=77 width=0)
-> Bitmap Index Scan on partial_agent_config_active (cost=0.00..9.33 rows=77 width=0)
Index Cond: (("workspaceId" = 4525) AND ((scope)::text = ANY ('{workspace,published}'::text[])))
-> Bitmap Index Scan on partial_agent_config_active (cost=0.00..12.86 rows=1 width=0)
Index Cond: (("workspaceId" = 4525) AND ("authorId" = 6334))

Replacing with :

EXPLAIN SELECT * FROM "agent_configurations" AS "agent_configuration"
WHERE
  "agent_configuration"."scope" = 'private'
  AND "agent_configuration"."authorId" = 6334
  AND "agent_configuration"."workspaceId" = 4525
  AND "agent_configuration"."status" = 'active'
  
QUERY PLAN
Index Scan using partial_agent_config_active on agent_configurations agent_configuration (cost=0.29..8.31 rows=1 width=799)
Index Cond: (("workspaceId" = 4525) AND ((scope)::text = 'private'::text) AND ("authorId" = 6334))

And :

EXPLAIN SELECT * FROM "agent_configurations" AS "agent_configuration"
WHERE
  "agent_configuration"."scope" in ('workspace', 'published')
  AND "agent_configuration"."workspaceId" = 4525
  AND "agent_configuration"."status" = 'active'

QUERY PLAN
Index Scan using partial_agent_config_active on agent_configurations agent_configuration (cost=0.29..317.96 rows=79 width=799)
Index Cond: (("workspaceId" = 4525) AND ((scope)::text = ANY ('{workspace,published}'::text[])))

Total planning and execution time is smaller, but more importantly we only do index scans.

Risk

List of assistants different (order ? )

Deploy Plan

deploy front

@tdraier tdraier requested a review from flvndvd October 16, 2024 14:21
Copy link
Contributor

@flvndvd flvndvd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tdraier tdraier merged commit 272ab0c into main Oct 17, 2024
5 checks passed
@tdraier tdraier deleted the split-query branch October 17, 2024 08:03
@spolu
Copy link
Contributor

spolu commented Oct 17, 2024

💥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants