<p>I once worked with an architect who banned the use of SQL views. His main reason was that views made it too easy for a thoughtless coder to needlessly involve joined tables which, if that coder tried harder, could be avoided altogether.  Implicitly he was encouraging code reuse via copy-and-paste instead of encapsulation in views.</p>

<p>The database had nearly 600 tables and was highly normalised, so most of the useful SQL was necessarily verbose.</p>

<p>Several years later I can see at least one bad outcome from the ban - we have many hundreds of dense, lengthy stored procs that verge on unmaintainable.</p>

<p>In hindsight I would say it was a bad decision, but what are your experiences with SQL views?  Have you found them bad for performance? Any other thoughts on when they are or are not appropriate?</p>

[{'answer_id': 42628, 'author': 'Tyler Gooch', 'author_id': 1372, 'author_profile': 'https://Stackoverflow.com/users/1372', 'pm_score': 4, 'selected': False, 'text': "<p>You've answered your own question:</p>\n\n<blockquote>\n  <p>he was encouraging code reuse via copy-and-paste</p>\n</blockquote>\n\n<p>Reuse the code by creating a view.  If the view performs poorly, it will be much easier to track down than if you have the same poorly performing code in several places.</p>\n"}, {'answer_id': 42629, 'author': 'Craig', 'author_id': 2894, 'author_profile': 'https://Stackoverflow.com/users/2894', 'pm_score': 3, 'selected': False, 'text': '<p>My current database was completely awash with countless small tables of no more than 5 rows each.  Well, I could count them but it was cluttered.  These tables simply held constant type values (think enum) and could very easily be combined into one table.  I then made views that simulated each of the tables I deleted to ensure backward compactability.  Worked great.</p>\n'}, {'answer_id': 42631, 'author': 'Sean Chambers', 'author_id': 2993, 'author_profile': 'https://Stackoverflow.com/users/2993', 'pm_score': 1, 'selected': False, 'text': '<p>We use views for all of our simple data exports to csv files. This simplifies the process of writing a package and embedding the sql within the package which becomes cumbersome and hard to debug against.</p>\n\n<p>Using views, we can execute a view and see exactly what was exported, no cruft or unknowns. It greatly helps in troubleshooting problems with improper data exports and hides any complex joins behind the view. Granted, we use a very old legacy system from a TERMS based system that exports to sql, so the joins are a little more complex than usual.</p>\n'}, {'answer_id': 42632, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': 2, 'selected': False, 'text': "<p>Views are good for ad-hoc queries, the kind that a DBA does behind the scenes when he/she needs quick access to data to see what's going on with the system.</p>\n\n<p>But they can be bad for production code.  Part of the reason is that it's sort of unpredictable what indexes you will need with a view, since the where clause can be different, and therefore hard to tune.  Also, you are generally returning a lot more data than is actually necesary for the individual queries that are using the view.  Each of these queries could be tightened up and tuned individually.</p>\n\n<p>There are specific uses of views in cases of data partitioning that can be extremely useful, so I'm not saying they should avoided altogether.  I'm just saying that if a view can be replaced by a few stored procedures, you will be better off without the view.</p>\n"}, {'answer_id': 42639, 'author': 'Guy Starbuck', 'author_id': 2194, 'author_profile': 'https://Stackoverflow.com/users/2194', 'pm_score': 6, 'selected': True, 'text': '<p>There are some very good uses for views; I have used them a lot for tuning and for exposing less normalized sets of information, or for UNION-ing results from multiple selects into a single result set.</p>\n\n<p>Obviously any programming tool can be used incorrectly, but I can\'t think of any times in my experience where a poorly tuned view has caused any kind of drawbacks from a performance standpoint, and the value they can provide by providing explicitly tuned selects and avoiding duplication of complex SQL code can be significant.</p>\n\n<p>Incidentally, I have never been a fan of architectural "rules" that are based on keeping developers from hurting themselves.  These rules often have unintended side-effects -- the last place I worked didn\'t allow using NULLs in the database, because developers might forget to check for null.  This ended up forcing us to work around "1/1/1900" dates and  integers defaulted to "0" in all the software built against the databases, and introducing a litany of bugs caused by devs working around places where NULL was the appropriate value.</p>\n'}, {'answer_id': 42644, 'author': 'Jakub Šturc', 'author_id': 2361, 'author_profile': 'https://Stackoverflow.com/users/2361', 'pm_score': 2, 'selected': False, 'text': '<p>Like all power, views have its own dark side. However, you cannot blame views for somebody writing bad performing code. Moreover views can limit the exposure of some columns and provide extra security.</p>\n'}, {'answer_id': 42650, 'author': 'Niniki', 'author_id': 4155, 'author_profile': 'https://Stackoverflow.com/users/4155', 'pm_score': 0, 'selected': False, 'text': '<p>Let\'s see if I can come up with a lame analogy ...</p>\n\n<p>"I don\'t need a phillips screwdriver.  I carry a flat head and a grinder!"</p>\n\n<p>Dismissing views out of hand will cause pain long term.  For one, it\'s easier to debug and modify a single view definition than it is to ship modified code.</p>\n'}, {'answer_id': 43448, 'author': 'Murph', 'author_id': 1070, 'author_profile': 'https://Stackoverflow.com/users/1070', 'pm_score': 2, 'selected': False, 'text': '<p>One thing that hasn\'t been mentioned thus far is use of views to provide a logical picture of the data to end users for ad hoc reporting or similar.</p>\n\n<p>This has two merits:</p>\n\n<ol>\n<li>To allow the user to single "tables" containing the data they expect rather requiring relatively non technical users to work out potentially complex joins (because the database is normalised)</li>\n<li>It provides a means to allow some degree of ah hoc access without exposing the data or the structure to the end users.</li>\n</ol>\n\n<p>Even with non ad-hoc reporting its sometimes signicantly easier to provide a view to the reporting system that contains the relveant data, neatly separating production of data from presentation of same.</p>\n'}, {'answer_id': 43461, 'author': 'BlaM', 'author_id': 999, 'author_profile': 'https://Stackoverflow.com/users/999', 'pm_score': 1, 'selected': False, 'text': '<p>Some time ago I\'ve tried to maintain code that used views built from views built from views... That was a pain in the a**, so I got a little allergic to views :)</p>\n\n<p>I usually prefer working with tables directly, especially for web applications where speed is a main concern. When accessing tables directly you have the chance to tweak your SQL-Queries to achieve the best performance. "Precompiled"/cached working plans might be one advantage of views, but in many cases just-in-time compilation with all given parameters and where clauses in consideration will result in faster processing over all.</p>\n\n<p>However that does not rule out views totally, if used adequately. For example you can use a view with the "users" table joined with the "users_status" table to get an textual explanation for each status - if you need it. However if you don\'t need the explanation: use the "users" table, not the view. As always: Use your brain!</p>\n'}, {'answer_id': 364408, 'author': 'HLGEM', 'author_id': 9034, 'author_profile': 'https://Stackoverflow.com/users/9034', 'pm_score': 4, 'selected': False, 'text': "<p>Not a big fan of views (Can't remember the last time I wrote one) but wouldn't ban them entirely either. If your database allows you to put indexes on the views and not just on the table, you can often improve performance a good bit which makes them better. If you are using views, make sure to look into indexing them.</p>\n\n<p>I really only see the need for views for partitioning data and for extremely complex joins that are really critical to the application (thinking of financial reports here where starting from the same dataset for everything might be critical). I do know some reporting tools seem to prefer views over stored procs.</p>\n\n<p>I am a big proponent of never returning more records or fields than you need in a specific instance and the overuse of views tends to make people return more fields (and in way too many cases, too many joins) than they need which wastes system resources. </p>\n\n<p>I also tend to see that people who rely on views (not the developer of the view - the people who only use them) often don't understand the database very well (so they would get the joins wrong if not using the view) and that to me is critical to writing good code against the database. I want people to understand what they are asking the db to do, not rely on some magic black box of a view. That is all personal opinion of course, your mileage may vary.</p>\n\n<p>Like BlaM I personally haven't found them easier to maintain than stored procs.</p>\n\n<p>Edited in Oct 2010 to add:\nSince I orginally wrote this, I have had occasion to work with a couple of databases designed by people who were addicted to using views. Even worse they used views that called views that called views (to the point where eventually we hit the limit of the number of tables that can be called). This was a performance nightmare. It took 8 minutes to get a simple count(*) of the records in one view and much longer to get data. If you use views, be very wary of using views that call other views. You will be building a system that will very probably not work under the normal performance load on production. In SQL Server you can only index views that do not call other views, so what ends up happening when you use views in a chain, is that the entire record set has to be built for each view and it is not until you get to the last one that the where clause criteria are applied. You may need to generate millions of records just to see three. You may join to the same table 6 times when you really only need to join to it once, you may return many many more columns than you need in the final results set. </p>\n"}, {'answer_id': 7122445, 'author': 'JGurtz', 'author_id': 287746, 'author_profile': 'https://Stackoverflow.com/users/287746', 'pm_score': 1, 'selected': False, 'text': '<p>Views have been helpful to us in their role for use by public web based applications that dip from a production database. Simplified security is the primary advantage we see since the table design in the database may combine sensitive and non-sensitive data within the same table. A stored procedure shares much of this advantage, but the view is read-only, has potential interop advantages, and is a less complex thing for junior people to implement.</p>\n\n<p>This security abstraction advantage also applies when views are used for end-user ad-hoc queries; this would be less of an advantage if we had a proper, flattened, data warehouse representation of our data.</p>\n'}, {'answer_id': 8297929, 'author': 'George Filippakos', 'author_id': 961333, 'author_profile': 'https://Stackoverflow.com/users/961333', 'pm_score': 0, 'selected': False, 'text': '<p>Views can also reduce the size of complex queries (in the same way stored procs can).</p>\n\n<p>This can reduce network bandwith for very busy databases.</p>\n'}, {'answer_id': 48433063, 'author': 'bleepzter', 'author_id': 499636, 'author_profile': 'https://Stackoverflow.com/users/499636', 'pm_score': 1, 'selected': False, 'text': "<p>From an application stand point which uses an ORM, it's a lot harder to execute a custom query than doing a select on a discretely mapped type (eg, the view). </p>\n\n<p>For example, if you need just 5 fields of a table that has many (say 30 or 40) an ORM framework will create an entity to represent the table.</p>\n\n<p>That means that even though you only need a few properties of the entity, the select query generated by the ORM framework will bring the entire entity in its full glory. A view on the other hand, although also mapped to an entity with the ORM framework, will only bring the data you need.</p>\n\n<p>Second, since ORM frameworks map entities to tables, relationships between entities are generated (and hydrated) on the client side, meaning that the query has to execute and return to the app before linking of those entities can happen at runtime within the app. </p>\n\n<p>Some frameworks bypass that by returning the data from multiple linked entities in a giant select (with multiple joins), bringing in the columns of all related tables in one call. Internally the framework disassembles the giant result set and structures the logical presentation of the linked entities before returning those entities to the caller app.</p>\n\n<p>Point being is that views are a life saver for apps using ORM. The alternative is to manually make db calls, and manually passing the resulting recordsets into usable entities/models.</p>\n\n<p>While this approach is good and definitely produces a result, it has lots of negative facets. Manual code... is manual; hard to maintain, cumbersome in implementation, and causes devs to worry more about the specifics of the DB provider API vs the logical domain model. Not to mention that it increases time to production (its a lot more labourious) costs for development, maintenance, surface area of bugs, etc.</p>\n\n<p>So for anyone saying views are bad, please consider the other side of things; The stuff the high and mighty DBA's most often have no clue about. </p>\n"}]
['https://Stackoverflow.com/questions/42620', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4200/']