STORED PROCEDURES: GOOD OR BAD(存储过程:好还是坏)
2024-07-21 02:07:07
供稿:网友
author
date of submission
user level
saikalyan prasad rao
07/06/2004
intermediate
作者
提交日期
用户级别
saikalyan prasad rao
07/06/2004
中级
i am sure this issue has been taken up and discussed in lots of articles on the net. this article aims to look at both sides of the coin. first we will dwell on the advantages of stored procedures.
我确信这个问题在网上已经被讨论多次了。这篇文章是从两方面讨论。首先我们先详细论述一下存储过程的优点。
stored procedures provide performance benefits such as local to database, pre-compiling and caching, a programming framework with use of input/output parameters, reuse of procedures and security feature such as encryption and privilege limits to users. a part from that it offers modularization of code and changes are immediately affected unlike business components which need to be recompiled and deployed. not forgetting that with the advent of .net, deployment issues have been reduced quite a lot. but nevertheless changes made to any component do need to be rebuilt. the other benefits include saving on round trips to the client apps and reduction of network traffic.
存储过程提供诸如数据本地化、预处理和缓冲等特点,一个使用输入输出参数的架构,可重复使用存储过程和安全特性作为加密手段和权限设定来限制用户。其中一部分来自它的代码模块化,数据变更的时候不必像商业组件那样需要重新编译和部署。别忘了自.net的出现后,部署已经被大大简化了,但对任何组件的更改仍需重新编译。另一个好处就是节省了与客户端应用程序的交互时间和加快网络响应。
but on the flip side stored procedures do come with its own share of problems. debugging and maintenance has always been a known issue and it makes it even the more difficult when developers like me get used to vs.net debugger. on a side note, i do think microsoft has always built a very good debugger in vs/vs.net.
但是,存储过程的另一个副作用则产生于它自身。调试与维护已成为一个众所周知的话题,对于像我这样已习惯了vs.net的调试器的开发者来说,这个可能更为困难。从某个角度来说,我认为微软vs/vs.net调试器并不总是表现得很好。
managing changes in stored procedures and applying service pack releases can be a bit teething at times. apart from this there are issues pertaining to migration. what if your application which was built with sql server needs to be ported to oracle or any other database? it would/is a nightmare converting all those stored procedures and t-sql specific code to a compatible/ansi sql code for that database.
有时存储过程中的变动和应用服务包应用可能有点不便。除了这点之外,还有一个移植方面的问题。如果你的应用程序是用sql sever编写的,但现在要移植到oracle或其他数据库你该怎么办?把所有的存储过程和t-sql代码转向一个与目标数据库相兼容的sql代码简直就是个噩梦。
personally, i would like to go in for stored procedures and leverage most of the database capabilities if i knew my project was going to use a specific database and wouldn’t change. i am sure many must be thinking on the same lines. after all one of the cool features that i liked about sql server was its support for xml. you should try doing bulk updates through xml, works like a charm and that too with less amount of code. in fact in .net, datasets have the capability to output out xml representation of data which saves you the effort of writing code to formulate the xml. pumping in of business logic in stored procedures have been done and makes a lot of sense for small projects. but if you want to scale up your application it poses a problem since your database and business logic get tied to your database tier. i am sure for small projects it wouldn’t matter much but for a large scale enterprise level solution this would at some point in time pose a huge problem.
如果我知道我的项目工程使用一个特定的数据库而且不会更改的话,我提倡使用存储过程。我相信很多人也有同样的想法。毕竟sql server有着一个能够支持xml的特性。你可以通过xml来进行大量的数据更新。事实上,在.net里,数据集能够把已存储的数据以xml的数据表现形式输出xml文件。存储过程中商业逻辑的导入使得很多小项目变得非常容易。但是,如果你想扩展的你应用程序,就会产生一个问题,因为你的数据库和商业逻辑捆绑在你的数据库tier。对于小工程来说这并不重要,但对于大型企业级的解决方案来讲,就可能是个大难题了。
i am sure there will always be two different schools of thoughts on whether or not to use stored procedures. all said and done, it does raise an interesting issue. if we weren’t to use stored procedures, what could be an alternative? different solutions come to mind such as a generic db layer component which would have all ansi sql statements which would allow one to connect to various databases or the ad-hoc sql approach. but both of these approaches do come with its share of hurdles and pitfalls. we all know how brittle ad-hoc scripts are since any small change to the database could have sever impacts on your system. building a generic db component needs to have a properly designed database which would get affected every time your database changes.
我相信对于是否使用存储过程肯定有不同的意见。这会引起一个很有趣的话题。如果我们不使用存储过程的话,会出现什么替代方法呢?不同的解决方案会导致这样的一个数据库层组件的产生:它拥有所有ansi sql表达式来适应多种不同的数据库或是特定的sql方法。但这两种方法都有共同的缺点。我们都知道,特定的脚本是很脆弱的,对数据库中的任何一个很小的变化都可能影响到你的系统。建立一个通用的数据库组件需要一个设计良好的的数据库来接受外界对数据库的改动。
i guess with both sides having its own share of advantages and disadvantages, i feel the best approach would be is to make best of both the worlds. all insertions, updating, selects etc to be done in stored procedures which would enable me to leverage some of the cool features of sql server like xml updates and put the business logic into components which would allow me to easily debug and scale them.
我猜想这两个方面都有它们的优点和缺点,我认为最好的解决方法就是一分为二的看待问题。所有的insert、update、select等都可以在能让我使用的sql server的xml更新功能的存储过程中完成,而把商业业务逻辑放到能让我轻松调试和扩展的组件中去。
the upcoming release of sql server “yukon” and asp.net “whidbey” aims to address these issues. yukon is coming up with inbuilt support for clr. that means we can now code stored procedures in any of the .net languages which is easier to write than t-sql and at the same time leverage the powerful debugging features of vs.net. in asp.net “whidbey” there are plans of introducing a new extensibility point called providers. this new provider model would support many new features likes membership, personalization, role manager, site navigation, build providers, and health monitoring etc. the provider model in asp.net whidbey enables developers to completely un-plug the logic/behavior/data interaction of a particular feature of asp.net and replaces it with one’s own logic/data layer. in short the provider model provides both data and business logic abstraction.
即将发布sql server “yukon”与asp.net “whidbey”都针对这个问题作了讨论。yukon支持clr。这也就意味着我们可以在具有强大的调试功能的vs.net里,使用任意的.net语言来替代t-sql来编写存储过程。在asp.net “whidbey”中,有计划提出一种称为provider的可扩展的方法。这种新的provider model会支持许多新的特性,例如membership、 personalization、role manager、site navigation、build providers和health monitoring等。asp.net whidbey 中的provider model可让开发者完全去除asp.net中的逻辑/行为/数据交互,取而代之的是一个逻辑/数据层。简言之,provider model把数据与商业逻辑抽象化。
both the upcoming releases have tried to bridge the gaps. i for one am eagerly waiting for their respective releases to happen. whether successful or not, only time will tell.
我一直在等待它们各自的版本的发布。无论成功与否,时间会证明一切。