何时使用sql以及何时使用存储过程?

时间:2022-09-10 23:26:59

when to use sql and when to use stored procedure?

何时使用sql以及何时使用存储过程?

what is the difference between them? I just know the stored procedure is precomplied and stored in the database server, we can reuse it. but what about sql? for example:

他们之间有什么区别?我只知道存储过程是预先编译并存储在数据库服务器中的,我们可以重用它。但是sql怎么样?例如:

select * from Employee

this sql should be compiled every time we send it to database? and what about other difference?

每次我们将它发送到数据库时都应该编译这个sql吗?其他区别呢?

1 个解决方案

#1


2  

The question can be re-interpreted as "Where do you want to keep your business logic?"

这个问题可以重新解释为“你想在哪里保持业务逻辑?”

Business logic is the core functionality for your system that defines how you want it to work.

业务逻辑是系统的核心功能,它定义了您希望它的工作方式。

You can choose to put your business logic in your DB (ie as stored procedures, stored functions, table constraints, etc) or in your application code (ie in software functions which then generate queries).

您可以选择将业务逻辑放在数据库中(即作为存储过程,存储函数,表约束等)或应用程序代码(即在软件函数中生成查询)。

This is an architectural decision, but in general I would recommend only using SPs where it makes sense (eg for a query that is repeated often in numerous places in your code, or for specific performance in an area where you really need it and where using an SP would make a real difference).

这是一个架构决策,但总的来说,我建议只使用有意义的SP(例如,对于在代码中的许多地方经常重复的查询,或者在您真正需要它的区域中的特定性能以及使用的地方) SP会产生真正的差异)。

Here's a link to an article which may help: http://c2.com/cgi/wiki?BusinessLogicInStoredProcedures

这是一篇可能有用的文章的链接:http://c2.com/cgi/wiki?BusinessLogicInStoredProcedures

#1


2  

The question can be re-interpreted as "Where do you want to keep your business logic?"

这个问题可以重新解释为“你想在哪里保持业务逻辑?”

Business logic is the core functionality for your system that defines how you want it to work.

业务逻辑是系统的核心功能,它定义了您希望它的工作方式。

You can choose to put your business logic in your DB (ie as stored procedures, stored functions, table constraints, etc) or in your application code (ie in software functions which then generate queries).

您可以选择将业务逻辑放在数据库中(即作为存储过程,存储函数,表约束等)或应用程序代码(即在软件函数中生成查询)。

This is an architectural decision, but in general I would recommend only using SPs where it makes sense (eg for a query that is repeated often in numerous places in your code, or for specific performance in an area where you really need it and where using an SP would make a real difference).

这是一个架构决策,但总的来说,我建议只使用有意义的SP(例如,对于在代码中的许多地方经常重复的查询,或者在您真正需要它的区域中的特定性能以及使用的地方) SP会产生真正的差异)。

Here's a link to an article which may help: http://c2.com/cgi/wiki?BusinessLogicInStoredProcedures

这是一篇可能有用的文章的链接:http://c2.com/cgi/wiki?BusinessLogicInStoredProcedures