使用可重用代码编写过程的建议做法是什么?
我想向这里一些更有经验的 Oracle 开发人员咨询后端开发的最佳实践。
我编写了很多以 XML 格式打印数据并由 HTTP 服务使用的包。
为此,我循环遍历游标并使用 htp.p 打印数据。
例如
for i in c_my_cursor loop
htp.p('<element>', i.data_field, '</element>');
end loop;
,现在我听说游标对性能不利(这是真的吗?)。而且,不同的包中使用了类似的游标,我觉得从维护的角度来看,切换到函数会更好。
但是我可以从该函数返回什么?我认为光标不起作用。大家都用什么?
I want to check with some of the more experienced Oracle developers here on the best-practices for backend development.
I write a lot of packages that print data in XML format and are used by HTTP services.
For this purpose, I loop through cursors and print the data using htp.p.
e.g.
for i in c_my_cursor loop
htp.p('<element>', i.data_field, '</element>');
end loop;
Now I've heard that cursors are bad for performance (is this true?). Moreover, there are similar cursors used in different packages, which I feel from a maintenance perspective would be better to switch to functions.
But what can I return from the function? I don't think a cursor would work. What do you folks use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
游标本质上并不是坏事。不好的是逐行处理,而不是使用集合处理。 SQL 就是关于集合的乐趣。游标的问题在于 PL/SQL 开发人员经常自动使用它们;这经常导致我们走 RBAR 路线,而直接的 SQL 语句会更有效。
函数并不比过程更有效。根据您是在执行某项操作还是在检索某项操作来选择函数或过程。
对于您的情况,我会考虑 Oracle 的内置 XML 功能是否适用于您的特定情况。这部分取决于您使用的 Oracle 版本,但自 8i 以来的几乎任何版本都可以使用您发布的特定示例。 了解更多信息。
Cursors are not inherently bad. What is bad is processing Row By Agonizing Row, rather than using set processing. SQL is all about The Joy Of Sets. The problem with cursors is that PL/SQL developers often reach for them automatically; this frequently leads us down the RBAR route, when a straight SQL statement would be more efficient.
Functions are no more efficient than procedures. Choose functions or procedures accoring to whether you're doing something or retrieving something.
In your case I would consider whether Oracle's built-in XML functionality will work in your particular case. This partly depends on which version of Oracle you're using, but pretty much any version since 8i would work with the specific example you posted. Find out more.