-
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 all 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
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 most_recent_beliefs_mview 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_most_recent_beliefs_mview_sensor_event | ||
| ON most_recent_beliefs_mview(sensor_id, event_start); | ||
| """ | ||
| ) | ||
|
|
||
| op.execute( | ||
| """ | ||
| CREATE INDEX idx_most_recent_beliefs_mview_event_start | ||
| ON most_recent_beliefs_mview(event_start); | ||
| """ | ||
| ) | ||
|
|
||
| # Create a unique index to allow concurrent refreshes | ||
| op.execute( | ||
| """ | ||
| CREATE UNIQUE INDEX idx_most_recent_beliefs_mview_unique | ||
| ON most_recent_beliefs_mview(sensor_id, event_start, source_id); | ||
| """ | ||
| ) | ||
|
|
||
|
|
||
| def downgrade(): | ||
| op.execute("DROP MATERIALIZED VIEW IF EXISTS most_recent_beliefs_mview CASCADE;") |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
FLEXMEASURES_MVIEW_REFRESH_INTERVALsetting requires a short discussion, @Muhammad-Moiz626 @nhoening. I already scoped some options with @Ahmad-Wahid. I suggest to spin out that discussion on this comment thread.There is currently no automation that runs the CLI command to refresh the mviews. Our go-to approach is to do this via a cronjob, for which we'd essentially have to move this setting in the cron job configuration. It doesn't then make sense to have that setting live in FlexMeasures, as FlexMeasures is not setting up cronjobs itself. I believe the config setting is, in this PR, only used to inform the user on the refresh rate in the UI. I think that is nice. If we want to keep that, then an alternative to this config setting may be to use the tooling we have that saves (in the db) the last time a specific CLI command was run successfully, and report that time in the UI instead.