将SQL保存在c#源代码或Stored Procs中有哪些优点/缺点?我一直在和一个朋友讨论这个问题,我们正在做一个开源项目(c# ASP。网论坛)。目前,大多数数据库访问都是通过在c#中构建内联SQL并调用SQL Server DB来完成的。所以我在试着确定,对于这个特定的项目,哪个是最好的。
到目前为止,我有:
in Code的优点:
更容易维护-不需要运行SQL脚本来更新查询 更容易移植到另一个DB -没有pros到移植
存储Procs的优点:
性能 安全
将SQL保存在c#源代码或Stored Procs中有哪些优点/缺点?我一直在和一个朋友讨论这个问题,我们正在做一个开源项目(c# ASP。网论坛)。目前,大多数数据库访问都是通过在c#中构建内联SQL并调用SQL Server DB来完成的。所以我在试着确定,对于这个特定的项目,哪个是最好的。
到目前为止,我有:
in Code的优点:
更容易维护-不需要运行SQL脚本来更新查询 更容易移植到另一个DB -没有pros到移植
存储Procs的优点:
性能 安全
当前回答
@Keith
安全?为什么scprocs会更安全?
正如Komradekatz所建议的,您可以禁止访问表(对于连接到DB的用户名/密码组合),而只允许SP访问。这样,如果有人获得了数据库的用户名和密码,他们可以执行SP,但不能访问表或数据库的任何其他部分。
(当然,执行scproc可以给他们所有他们需要的数据,但这将取决于可用的scproc。给他们访问表的权限,他们就能访问所有东西。)
其他回答
存储过程的性能优势通常可以忽略不计。
存储过程的更多优点:
防止反向工程(当然,如果使用加密创建的话) 更好地集中数据库访问 能够透明地更改数据模型(无需部署新客户端);当多个程序访问相同的数据模型时尤其方便
CON
我发现在存储过程中进行大量的处理会使您的DB服务器在扩展您的行为时成为一个单一的不灵活点。
然而,如果您有多个服务器运行您的代码,那么在您的程序中进行所有这些处理而不是sql-server,可能会允许您进行更多的扩展。当然,这并不适用于只进行正常获取或更新的存储procs,而是适用于执行更多处理(如在数据集上循环)的存储procs。
PROS
Performance for what it may be worth (avoids query parsing by DB driver / plan recreation etc) Data manipulation is not embedded in the C/C++/C# code which means I have less low level code to look through. SQL is less verbose and easier to look through when listed separately. Due to the separation folks are able to find and reuse SQL code much easier. Its easier to change things when schema changes - you just have to give the same output to the code and it will work just fine Easier to port to a different database. I can list individual permissions on my stored procedures and control access at that level too. I can profile my data query/ persistence code separate from my data transformation code. I can implement changeable conditions in my stored procedure and it would be easy to customize at a customer site. It becomes easier to use some automated tools to convert my schema and statements together rather than when it is embedded inside my code where I would have to hunt them down. Ensuring best practices for data access is easier when you have all your data access code inside a single file - I can check for queries that access the non performant table or that which uses a higher level of serialization or select *'s in the code etc. It becomes easier to find schema changes / data manipulation logic changes when all of it is listed in one file. It becomes easier to do search and replace edits on SQL when they are in the same place e.g. change / add transaction isolation statements for all stored procs. I and the DBA guy find that having a separate SQL file is easier / convenient when the DBA has to review my SQL stuff. Lastly you don't have to worry about SQL injection attacks because some lazy member of your team did not use parametrized queries when using embedded sqls.
在某些情况下,在代码中动态创建的sql可能比存储的proc具有更好的性能。如果您已经创建了一个存储的proc(例如sp_customersearch),它必须非常灵活,因此具有许多参数,非常复杂,那么您可能可以在运行时在代码中生成一个更简单的sql语句。
有人可能会说,这只是将一些处理从SQL转移到web服务器,但总的来说,这是一件好事。
这种技术的另一个优点是,如果你在SQL分析器中查找,你可以看到你生成的查询,调试它比看到一个存储的带有20个参数的proc调用要容易得多。
在我看来,你不能在这个问题上投赞成票或反对票。这完全取决于应用程序的设计。
我完全反对在三层环境中使用sp,在这种环境中,前端是应用服务器。在这种环境中,应用服务器用于运行业务逻辑。如果你额外使用sp,你就开始在整个系统中分配业务逻辑的实现,谁负责什么就会变得非常不清楚。最终你将得到一个基本上只会做以下事情的应用服务器:
(Pseudocode)
Function createOrder(Order yourOrder)
Begin
Call SP_createOrder(yourOrder)
End
因此,最终你的中间层运行在这个非常酷的4个服务器集群上,每个服务器都配备了16个cpu,实际上它什么都不会做!太浪费了!
如果你有一个臃肿的gui客户端,直接连接到你的数据库或甚至更多的应用程序,这是一个不同的故事。在这种情况下,SPs可以充当某种伪中间层,将应用程序与数据模型解耦,并提供可控的访问。
I'm firmly on the side of stored procs assuming you don't cheat and use dynamic SQL in the stored proc. First, using stored procs allows the dba to set permissions at the stored proc level and not the table level. This is critical not only to combating SQL injection attacts but towards preventing insiders from directly accessing the database and changing things. This is a way to help prevent fraud. No database that contains personal information (SSNs, Credit card numbers, etc) or that in anyway creates financial transactions should ever be accessed except through strored procedures. If you use any other method you are leaving your database wide open for individuals in the company to create fake financial transactions or steal data that can be used for identity theft.
存储的proc也比从应用程序发送的SQL更容易维护和性能调优。它们还允许dba查看数据库结构更改对数据访问方式的影响。我从未遇到过允许动态访问数据库的优秀dba。