-
Notifications
You must be signed in to change notification settings - Fork 45
Speed up queries via materialized view #1671
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
Open
Muhammad-Moiz626
wants to merge
17
commits into
main
Choose a base branch
from
feat/materialized-veiws
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3ca67f4
WIP: added migration file.
Muhammad-Moiz626 14226ba
Added materialized view boolean to backend logic and created CLI comm…
Muhammad-Moiz626 ce19cb0
Added refresh interval, Fixed some bugs.
Muhammad-Moiz626 7dd77fe
Merged main
Muhammad-Moiz626 fd41966
Resolved comments.
Muhammad-Moiz626 e483d98
Tiny refactor.
Muhammad-Moiz626 e3277eb
Merge branch 'main' into feat/materialized-veiws
Muhammad-Moiz626 3795934
Merge branch 'main' into feat/materialized-veiws
Muhammad-Moiz626 74d6651
Update documentation/configuration.rst
Muhammad-Moiz626 c65ef8c
Moved get_timed_belief_min_v and some other small changes.
Muhammad-Moiz626 cc88389
Merged main.
Muhammad-Moiz626 942380c
Moved get_timed_belief_min_v to timely-beliefs repo.
Muhammad-Moiz626 b6ca3d4
Updated base.html according to materialized view refresh interval
Muhammad-Moiz626 e2140a1
Renamed timed_belief_min_v to most_recent_beliefs_mview
Muhammad-Moiz626 475cf0d
Merge branch 'main' into feat/materialized-veiws
Muhammad-Moiz626 fa522fa
Added changelog.
Muhammad-Moiz626 e7b4599
Merge branch 'main' into feat/materialized-veiws
Muhammad-Moiz626 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
71 changes: 71 additions & 0 deletions
71
flexmeasures/data/migrations/versions/timed_beliefs_materialized_views.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| """Add materialized view for belief optimization | ||
| Revision ID: c98798csds8c | ||
| Revises: b8f3cda5e023 | ||
| Create Date: 2025-08-08 04:55:33.722545 | ||
| """ | ||
|
|
||
| from alembic import op | ||
|
|
||
| # revision identifiers | ||
| revision = "c98798csds8c" | ||
| down_revision = "b8f3cda5e023" | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
|
|
||
| def upgrade(): | ||
| # Create the materialized view with proper alias | ||
| op.execute( | ||
| """ | ||
| CREATE MATERIALIZED VIEW timed_belief_min_v AS | ||
| SELECT * | ||
| FROM ( | ||
| SELECT | ||
| timed_belief.sensor_id, | ||
| timed_belief.event_start, | ||
| timed_belief.source_id, | ||
| MIN(timed_belief.belief_horizon) AS most_recent_belief_horizon | ||
| FROM timed_belief | ||
| INNER JOIN data_source | ||
| ON data_source.id = timed_belief.source_id | ||
| GROUP BY | ||
| timed_belief.sensor_id, | ||
| timed_belief.event_start, | ||
| timed_belief.source_id | ||
| ) AS belief_mins | ||
| GROUP BY | ||
| sensor_id, | ||
| event_start, | ||
| source_id, | ||
| most_recent_belief_horizon; | ||
| """ | ||
| ) | ||
|
|
||
| # Create indexes | ||
| op.execute( | ||
| """ | ||
| CREATE INDEX idx_timed_belief_min_v_sensor_event | ||
| ON timed_belief_min_v(sensor_id, event_start); | ||
| """ | ||
| ) | ||
|
|
||
| op.execute( | ||
| """ | ||
| CREATE INDEX idx_timed_belief_min_v_event_start | ||
| ON timed_belief_min_v(event_start); | ||
| """ | ||
| ) | ||
|
|
||
| # Create a unique index to allow concurrent refreshes | ||
| op.execute( | ||
| """ | ||
| CREATE UNIQUE INDEX idx_timed_belief_min_v_unique | ||
| ON timed_belief_min_v(sensor_id, event_start, source_id); | ||
| """ | ||
| ) | ||
|
|
||
|
|
||
| def downgrade(): | ||
| op.execute("DROP MATERIALIZED VIEW IF EXISTS timed_belief_min_v CASCADE;") | ||
nhoening marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.