-
Notifications
You must be signed in to change notification settings - Fork 52
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
35
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
35 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 24be3f5
Merge remote-tracking branch 'refs/remotes/origin/main' into feat/mat…
Flix6x aabd536
chore: db merge
Flix6x 22cf553
Merge origin/main into feat/materialized-views
Ahmad-Wahid a456cb8
fix: revert accidental regressions in base.html and openapi-specs.json
Flix6x f4726b9
Merge remote-tracking branch 'origin/main' into feat/materialized-veiws
Flix6x 7e359dd
feat: rework materialized view integration around tb ownership and a …
Flix6x 6fee373
style: black
Flix6x 5eb7e02
chore(deps): bump timely-beliefs to >=4.0.0
Flix6x 81a0c6f
chore(deps): bump timely-beliefs to >=4.0.0
Flix6x ccfabc8
refactor: flake8 complained about complexity
Flix6x b75b802
Merge remote-tracking branch 'origin/feat/materialized-veiws' into fe…
Flix6x 8cace80
docs: set user expectation when upgrading the db
Flix6x ae5daa5
Merge remote-tracking branch 'origin/main' into feat/materialized-veiws
Flix6x 70e5be5
feat: support automatically renaming duplicate root assets
Flix6x 6caf424
fix: run ANALYZE after creating or refreshing the materialized view
Flix6x 01812b4
chore(deps): require timely-beliefs>=4.0.1 for mview predicate pushdo…
Flix6x f725653
docs: quantify the mview migration's duration and log a per-table est…
Flix6x 2a0e359
fix: print the mview migration's duration estimate so it is actually …
Flix6x 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
|
||
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.