Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query vs. View

I want to know what is the difference between a query and a view in terms of performance. And if a view is costly, what else besides a query could I do to improve performance?

like image 855
LuRsT Avatar asked Nov 27 '08 16:11

LuRsT


People also ask

What is the difference between a view and a query?

A view is a virtual table defined by a SQL query. When you create a view, you query it in the same way you query a table. When a user queries the view, the query results contain data only from the tables and fields specified in the query that defines the view.

Is a view a query?

A view is a virtual table whose contents are defined by a query. Like a table, a view consists of a set of named columns and rows of data.

Why use a view instead of a query?

Views have the following benefits: Security - Views can be made accessible to users while the underlying tables are not directly accessible. This allows the DBA to give users only the data they need, while protecting other data in the same table. Simplicity - Views can be used to hide and reuse complex queries.

Which is faster view or query?

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.


1 Answers

I can't speak for all databases, but in SQL Server you cannot index views unless you have an Enterprise version. An unindexed view can be significantly poorer in terms of performance than a query especially if you are writing a query against it to add some where conditions. Indexed views generally can perform fairly well. An indexed view can also be against multiple fields which are in differnt tables and that may imporve performance over the ad hoc query. (It may not too, in performance tuning, you must always test against your particular circumstances.)

One point against views is that they do not allow for run-time selection of where criteria. So often you end up with both a view and a query.

Views can be more easily maintained (Just add that new table in a join and everything accessing financial reports has it available) but they are much more difficult to performance tune. This is in part because they tend to be over generalized and thus are slower than their counterparts which only return the minimum necessary. And yes as Jonathan said, you can far too easily get into joining together views for a report into a mess which joins to the same large tables many more times than need be and is very slow.

Two places where views shine though is: Making sure that complex relationships are always correctly described. This is one reason why report writers tend to favor them. Limiting access to a subset of records

There are also limitations on the type of queries that can be done for a view vice an ad hoc query or a stored proc. For instance you can't use an if statement (or other procedural type code such as looping) or as noted above you cannot provide run-time values for the where criteria.

One place where views are often significantly slower is when they call other views. The underlying views need to be fully realized in some databases and thus you might need to callup 4,459,203 records to see the 10 you are ultimately interested in. Start to layer this more than once and it can get very slow, very fast; views that call views are simply a poor practice.

like image 73
HLGEM Avatar answered Oct 01 '22 04:10

HLGEM