Scale up as you grow â whether you're running one virtual machine or ten thousand.

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

A Materialized View in PostgreSQL is like a snapshot of a SQL query result that’s physically stored in the database. Unlike a regular view (which re-runs the query every time you access it), a materialized view stores the data and can be refreshed manually or on a schedule.
Performance Boost: Materialized views avoid running complex or expensive queries repeatedly.
Precomputed Aggregates: Great for analytics (e.g., leaderboards, dashboards, reports).
Simplifies Code: Complex joins or calculated fields can be offloaded to the database.
Can Be Indexed: Unlike regular views, you can add indexes to speed up access.
REFRESH MATERIALIZED VIEW my_view;
Concurrent Refresh (allows reads during refresh but requires a unique index):
REFRESH MATERIALIZED VIEW CONCURRENTLY my_view;
Django doesn’t have native support for materialized views, but you can integrate them easily with:
CREATE MATERIALIZED VIEW user_leaderboard AS
SELECT user_id, COUNT(*) AS quizzes_played
FROM quiz_quizresult
GROUP BY user_id;
class UserLeaderboard(models.Model):
user = models.ForeignKey(User, on_delete=models.DO_NOTHING)
quizzes_played = models.IntegerField()
class Meta:
managed = False # So Django doesn't try to create the table
db_table = 'user_leaderboard'
from django.db import connection
def refresh_leaderboard():
with connection.cursor() as cursor:
cursor.execute("REFRESH MATERIALIZED VIEW CONCURRENTLY user_leaderboard;")
Matteo Scarpa
Matteo Scarpa
KFSys
4ae272398ee6445a9bf73f4cd632b5
KFSys
phosphide
KFSys
KFSys
garantieheld
KFSys