A customer approached us as they were having problems with report performance. The problem related to a report being based upon a view that took 90 seconds to run. IÂ didnât think that was too slow until they told me that there were hundreds of concurrent reports all hitting the same query.
First response was to rewrite the view to see if it could be made more efficient. However, the view was very complex and trusted, so needed to be left as was.
MyÂ second response was to drop the data into a small datamart, then change the reportâs connections. That wasnât suitable as they needed the data to be up to the minute.
No problem, letâs use an Indexed View (materialised view for Oracle users). That failed too as Microsoft SQL Server doesnât support views with outer joins or sub-queries. Iâve no idea why, as Oracle manages it!
After much thinking, I came up with the idea of caching a view. Originally, I thought about changing the report to use a Stored Procedure so that the data could be cached. However, due to the high concurrency, I wasnât happy about the caching algorithm. As there were many reports, it was also better if the original view definition was usedÂ to save time changing all the reports.
The final solution was a scheduled stored procedure. This used the original viewâs definition to populate a database table, adding indexes to improve performance. Each time the stored procedure runs, a different cache table is generated. Once populated, the view used by the reports is changed to point to the newly cached data. Five cache tables are used to ensure that reports that are running never lose their data source.
End result â 90 second test query now takes less than 2 seconds to run.
At the moment, creating the necessary tables and views is mostly a manual process. However, if thereâs interest in this as a product, I may create a user interface to allow better management.