Skip to Main Content

Write Great SQL Office Hours

Free tips and training every month! Subscribe for reminders and more from Office Hours. FAQ

Header container

September 15, 2020

13:00 UTC   Start Times Around the World

Subscribe to be notified of changes to sessions and give us feedback!

Having trouble watching the video on this page? Open the video in your browser.


Count and Sum Millions of Rows Fast with Materialized Views
Queries summarizing data by day, week, or year can often process millions of rows leading to long execution times.

Yet the number of rows they return is small with a fixed upper limit. So you can answer these questions much faster by pre-calculating the result and storing it in a table.

Doing this manually is time-consuming and error-prone. Oracle Database offers an automated solution: materialized views (MVs). These store the results of the query. When you run the query in the MV, the database can redirect it to the results in the MV, leading to huge performance gains with no code changes.

Highlights include:

04:15 - Using indexes to speed up aggregation queries
06:45 - Creating materialized views to store the results of queries
09:50 - Enabling query rewrite, so the optimizer can redirect matching queries from the base table to materialized views
15:20 - Limitations of query rewrite
16:45 - Using dbms_mview.explain_rewrite to understand why a query didn't use an MV
18:00 - The impact of data changes on query rewrite
23:00 - Refreshing the data in MVs
29:00 - Creating materialized view logs to enable fast refresh
38:00 - Real-time materialized views
43:40 - Limitations to fast refresh; using dbms_mview.explain_mview to understand these
45:00 - Bitmap-based count distinct for MVs (19c)
53:00 - Using Database In-Memory to speed up aggregation queries
58:15 - MVs and In-Memory comparisons

Your Experts

    Chris Saxon

    Chris Saxon   

    Chris Saxon is an Oracle Developer Advocate and quizmaster on Dev Gym. His job is to help you get the best out of the Oracle Database and have fun with SQL! He's also part of the answer team on Ask TOM and creates YouTube videos at The Magic of SQL channel.
    Keith Laker

    Keith Laker   

    I have been working with Oracle data warehouse technology for over 20 years working on a wide variety of data warehouse projects both as a consultant and an onsite support engineer. I am now part of the Data Warehouse Product Management Team where I am responsible for Autonomous Database and analytical SQL. I am based in the UK at our Manchester office. A key part of my role is to work with our sales teams to brief our customers on data warehousing and analytical SQL: explaining the wide variety of new and exciting opportunities that our DW and analytical solutions can support. I regularly deliver sales training for data warehousing and analytical SQL across all our sales regions and provide competitive intelligence support across all the major data warehouse vendors.