r/learnSQL • • 4d ago

Use Case of Views and Materialized Views???

Hi there,

As a beginner of learning sql , I really don't know where the data analysts folks actually use of Views and materialised Views to analyse the real world data. Are people really use this in the real world problems day to day .

Help me to understand where is the real use case of these in the real world problems

4 Upvotes

8 comments sorted by

6

u/0utlawViking 4d ago

Yep, definitely! in real world projects, views are used to simplify complex queries and reuse them without writing the same SQl again.
Materialized view are useful when when you frequently need the same heavy calculations or aggregations, as they store the results and save query time, you'll come across both quite often in data analytics!

5

u/First-Feature-3556 4d ago

Views are also an easy way to provide an "external interface" to data:

You are the developer of software A. Software B (maintained by someone else) regularly needs to read data in your database. As soon as your software A changes its database schema (for example, during a release upgrade), software B breaks.

Solution: You create a view with the data that software B needs. That means you can change the DB schema of software A as you see fit, you just need to adjust the view as well to keep it backwards-compatible.

2

u/Wald0101 3d ago

This! Used MVs a lot for external data requests of stakeholders and TPAs. Mainly through integrations, custom pages and dashboards or setting us scheduled reporting with limited access to our data sources.

2

u/treznor70 4d ago

Another usage of views is to abstract away physical data model changes. Data model changes break people's analytical processes. Using views allows you to make a physical data model change while still presenting the data to the analytics process in the same manner. That allows the analytics processes to change over time as opposed to the same moment as the data model change (or potentially never change, depending on the needs of the organization).

2

u/jeffrey_f 2d ago

Here is a good use for a view in a healthcare: example

Allowing access to to reporting information while protecting Personally Identifiable Information (PII) and Protected Health Information (PHI).

With HIPAA, neither of those pieces of information can be shared without proper consent and disclosure that it was shared. You can anonymize patient info via a view that possibly replaces or masks that info.

That view will give access to information without disclosing said information. The care itself isn't the private part. Who was given the care is.

1

u/Discordbothost 4d ago

Try a dashboard that shows yesterday's sales by store and refreshes before the morning meeting. A materialized view can do that grouping once per refresh instead of for everyone who opens the dashboard. If the same page needs to show refunds as they happen, use a live query for that number. How fresh each number needs to be

1

u/navicat-gu 2d ago

Yes, all the time. Some everyday examples:

Views (a saved query, always up to date):

  • One definition of a metric. active_customers or net_revenue is written once as a view, so every report, dashboard and analyst uses the same logic instead of ten slightly different WHERE clauses.
  • Hiding messy joins. Analysts query v_orders_enriched instead of joining 6 tables every time.
  • Access control. Give a team access to a view without the salary or email columns, instead of the raw table.
  • A stable interface. You can restructure the underlying tables, and as long as the view returns the same columns, other people's queries keep working.

Materialized views (the result is stored and refreshed on a schedule):

  • Heavy aggregations that many people read but that only need to be fresh daily or hourly, e.g. "sales by store by day" for a morning dashboard.

Some database-specific details beginners get caught out by:

  • PostgreSQL: REFRESH MATERIALIZED VIEW sales_daily; locks readers out during the refresh. REFRESH ... CONCURRENTLY avoids that but needs a unique index on the materialized view.
  • MySQL has no materialized views. People use a summary table refreshed by a scheduled Event or a job.
  • SQL Server has indexed views, which are maintained automatically but come with a lot of restrictions.

A good exercise: take a query you've written three times, turn it into a view, then try a materialized version in Postgres and compare timings with EXPLAIN ANALYZE.

Disclosure: I'm RC, Director of Digital Strategy @ Navicat. If you want a GUI to practise in, Navicat 18 works with PostgreSQL, MySQL and SQL Server, and Navicat Premium Lite is free to try for writing views and queries. The visual view builder is in the paid editions.