RBAR 与基于集合的 SQL 编程

发布于 2024-08-10 11:15:37 字数 374 浏览 3 评论 0原文

阅读了 RBAR< 上的此链接/a> 和这个,我对 RBAR 的理解相当于:

  1. 任何有循环和游标
  2. 任何不基于集合的东西

我知道这听起来有点完全,这就是为什么我问是否有人对基于集合的编程有一个更优雅的解释(在 SQL 上下文中)。

Having read this link on RBAR and this, my understanding of RBAR amounts to this:

  1. Anything that have loops and cursors
  2. Anything that's not set based

I know this sounds kinda wholly which is why I am asking if anyone has a more elegant explanation as to what set-based programming is (within SQL context).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给不了的爱 2024-08-17 11:15:37

基于集合的编程基于集合的数学概念,并且具有一次对整个集合起作用的运算符。过程式(RBAR)编程更多地基于文件和记录的传统计算机概念。因此,要将 X 部门所有员工的工资增加 10%:

基于集合:

UPDATE employees SET salary = salary * 1.10 WHERE department = 'X';

过程式(极端示例,伪代码):

OPEN cursor FOR SELECT * FROM employees;
LOOP
   FETCH cursor INTO record;
   EXIT WHEN (no more records to fetch);
   IF record.department = 'X' THEN
      UPDATE employees
      SET    salary = salary * 1.10
      WHERE  employee_id = record.employee_id;
   END IF
END LOOP
CLOSE cursor;

在过程式版本中,一次仅更新一个员工行;在基于集合的版本中,“部门 X 的员工集合”中的所有行都会立即更新(就我们而言)。

不确定这是否会为您在链接中已经阅读的内容添加任何内容,但我想我会尝试一下!

Set-based programming is based upon the mathematical concept of a set, and has operators that work on a whole set at a time. Procedural (RBAR) programming is based more on the traditional computer concepts of files and records. So to increase the salary of all employees in department X by 10%:

Set-based:

UPDATE employees SET salary = salary * 1.10 WHERE department = 'X';

Procedural (extreme example, pseudo-code):

OPEN cursor FOR SELECT * FROM employees;
LOOP
   FETCH cursor INTO record;
   EXIT WHEN (no more records to fetch);
   IF record.department = 'X' THEN
      UPDATE employees
      SET    salary = salary * 1.10
      WHERE  employee_id = record.employee_id;
   END IF
END LOOP
CLOSE cursor;

In the procedural version, only one employee row is being updated at a time; in the set-based version, all rows in the "set of employees in department X" are updated at once (as far as we are concerned).

Not sure this adds anything to what you will have already read in your links, but I thought I'd have a shot at it!

风吹雨成花 2024-08-17 11:15:37

我将指出基于集合的处理可能涉及循环。如果您想批量处理而不是在一个基于集合的进程中将一百万条记录加载到其中时绑定多个表,您可以循环处理批量记录,这比一次操作一行的游标更快,并且对于您的数据库来说,这可能比执行一个巨大的插入语句要好得多。

一些 RBAR 进程看起来也不像游标或循环。这些将包括相关子查询和许多用户定义的函数。

I will point out that set-based processing can involve loops. If you want to process in batches rather than tie up several tables while you load a million records into them in one set-based process, you can loop through batches of records, this is faster than a cursor which operates one row at a time and may be far better for your database than doing one giant insert statement.

Some RBAR processes don't look like cursors or loops either. These would include correlated subqueries and many user-defined functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文