I have a table of data that gets update once a week. I then have a query that process this data and essentially returns a list of codes and the amount of hours booked to those codes. This query is reasonably complicated and takes about 5 seconds to run.
This data needs to be used by a lot of other query's in the DB, so I want to put it somewhere that it can be accessed easily by other query's. Doing this in view seemed like a good idea, but that means that every time this view is called it will re-run this query, taking 5 seconds, if there are a lot of calls to this in one go then its going to cause the app to slow down.
So I was thinking, would it be better to create this view as a table when the data is imported on a Monday, as that will be the only time this changes. Is this the best Idea, or am I looking at this the wrong way?
Because you store data in a table on the database, it can be quicker to access. Once you open the application, you can quickly access the information you seek. Data in a view can take longer to access because you have to run a query first. If you want results for data from multiple tables, this can take even longer.
A table contains data, a view is just a SELECT statement which has been saved in the database (more or less, depending on your database). The advantage of a view is that it can join data from several tables thus creating a new view of it.
View and Table both are integral parts of a relational database, and both terms are used interchangeably. The view is a result of an SQL query and it is a virtual table, whereas a Table is formed up of rows and columns that store the information of any object and be used to retrieve that data whenever required.
Views enable us to hide some of the columns from the table. It simplifies complex queries because it can draw data from multiple tables and present it as a single table. It helps in data security that shows only authorized information to the users.
A co-worker pointed me to "Materialized Views".
http://www.pgcon.org/2008/schedule/attachments/64_BSDCan2008-MaterializedViews-paper.pdf
Basically you copy data from a view into a table and uses this table as a view. The point in that presentation (link) is that you can work with triggers and functions to update only some portions of that table.
Very useful, I've implemented such an materialized view in SQL Server.
I deal with the same issues with most of my projects.
We have copious amounts of data which needs to be reorganised for different purposes. We also benefit from a corporate culture that is used to batch jobs and overnight processes, so users are well educated about the snapshotted nature of data. The first thing most users to is export the data to Excel, so it's a non-issue.
Using additional tables is a sensible way to go here.
Personally, I prefix these tables with an underscore.
_LargeUsefulData
This allows me to easily identify convenience tables from entities that play an active role in the normal operations of the system.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With