Do database views affect query performance?

前端 未结 7 1069
栀梦
栀梦 2020-12-15 04:48

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 th

相关标签:
7条回答
  • 2020-12-15 05:24

    I know this is an old thread. Discussion is good, but I do want to throw in one more thought. Performance also depends on what you are using to pull data with. For example, if you are front-ending with something like Microsoft Access you can definately gain performance for some complex queries by using a view. This is because Access does not always pull from the SQL server as we would like -- in some cases it would pull entire tables across then try to process locally from there! Not so if you use a view.

    0 讨论(0)
  • 2020-12-15 05:27

    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.

    0 讨论(0)
  • 2020-12-15 05:31

    It depends on the RDBMS, but usually there isn't optimization going on, and it's just a convenient way to simplify queries. Some database systems use "materialized views" however, which do use a caching mechanism.

    0 讨论(0)
  • 2020-12-15 05:32

    Generally speaking, views should perform equivalently to a query written directly on the underlying tables.

    But: there may be edge cases, and it would behoove you to test your code. All modern RDBMS systems have tools that will let you see the queryplans, and monitor execution. Don't take my (or anybody else's) word for it, when you can have the definitive data at your fingertips.

    0 讨论(0)
  • 2020-12-15 05:34

    Yes, in all modern RDBMS's (MSSQL after 2005? etc) view's query plans are cached removing the overhead of planning the query and speeding up performance over the same SQL performed in-line. Previously to this (and it applies to parameterized SQL/Prepared Statements as well) people correctly thought stored procedures performed better.

    Many still hang onto this today making it a modern DB myth. Ever since Views/PS's got the cached query planning of SPs they've been pretty much even.

    0 讨论(0)
  • 2020-12-15 05:40

    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.

    0 讨论(0)
提交回复
热议问题