简体   繁体   English

存储过程还是视图?

[英]Stored procedure or view?

Currently i am using a lot of stored procedures such as the following: 目前,我正在使用许多存储过程,例如:

CREATE PROCEDURE `get_bindings_chart`(IN in_layout_id TINYINT(3), IN in_game_id SMALLINT(5))
BEGIN
    SELECT  b.normal_group, b.normal_action, b.shift_group, b.shift_action, b.ctrl_group, b.ctrl_action, b.alt_group, b.alt_action, b.altgr_group, b.altgr_action, b.extra_group, b.extra_action, b.image_file, b.key_number
    FROM    bindings as b
    WHERE   b.layout_id = in_layout_id
    AND     b.game_id = in_game_id;
END

Could I create a view to do this? 我可以创建一个视图来做到这一点吗? Which is better? 哪个更好? Thanks. 谢谢。

It is concept about view and stored procedure 是关于视图和存储过程的概念

Views 查看

1.Does not accepts parameters 1.不接受参数

2.Can be used as a building block in large query. 2.可以用作大型查询的构件。

3.Can contain only one single Select query. 3.只能包含一个Select查询。

4.Can not perform modification to any table. 4.不能对任何表进行修改。

5.Can be used (sometimes) as the target for Insert, update, delete queries. 5.可以(有时)用作插入,更新,删除查询的目标。

Stored Procedure 储存程序

1.Accept parameters 1.接受参数

2.Can not be used as a building block in large query. 2.不能用作大型查询的构件。

3.Can contain several statement like if, else, loop etc. 3.可以包含多个语句,例如if,else,loop等。

4.Can perform modification to one or several tables. 4.可以对一张或多张表进行修改。

5.Can not be used as the target for Insert, update, delete queries. 5.不能用作插入,更新,删除查询的目标。

A view is probably better since there's no real logic involved here. 一个视图可能更好,因为这里没有实际的逻辑。

  1. A view can be used in other queries (joins, where clauses, etc.) 视图可用于其他查询(联接,where子句等)
  2. A view can have an additional where clause applied to refine results 视图可以具有附加的where子句以优化结果
  3. A view can have an order by clause applied to, well, order the results appropriately for different situations. 视图可以将order by子句应用于根据不同情况适当地对结果进行排序。

You might consider a stored procedure to add more logic...perhaps doing some aggregations and calculations that cannot be performed in a single statement. 您可能会考虑使用存储过程来添加更多的逻辑……也许会执行某些聚合和计算,而这些聚合和计算无法在单个语句中执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM