存储过程和视图有什么区别?
我对以下几点感到困惑:
存储过程和视图有什么区别?
在 SQL Server 中,什么时候应该使用存储过程,什么时候应该使用视图?
视图是否允许创建可以传递参数的动态查询?
哪一个是最快的,在什么基础上一个比另一个更快?
视图或存储过程是否永久分配内存?
如果有人说视图创建虚拟表,而过程创建材料表,这是什么意思?
如果有的话,请让我知道更多要点。
I am confused about a few points:
What is the difference between a stored procedure and a view?
When should I use stored procedures, and when should I use views, in SQL Server?
Do views allow the creation of dynamic queries where we can pass parameters?
Which one is the fastest, and on what basis is one faster than the other?
Do views or stored procedures allocate memory permanently?
What does it mean if someone says that views create a virtual table, while procedures create a materials table?
Please let me know about more points, if there are any.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
视图是在数据库中保存复杂
SELECT
的简单方法。当简单的 SQL 不够时,可以使用存储过程。存储过程包含变量、循环和对其他存储过程的调用。它是一种编程语言,而不是查询语言。
视图是静态的。将它们视为具有特定布局的新表,其中的数据是使用您创建的查询动态创建的。与任何 SQL 表一样,您可以使用
WHERE
、GROUP BY
和ORDER BY
对其进行排序和过滤。这取决于你做什么。
这取决于数据库。简单视图只是运行查询并过滤结果。但是像 Oracle 这样的数据库允许创建“物化”视图,它基本上是一个表,当视图的基础数据发生变化时会自动更新。
物化视图允许您在视图的列上创建索引(特别是在数据库中不存在的计算列上)。
我听不懂你在说什么。
A view is a simple way to save a complex
SELECT
in the database.A store procedure is used when simple SQL just isn't enough. Store procedures contain variables, loops and calls to other stored procedures. It's a programming language, not a query language.
Views are static. Think of them as new tables with a certain layout and the data in them is created on the fly using the query you created it with. As with any SQL table, you can sort and filter it with
WHERE
,GROUP BY
andORDER BY
.The depends on what you do.
The depends on the database. Simple views just run the query and filter the result. But databases like Oracle allow to create a "materialized" view which is basically a table which is updated automatically when the underlying data of the view changes.
A materialized view allows you to create indexes on the columns of the view (especially on the computed columns which don't exist anywhere in the database).
I don't understand what you're talking about.
当 Mahesh 建议您不能更改视图中的数据时,他并不完全正确。因此,在帕特里克的视图中,
我可以更新数据...作为一个例子,我可以执行其中任何一个...
或者
您无法插入到此视图,因为并非所有表中的所有字段都存在,而且我假设 PROFILE_ID 是主键并且不能为 NULL。
但是,您有时可以插入到视图中...
在现有表上创建了一个视图
我使用... THEN
,并且
在这种情况下,INSERT 和 DELETE 都有效
显然,您无法更新任何聚合或计算的字段,但任何字段只是一个直接视图的视图应该是可更新的。
如果视图包含多个表,则您无法插入或删除,但如果视图是一个表的子集,则通常可以。
Mahesh is not quite correct when he suggests that you can't alter the data in a view. So with patrick's view
I CAN update the data ... as an example I can do either of these ...
or
You can't INSERT to this view as not all of the fields in all of the table are present and I'm assuming that PROFILE_ID is the primary key and can't be NULL.
However you can sometimes INSERT into a view ...
I created a view on an existing table using ...
THEN
and
Both the INSERT and the DELETE worked in this case
Obviously you can't update any fields which are aggregated or calculated but any view which is just a straight view should be updateable.
If the view contains more than one table then you can't insert or delete but if the view is a subset of one table only then you usually can.
主要区别在于,当您查询视图时,它的定义将粘贴到您的查询中。过程也可以给出查询结果,但它是经过编译的并且速度更快。另一种选择是索引视图。
Main difference is that when you are querying a view then it's definition is pasted into your query. Procedure could also give results of query, but it is compiled and for so faster. Another option are indexed views..
@Patrick 的说法是正确的,但是为了回答您的其他问题,视图将在内存中创建自己,并且根据连接、数据的类型以及是否完成了任何聚合,它可能是一个非常消耗内存的视图。
存储过程使用临时哈希表(例如#tmpTable1)或使用@tmpTable1 在内存中进行所有处理。取决于你想告诉它做什么。
存储过程类似于函数,但直接通过其名称进行调用。而不是查询本身内部实际使用的函数。
显然,如果您不检索大量数据,大多数时候内存表会更快。
@Patrick is correct with what he said, but to answer your other questions a View will create itself in Memory, and depending on the type of Joins, Data and if there is any aggregation done, it could be a quite memory hungry View.
Stored procedures do all their processing either using Temp Hash Table e.g #tmpTable1 or in memory using @tmpTable1. Depending on what you want to tell it to do.
A Stored Procedure is like a Function, but is called Directly by its name. instead of Functions which are actually used inside a query itself.
Obviously most of the time Memory tables are faster, if you are not retrieveing alot of data.
视图代表一个虚拟表。您可以在视图中连接多个表,并使用该视图来呈现数据,就像数据来自单个表一样。
存储过程使用参数来执行功能...无论是更新和插入数据,还是返回单个值或数据集。
创建视图和存储过程 - 来自 Microsoft 的一些信息,说明何时以及为什么要使用每个。
假设我有两个表:
tbl_user
,其中包含列:user_id
、user_name
、user_pw
tbl_profile,包含列:
profile_id
、user_id
、profile_description
因此,如果我发现自己从这些表中查询很多......而不是这样做在每条 SQL 中的连接,我会定义一个视图,例如:
因此,如果我将来想通过
user_id
查询profile_description
,我所要做的就是:该代码可以在存储过程中使用,例如:
因此,稍后,我可以调用:
我将获得
user_id
25 的描述,其中25
是您的参数。显然还有很多细节,这只是基本想法。
A view represents a virtual table. You can join multiple tables in a view and use the view to present the data as if the data were coming from a single table.
A stored procedure uses parameters to do a function... whether it is updating and inserting data, or returning single values or data sets.
Creating Views and Stored Procedures - has some information from Microsoft as to when and why to use each.
Say I have two tables:
tbl_user
, with columns:user_id
,user_name
,user_pw
tbl_profile
, with columns:profile_id
,user_id
,profile_description
So, if I find myself querying from those tables A LOT... instead of doing the join in EVERY piece of SQL, I would define a view like:
Thus, if I want to query
profile_description
byuser_id
in the future, all I have to do is:That code could be used in a stored procedure like:
So, later on, I can call:
and I will get the description for
user_id
25, where the25
is your parameter.There is obviously a lot more detail, this is just the basic idea.
此处提供了大量信息
这里有一个很好的总结:
存储过程:
陈述。
视图:
删除语句。
Plenty of info available here
Here is a good summary:
A Stored Procedure:
statement.
A View:
DELETE statement.
SQL视图是一个虚拟表,它基于SQL SELECT查询。视图引用一个或多个现有数据库表或其他视图。它是数据库的快照,而存储过程是一组编译成单个执行计划的 Transact-SQL 语句。
视图是简单地显示存储在数据库表中的数据,而存储过程是一组可以执行的语句。
视图速度更快,因为它显示引用表中的数据,而存储过程执行 SQL 语句。
查看这篇文章:查看与存储过程。正是您正在寻找的
A SQL View is a virtual table, which is based on SQL SELECT query. A view references one or more existing database tables or other views. It is the snap shot of the database whereas a stored procedure is a group of Transact-SQL statements compiled into a single execution plan.
View is simple showcasing data stored in the database tables whereas a stored procedure is a group of statements that can be executed.
A view is faster as it displays data from the tables referenced whereas a store procedure executes sql statements.
Check this article : View vs Stored Procedures . Exactly what you are looking for
首先你需要明白,两者是不同的东西。
存储过程
最适合用于INSERT-UPDATE-DELETE
语句。而视图
用于SELECT
语句。你应该同时使用它们。在视图中您无法更改数据。某些数据库具有可更新的视图,您可以在视图
上使用INSERT-UPDATE-DELETE
。First you need to understand, that both are different things.
Stored Procedures
are best used forINSERT-UPDATE-DELETE
statements. WhereasViews
are used forSELECT
statements. You should use both of them.In views you cannot alter the data.Some databases have updatable Views where you can useINSERT-UPDATE-DELETE
onViews
.除了上面的评论之外,我还想补充几点关于Views的观点。
In addition to the above comments, I would like to add few points about Views.