Is a View faster than a regular Query?

8

When I use a query of type

SELECT * FROM myView

is faster than

SELECT * FROM (query para gerar a view acima)

I have a query and I would like to cache it or make it faster, but I am in doubt if I create with View or Query !     

asked by anonymous 17.10.2017 / 04:51

2 answers

7

Views are commonly used to improve security, avoid repetition of SQL code, and semantics.

In most cases it will not bring about performance improvements, unless you're talking about a Indexed View . In this case could be an increase in performance. Read the excerpt from MSDN :

  

Creating a unique clustered index in a view improves performance because the view is stored in the database in the same way that a clustered index is.

Microsoft has released a chart that shows an absurd gain in performance with this type of view:

Thishasbeenremovedfrom Improving performance with SQL Server 2005 Indexed Views.

18.10.2017 / 04:20
3

VIEW , among other things, is for you to store a complex query on an object in the database, making it easier to reuse this SQL code. Then every time it is triggered the SQL code will be executed.

Assuming you're using SQL Server, you'll have performance gains if you use Indexed Views . This feature allows SQL Server to search the indexes created for the view as if it were a table.

There are some restrictions that you can find in the Microsoft documentation. The following link is also documented how to create a Indexed View link

PostgreSQL and Oracle also implement something similar, but with the name Materialized View . Here are some links on Materialized View migration from Oracle to Indexed Views of SQL Server.

link

link

    
18.10.2017 / 01:07