I can understand wanting to avoid having to use a cursor due to the overhead and inconvenience, but it looks like there's some serious cursor-phobia-mania going on where people are going to great lengths to avoid having to use one.

For example, one question asked how to do something obviously trivial with a cursor and the accepted answer proposed using a common table expression (CTE) recursive query with a recursive custom function, even though this limits the number of rows that could be processed to 32 (due to recursive function call limit in sql server). This strikes me as a terrible solution for system longevity, not to mention a tremendous effort just to avoid using a simple cursor.

What is the reason for this level of insane hatred? Has some 'noted authority' issued a fatwa against cursors? Does some unspeakable evil lurk in the heart of cursors that corrupts the morals of children or something?

Wiki question, more interested in the answer than the rep.

Related Info:

SQL Server Fast Forward Cursors

EDIT: let me be more precise: I understand that cursors should not be used instead of normal relational operations; that is a no-brainer. What I don't understand is people going waaaaay out of their way to avoid cursors like they have cooties or something, even when a cursor is a simpler and/or more efficient solution. It's the irrational hatred that baffles me, not the obvious technical efficiencies.

推荐答案

The "overhead" with cursors is merely part of the API. Cursors are how parts of the RDBMS work under the hood. Often CREATE TABLE and INSERT have SELECT statements, and the implementation is the obvious internal cursor implementation.

Using higher-level "set-based operators" bundles the cursor results into a single result set, meaning less API back-and-forth.

Cursors predate modern languages that provide first-class collections. Old C, COBOL, Fortran, etc., had to process rows one at a time because there was no notion of "collection" that could be used widely. Java, C#, Python, etc., have first-class list structures to contain result sets.

The Slow Issue

In some circles, the relational joins are a mystery, and folks will write nested cursors rather than a simple join. I've seen truly epic nested loop operations written out as lots and lots of cursors. Defeating an RDBMS optimization. And running really slowly.

Simple SQL rewrites to replace nested cursor loops with joins and a single, flat cursor loop can make programs run in 100th the time. [They thought I was the god of optimization. All I did was replace nested loops with joins. Still used cursors.]

This confusion often leads to an indictment of cursors. However, it isn't the cursor, it's the misuse of the cursor that's the problem.

The Size Issue

For really epic result sets (i.e., dumping a table to a file), cursors are essential. The set-based operations can't materialize really large result sets as a single collection in memory.

Alternatives

I try to use an ORM layer as much as possible. But that has two purposes. First, the cursors are managed by the ORM component. Second, the SQL is separated from the application into a configuration file. It's not that the cursors are bad. It's that coding all those opens, closes and fetches is not value-add programming.

Sql相关问答推荐

将有界时间周期作为阶跃函数,其中周期开始返回1,周期结束返回0

为什么postgres中CURRENT_TIMESTAMP的日期与CURRENT_DATE不同?

使用来自不同深度的嵌套组的值执行计算的干净方法?

SQL将 Select 查询作为新列添加到另一个 Select 查询

MariaDB查询在逗号分隔的字符串中查找多个值

Django将字符串筛选为整数?

我怎样才能得到列值对应的最大值在另一个?

SQL:如何在表中同时使用GROUPING和CONDITION?

在SQL中为两个日期之间的每个日期添加行

在MS Access Modern图表的X轴上显示时间值时遇到问题

如何使用jsonn_populate_record()插入到包含IDENTITY列的表中

使用 union 的有序结果获取行数

SQL SUM Filter逻辑解释

根据不同日期标准分配组的逻辑

使用日期和间隔作为键加入 Athena 上的表?

Select 多年的日期范围

PostgreSQL 中的递归树查询

snowflake插入覆盖行为

BigQuery 将一行拆分为多列

Presto SQL - 将 varchar 中的日期转换为日期格式时出现问题