[MDB-47576] Optimize GET_DATABASES_SQL#337
Open
UberDever wants to merge 1 commit into
Open
Conversation
…r then engine = 'Replicated'
Contributor
Reviewer's guide (collapsed on small PRs)Reviewer's GuideRefactors the GET_DATABASES_SQL query to explicitly split handling of Replicated and non-Replicated databases, ensuring correct nullable typing of engine_full and clearer filtering logic. Flow diagram for updated GET_DATABASES_SQL filtering and engine_full assignmentflowchart TD
A[system.databases] --> B[Filter name NOT IN excluded_system_dbs]
B --> C[engine = Replicated]
B --> D[engine != Replicated]
C --> E("Select name, engine, metadata_path, uuid, CAST(engine_full, Nullable(String)) AS engine_full")
D --> F("Select name, engine, metadata_path, uuid, CAST(NULL, Nullable(String)) AS engine_full")
E --> G[UNION ALL]
F --> G[UNION ALL]
G --> H[FORMAT JSON result]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- The
name NOT IN (...)filter is duplicated across both branches of the UNION; consider factoring this into a common subquery or CTE to avoid repetition and keep the exclusion list maintainable. - You can likely avoid the UNION by using a single SELECT with a conditional expression (e.g.,
if(engine = 'Replicated', CAST(engine_full, 'Nullable(String)'), CAST(NULL, 'Nullable(String)')) AS engine_full) which keeps the query simpler while preserving the desired type and behavior.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The `name NOT IN (...)` filter is duplicated across both branches of the UNION; consider factoring this into a common subquery or CTE to avoid repetition and keep the exclusion list maintainable.
- You can likely avoid the UNION by using a single SELECT with a conditional expression (e.g., `if(engine = 'Replicated', CAST(engine_full, 'Nullable(String)'), CAST(NULL, 'Nullable(String)')) AS engine_full`) which keeps the query simpler while preserving the desired type and behavior.
## Individual Comments
### Comment 1
<location path="ch_backup/clickhouse/control.py" line_range="300-302" />
<code_context>
+ CAST(engine_full, 'Nullable(String)') AS engine_full
FROM system.databases
- WHERE name NOT IN ('system', '_temporary_and_external_tables', 'information_schema', 'INFORMATION_SCHEMA', '{system_db}')
+ WHERE
+ engine = 'Replicated' AND
+ name NOT IN ('system', '_temporary_and_external_tables', 'information_schema', 'INFORMATION_SCHEMA', '{system_db}')
+ UNION ALL
+ SELECT
</code_context>
<issue_to_address>
**suggestion (performance):** The duplicated filters across both SELECTs may cause unnecessary double scanning of system.databases.
Both UNION ALL branches reapply the same `name NOT IN (...)` filter and differ only by the engine condition and `engine_full`. As a result, `system.databases` is scanned twice with nearly identical predicates. If this query runs on large deployments, consider consolidating into a single SELECT (e.g., with a conditional `engine_full` expression) or introducing a subquery so these filters are evaluated only once while preserving the Nullable semantics.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary by Sourcery
Refine the GET_DATABASES_SQL query to handle replicated and non-replicated databases separately while preserving nullable engine_full handling.