Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL View or Table

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?

like image 614
Sam Cogan Avatar asked Feb 02 '10 11:02

Sam Cogan


People also ask

Is it better to query a view or a table?

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.

Why view is better than table in SQL?

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.

What is the difference between SQL table and SQL view?

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.

Why are views better than tables?

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.


2 Answers

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.

like image 100
Arthur Avatar answered Oct 02 '22 19:10

Arthur


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.

like image 36
Paul Alan Taylor Avatar answered Oct 02 '22 21:10

Paul Alan Taylor