← Home ← Codex ← DEBT ← Engine
Browse by Category
+ added · updated 7d
← Back to glossary

Database Views & Materialised Views

Database PHP 5.0+ Intermediate
debt(d7/e5/b5/t7)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The detection_hints indicate no automated detection, and tools like mysql-workbench and pgadmin are manual inspection tools rather than automated scanners. Identifying whether a regular view vs materialised view is appropriate, or spotting repeated complex JOINs that should be views, requires careful review of query patterns and performance profiling.

e5 Effort Remediation debt — work required to fix once spotted

Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix suggests encapsulating complex joins into views, which typically means modifying multiple PHP query locations to use the new view, creating the view definition, and potentially setting up refresh schedules for materialised views. Not a one-line fix, but contained to the database layer and its consumers.

b5 Burden Structural debt — long-term weight of choosing wrong

Closest to 'persistent productivity tax' (b5). Views and materialised views create ongoing maintenance burden: refresh schedules must be managed, SELECT * views can break when tables change, and the abstraction layer between PHP code and raw tables adds complexity. Applies broadly across web and cli contexts per applies_to, affecting multiple work streams that touch reporting or complex queries.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7). The misconception explicitly states developers believe 'views improve query performance' when regular views provide no caching at all — they re-execute the full query every time. This contradicts how similar concepts work (e.g., caching layers, indexes) where the name implies performance benefit. The common_mistakes compound this with stale materialised view data being served silently.

About DEBT scoring →

Also Known As

view materialised view virtual table REFRESH MATERIALIZED VIEW

TL;DR

A view is a saved SELECT query presented as a virtual table. A materialised view stores the query result physically — trading freshness for query speed.

Explanation

Regular views execute the underlying query every time they are accessed — no storage, always current, but no performance benefit over the query itself. Materialised views (PostgreSQL, Oracle) store the result set and must be refreshed (REFRESH MATERIALIZED VIEW) — O(1) reads at the cost of staleness and storage. Use views for: readable query aliases, hiding complexity, column-level security. Use materialised views for: expensive aggregations, reporting queries, denormalised read models that can tolerate some staleness.

Common Misconception

Views improve query performance — regular views are just saved queries; they do not cache results and provide no performance benefit. Only materialised views store data.

Why It Matters

A reporting query that joins 5 tables and takes 8 seconds becomes a sub-millisecond lookup as a materialised view — the right tool can eliminate entire reporting bottlenecks.

Common Mistakes

  • Expecting regular views to cache results — they re-execute the full query on every access.
  • Not refreshing materialised views after data changes — stale data silently served to users.
  • REFRESH MATERIALIZED VIEW blocking reads — use REFRESH MATERIALIZED VIEW CONCURRENTLY in PostgreSQL to avoid locks.
  • Views with SELECT * — underlying table changes (added columns) change the view's output unexpectedly.

Code Examples

✗ Vulnerable
-- Regular view used for performance — no benefit:
CREATE VIEW monthly_revenue AS
    SELECT DATE_TRUNC('month', created_at) AS month, SUM(total)
    FROM orders GROUP BY 1;
-- SELECT * FROM monthly_revenue still scans millions of orders every time
✓ Fixed
-- Materialised view — computed once, queried fast:
CREATE MATERIALIZED VIEW monthly_revenue AS
    SELECT DATE_TRUNC('month', created_at) AS month, SUM(total)
    FROM orders GROUP BY 1;
CREATE UNIQUE INDEX ON monthly_revenue(month);

-- Refresh after order changes (or on schedule):
REFRESH MATERIALIZED VIEW CONCURRENTLY monthly_revenue;
-- Now: SELECT * FROM monthly_revenue — sub-millisecond

Added 15 Mar 2026
Edited 22 Mar 2026
Views 68
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W 1 ping T 2 pings F 0 pings S 1 ping S 0 pings M 1 ping T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 2 pings T 0 pings W 0 pings T 1 ping F
Applebot 1
No pings yesterday
Amazonbot 9 Scrapy 9 Perplexity 6 ChatGPT 6 Ahrefs 5 SEMrush 4 PetalBot 3 Unknown AI 2 Google 1 Majestic 1 Meta AI 1 Bing 1 Twitter/X 1 Brave Search 1 Applebot 1
crawler 47 crawler_json 4
🧱 FUNDAMENTALS — new to this? Start with the ground floor.
Database database A database is an organized collection of data stored electronically, designed so programs can efficiently retrieve, add, update, and delete information.

Nearly every application needs to remember information between sessions. Databases provide the reliable, fast, and organized storage that makes persistent data possible at any scale.

💡 Always use prepared statements with placeholders—never concatenate user input directly into database queries.

Ask Codex about Database →
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Use views to encapsulate complex joins and provide a stable API to reporting tools — if the view is slow, consider a materialised view with a refresh schedule instead
📦 Applies To
PHP 5.0+ web cli
🔗 Prerequisites
🔍 Detection Hints
Same complex JOIN repeated in 5 different PHP queries; reporting query joining 8 tables inline
Auto-detectable: ✗ No mysql-workbench pgadmin
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: Medium Context: File


✓ schema.org compliant