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

MySQL is Looking Pretty Cool

OK. You may think I'm crazy, but, I just started working with MySQL for the first time. That's right. I've never used MySQL until now. However, to compensate, I've been using PostgreSQL and SQL Server for the past 7 or 8 years. Now, I played with MySQL while I was attending USU but never got into it too much because, back then, it wouldn't even enforce primary and foreign keys. Thus, frankly, I brushed it off.

Also, when developing web sites, I've had several opportunities to work with MySQL but because SQL Server was available, and I was already familiar with it, I used it instead.

Anyway, I have to admit MySQL is awesome. It's come a long way since I last worked with it in college. One thing I really like: its so darn easy to backup and restore a DB. Plus, it's fast, it's reliable, and, best of all, it's open source and free. I highly recommend it.

Restoring a SQL Server Database by Attaching

I recently needed a copy of a SQL Server database from a co-worker's machine. Instead of trying to use the restore option in SQL Server, I just simply copied the .mdf and .ldf files for that database from SQL Server directories (usually, C:\Program Files\Microsoft SQL Server\MSSQL.[dbnumber]\MSSQL\data), and pasted them into my SQL Server directories. I then right clicked on "Databases" and selected "Attach". Browse to the new files, and it adds the database to the list.

Very easy.

FIX: Backup Problems with SQL Server Express

For several months (if not years) I've always had problems backing up and restoring SQL Server Express databases. Usually, the error messages related "Access is Denied" to the backup file from which I was restoring. Up until now this was not a major issue because I was only using the databases internally and did not need to share or distribute them to others. However, with some recent developments in a new product we're creating, I finally resolved the issue.

The error messages always relayed the same "Access is Denied" issue. So, I assumed from the beginning that SQL Server Express did not have sufficient privileges to read and write to the backup file. Come to find out, SQL Server Express uses the NETWORK SERVICE account when the service is started. Thus, with the help of this post on the Microsoft forums I resolved the problem by changing the login account from NETWORK SERVICE to the Local System Account:

1) Go to Start -> Control Panel -> Administrative Tools

2) If you are logged in as a limited user, right-click on "Services" and click "Run As" then click "The following user:" and login as an Administrator. Otherwise, just double-click the "Services" icon.

3) Scroll down to the "SQL Server (SQLEXPRESS)" service. You'll notice that the Log On As column is set to "Network Service".

4) Right-click on the service and click "Properties".

5) Click the "Log On" tab and you'll notice the "This account:" button is highlighted. Select the "Local System account".

6) Click OK

7) Repeat items 1 - 6 for the following services:

SQL Server Active Directory Helper

SQL Server Browser

SQL Server Full Text Search (SQLEXPRESS)

SQL Server Reporting Services (SQLEXPRESS)

8) Restart your machine.

After restarting, restoring a database from the backups worked just fine. I'm not sure how this could be implemented on a hosted solution, since our final product will be delivered to a hosting company and I won't have direct access to the server. We may still have to build a script to install the database from scratch. Bummer.

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