Skip to content

Speed up queries via materialized view#1671

Open
Muhammad-Moiz626 wants to merge 28 commits into
mainfrom
feat/materialized-veiws
Open

Speed up queries via materialized view#1671
Muhammad-Moiz626 wants to merge 28 commits into
mainfrom
feat/materialized-veiws

Conversation

@Muhammad-Moiz626

@Muhammad-Moiz626 Muhammad-Moiz626 commented Aug 19, 2025

Copy link
Copy Markdown
Contributor

Description

Our data model with multiple beliefs per event time (multiple horizons as well as sources) makes queries computationally expensive. Most-used are queries where we look only for the most recent beliefs about each event, when given any moment in time as max belief time.

Pre-computation with Postgres' materialized views can make queries much faster. This PR will enable hosts to use this performance enhancement.

  • Created migrations file to create materialized view.
  • Use the MV in queries.
  • Use SQL Alchemy for MV.
  • Automate the refreshing of the MV.
  • Make MV optional / configurable.
  • Document the process

...

Further Improvements

  • the full queries (used in playbacks, getting fulls set of beliefs for each event) might also enjoy a materialized view, but maybe it is just too much data, then we should just limit playback to shorter time ranges
  • give hosts a range of options (e.g. refresh time)
  • actually move to TimeScaleDB, which auto-refreshes their materialized views on inserts

Related Items

Closes 108

...


Sign-off

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on code under GPL or other license that is incompatible with FlexMeasures

@Muhammad-Moiz626 Muhammad-Moiz626 marked this pull request as draft August 19, 2025 15:23
@nhoening nhoening added the Data label Aug 19, 2025
@nhoening nhoening changed the title WIP: added migration file. WIP: Speed up queries via materialized view Aug 19, 2025

@Flix6x Flix6x left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Some early review comments. Nice to see the refresh logic taking shape.

Comment thread flexmeasures/api/dev/sensors.py Outdated
Comment thread flexmeasures/cli/db_ops.py Outdated
Comment thread flexmeasures/cli/db_ops.py Outdated
Comment thread flexmeasures/cli/db_ops.py Outdated
Comment thread flexmeasures/data/schemas/tests/test_input_schema.py Outdated
Comment thread flexmeasures/cli/db_ops.py Outdated
Comment thread flexmeasures/utils/config_defaults.py Outdated
@read-the-docs-community

read-the-docs-community Bot commented Aug 25, 2025

Copy link
Copy Markdown

Documentation build overview

📚 flexmeasures | 🛠️ Build #30701717 | 📁 Comparing aabd536 against latest (08d77fa)


🔍 Preview build

Show files changed (5 files in total): 📝 5 modified | ➕ 0 added | ➖ 0 deleted
File Status
changelog.html 📝 modified
configuration.html 📝 modified
_autosummary/flexmeasures.data.models.generic_assets.html 📝 modified
_autosummary/flexmeasures.data.models.time_series.html 📝 modified
api/v3_0.html 📝 modified

@Muhammad-Moiz626 Muhammad-Moiz626 marked this pull request as ready for review August 27, 2025 12:00

@Flix6x Flix6x left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Exciting work! Thanks for addressing my earlier comments. Just a short code review for now, so you don't have to wait for me testing. Looking forward to testing tomorrow.

Comment thread documentation/configuration.rst Outdated
Comment thread documentation/configuration.rst Outdated
Comment thread flexmeasures/utils/config_defaults.py Outdated
Comment thread flexmeasures/ui/views/assets/views.py Outdated
Comment thread flexmeasures/utils/validation_utils.py Outdated
Comment thread flexmeasures/ui/templates/base.html Outdated
Comment thread flexmeasures/ui/templates/base.html Outdated
Comment thread flexmeasures/utils/validation_utils.py Outdated
Comment thread flexmeasures/utils/validation_utils.py Outdated

@nhoening nhoening left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also a quick review from me, before having tested myself, mostly about concepts.

I also suggest moving get_timed_belief_min_v() to timely-beliefs and add basic documentation there (not much more than the SQL command so anybody reading only that could in principle use it)

Comment thread documentation/configuration.rst Outdated
Comment thread documentation/configuration.rst Outdated
Comment thread documentation/configuration.rst Outdated
Comment thread documentation/configuration.rst Outdated
Comment thread flexmeasures/data/migrations/versions/timed_beliefs_materialized_views.py Outdated
Comment thread flexmeasures/utils/validation_utils.py Outdated
@Muhammad-Moiz626 Muhammad-Moiz626 changed the title WIP: Speed up queries via materialized view Speed up queries via materialized view Sep 8, 2025
Comment thread documentation/configuration.rst Outdated
…erialized-veiws

# Conflicts:
#	documentation/changelog.rst
#	documentation/configuration.rst
#	flexmeasures/data/models/generic_assets.py
#	flexmeasures/ui/templates/base.html
Signed-off-by: F.N. Claessen <felix@seita.nl>
Ahmad-Wahid and others added 6 commits June 4, 2026 21:46
Resolved conflicts in changelog.rst, configuration.rst, time_series.py,
assets.py, base.html, and views.py, keeping materialized view features
alongside upstream changes.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
base.html gained ~950 lines of stale template blocks (copies of JS that lives in
includes/graphs.html) and openapi-specs.json had its version reverted. Restore both
to main; the materialized-view UI toggle will be re-added on top of the real
chart-loading code in includes/graphs.html.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Qprz6PKnXkVE1TJ8d7PYe9
…recorded refresh time

- migration: frozen SQL from timely-beliefs' DDL generators (simplified view query, no
  CASCADE on downgrade), repointed onto main's migration head (drops the stale merge migration)
- CLI: refresh-materialized-views records its run in latest_task_run via
  @task_with_status_report, supports --concurrent (autocommit connection), aborts with an
  error exit code on failure; monitorable with 'flexmeasures monitor latest-run'
- config: replace FLEXMEASURES_MVIEW_REFRESH_INTERVAL with
  FLEXMEASURES_MVIEW_ALWAYS_INCLUDE_LIVE_TAIL (the refresh cadence now lives solely in how
  hosts schedule the CLI command; the recorded refresh time is the single source of truth)
- queries: TimedBelief.search decides centrally whether to trust the view (it must exist at
  startup and have a recent enough recorded refresh) and passes a cutoff so events recorded
  since the last refresh are looked up in the beliefs table (live tail); new include_live_tail
  param threaded through Sensor/GenericAsset.search_beliefs and the chart data API endpoints
- UI: 'Include latest data' toggle on asset graphs (shown only when the view is in use and
  the live tail is not already always included), wired to the real chart-loading code in
  includes/graphs.html; tooltip shows the actual last refresh time
- docs: configuration.rst setting description, hosting docs section with cron example,
  changelog entry
- tests: CLI refresh success/failure recording, mview/live search equivalence

Note: requires the timely-beliefs release containing SeitaBV/timely-beliefs#204
(materialized_views module); the dependency pin should be bumped once that is released.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Qprz6PKnXkVE1TJ8d7PYe9
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@socket-security

socket-security Bot commented Jul 6, 2026

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Updatedtimely-beliefs@​3.5.5 ⏵ 4.0.0100 +1100100100100

View full report

Flix6x and others added 3 commits July 6, 2026 10:40
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
…at/materialized-veiws

# Conflicts:
#	uv.lock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants