Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it okay to have a lot of database views?

I infrequently (monthly/quarterly) generate hundreds of Crystal Reports reports using Microsoft SQL Server 2005 database views. Are those views wasting CPU cycles and RAM during all the time that I am not reading from them? Should I instead use stored procedures, temporary tables, or short-lived normal tables since I rarely read from my views?

I'm not a DBA so I don't know what's going on behind the scenes inside the database server.

Is it possible to have too many database views? What's considered best practice?

like image 761
Zack Peterson Avatar asked Sep 02 '08 17:09

Zack Peterson


People also ask

Do database views improve performance?

Views make queries faster to write, but they don't improve the underlying query performance. However, we can add a unique, clustered index to a view, creating an indexed view, and realize potential and sometimes significant performance benefits, especially when performing complex aggregations and other calculations.

Should you use database views?

Views are virtual tables that can be a great way to optimize your database experience. Not only are views good for defining a table without using extra storage, but they also accelerate data analysis and can provide your data extra security.

How many views do you need to display a database?

There are two types of database views: dynamic views and static views. Dynamic views can contain data from one or two tables and automatically include all of the columns from the specified table or tables.

Do views slow down database?

The falsehood is that Views are slower because the database has to calculate them BEFORE they are used to join to other tables and BEFORE the where clauses are applied. If there are a lot of tables in the View, then this process slows everything down.


2 Answers

For the most part, it doesn't matter. Yes, SQL Server will have more choices when it parses SELECT * FROM table (it'll have to look in the system catalogs for 'table') but it's highly optimized for that, and provided you have sufficient RAM (most servers nowadays do), you won't notice a difference between 0 and 1,000 views.

However, from a people-perspective, trying to manage and figure out what "hundreds" of views are doing is probably impossible, so you likely have a lot of duplicated code in there. What happens if some business rules change that are embedded in these redundant views?

The main point of views is to encapsulate business logic into a pseudo table (so you may have a person table, but then a view called "active_persons" which does some magic). Creating a view for each report is kind of silly unless each report is so isolated and unique that there is no ability to re-use.

like image 115
Matt Rogish Avatar answered Nov 16 '22 01:11

Matt Rogish


A view is a query that you run often with preset parameters. If you know you will be looking at the same data all the time you can create a view for ease of use and for data binding.

That being said, when you select from a view the view defining query is run along with the query you are running.

For example, if vwCustomersWhoHavePaid is:

Select * from customers where paid = 1

and the query you are running returns the customers who have paid after August first is formatted like this:

Select * from vwCustomersWhoHavePaid where datepaid > '08/01/08'

The query you are actually running is:

Select * from (Select * from customers where paid = 1) where datepaid > '08/01/08'

This is something you should keep in mind when creating views, they are a way of storing data that you look at often. It's just a way of organizing data so it's easier to access.

like image 2
Sara Chipps Avatar answered Nov 16 '22 02:11

Sara Chipps