No Materialized Views in MySQL

I've created a nasty view in MySQL that retrieves a lot of data across multiple tables and performs some calculations for columns. The query runs slow and will obviously become a problem under high loads. Another problem is that MySQL doesn't support materialized views.

Materialized views are a way to cache or store a view's data like an actual table to increase data access speeds for queries. In this scenario, when you request data from a view in a SELECT statement, the data is retrieved from the cached table instead of executing the query for the view to get the data. Because the SQL parser doesn't have to execute the query for the view, performance is increased. Data for the materialized view is updated frequently to prevent dirty reads from the view. Materialized views are normally used in data warehousing to improve querying on large amounts of data or for complex views. But I find them useful for complex queries.

However, in MySQL (and PostgreSQL), when you perform a SELECT statement on a view, the SQL for that view is actually executed to get the data. This can have serious performance problems if the view is a complex query (like the one I created) or is accessed (executed) frequently.

You can improve performance for a complex view by indexing the view. This is not supported in MySQL but the underlying indexes in tables will be used which can increase performance.
http://forums.mysql.com/read.php?100,22967,23070#msg-23070

You can also mimic materialized views by setting up a table with the columns you need. Then create a MySQL function that executes the query for the view and populates the table. This can be executed at specific intervals using cron jobs. The downside is that the table is not updated in real-time (due to the cron job). See the comments at the bottom of the following pages:
http://dev.mysql.com/doc/refman/6.0/en/create-view.html
http://dev.mysql.com/doc/refman/5.0/en/create-view.html

SQL Server supports materialized and indexed views as per this article:
http://www.microsoft.com/technet/prodtechnol/sql/2005/impprfiv.mspx

Oracle also supports materialized views:
http://download.oracle.com/docs/cd/B10501_01/server.920/a96567/repmview.htm

Comments
Contact Chris SchofieldBlogCFC was created by Raymond Camden. This blog is running version 5.9.001.