<p>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?</p>

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

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

[{'answer_id': 40179, 'author': 'Matt Rogish', 'author_id': 2590, 'author_profile': 'https://Stackoverflow.com/users/2590', 'pm_score': 4, 'selected': True, 'text': '<p>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.</p>\n\n<p>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?</p>\n\n<p>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.</p>\n'}, {'answer_id': 40187, 'author': 'Kevin Fairchild', 'author_id': 3743, 'author_profile': 'https://Stackoverflow.com/users/3743', 'pm_score': 1, 'selected': False, 'text': "<p>The views are only going to take up cpu/memory resources when they are called.</p>\n\n<p>Anyhow, best practice would be to consolidate what can be consolidated, remove what can be removed, and if it's literally only used by your reports, choose a consistent naming standard for the views so they can easily be grouped together when looking for a particular view.</p>\n\n<p>Also, unless you really need transactional isolation, consider using the NOLOCK table hint in your queries.</p>\n\n<p>-- Kevin Fairchild</p>\n"}, {'answer_id': 40191, 'author': 'Sara Chipps', 'author_id': 4140, 'author_profile': 'https://Stackoverflow.com/users/4140', 'pm_score': 2, 'selected': False, 'text': "<p>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. </p>\n\n<p>That being said, when you select from a view the view defining query is run along with the query you are running. </p>\n\n<p>For example, if vwCustomersWhoHavePaid is:</p>\n\n<pre><code>Select * from customers where paid = 1\n</code></pre>\n\n<p>and the query you are running returns the customers who have paid after August first is formatted like this:</p>\n\n<pre><code>Select * from vwCustomersWhoHavePaid where datepaid &gt; '08/01/08'\n</code></pre>\n\n<p>The query you are actually running is:</p>\n\n<pre><code>Select * from (Select * from customers where paid = 1) where datepaid &gt; '08/01/08'\n</code></pre>\n\n<p>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.</p>\n"}, {'answer_id': 108436, 'author': 'Amy B', 'author_id': 8155, 'author_profile': 'https://Stackoverflow.com/users/8155', 'pm_score': 1, 'selected': False, 'text': "<p>You ask: What's going on behind the scenes?</p>\n\n<p>A view is a bunch of SQL text.  When a query uses a view, SQL Server places that SQL text into the query.  This happens BEFORE optimization.  The result is the optimizer can consider the combined code instead of two separate pieces of code for the best execution plan.</p>\n\n<p>You should look at the execution plans of your queries!  There is so much to learn there.</p>\n\n<p>SQL Server also has a concept of a <em>clustered view</em>.  A <em>clustered view</em> is a system maintained result set (each insert/update/delete on the underlying tables can cause insert/update/deletes on the <em>clustered view</em>'s data).  It is a common mistake to think that views operate in the way that <em>clustered views</em> operate.</p>\n"}]
['https://Stackoverflow.com/questions/40169', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/83/']