Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do database views affect query performance?

Are database views only a means to simplify the access of data or does it provide performance benefits when accessing the views as opposed to just running the query which the view is based on? I suspect views are functionally equivalent to just the adding the stored view query to each query on the view data, is this correct or are there other details and/or optimizations happening?

like image 975
Morten Christiansen Avatar asked Feb 09 '09 18:02

Morten Christiansen


People also ask

Does views improve query 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.

Do views slow down queries?

A view in any kind of a loop will cause serious slowdown because the view is repopulated each time it is called in the loop. Same as a query. In this situation a temporary table using # or @ to hold your data to loop through is faster than a view or a query.

Are database views slower than tables?

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.

Is Creating views can improve query response time?

The view is the virtual tables that are created to restrict access to the data and protect intricate or sensitive data. Virtual tables can improve the query response time by structuring the data in such a way that users find intuitive. Hence, all the above statements are correct for views.


2 Answers

I have always considered Views to be like a read-only Stored Procedures. You give the database as much information as you can in advance so it can pre-compile as best it can.

You can index views as well allowing you access to an optimised view of the data you are after for the type of query you are running.

like image 110
Robin Day Avatar answered Sep 22 '22 15:09

Robin Day


Although a certain query running inside a view and the same query running outside of the view should perform equivalently, things get much more complicated quickly when you need to join two views together. You can easily end up bringing tables that you don't need into the query, or bringing tables in redundantly. The database's optimizer may have more trouble creating a good query execution plan. So while views can be very good in terms of allowing more fine grained security and the like, they are not necessarily good for modularity.

like image 43
Craig Stuntz Avatar answered Sep 24 '22 15:09

Craig Stuntz