游标分类

静态游标

静态游标 (Static Cursor) 是利用暂存资料表作为储存结果集空间的一种游标,它可以让应用程式可以快速的存取结果集,但在静态游标开启期间,任何对资料表所做的变更都不会反映在结果集中;同时,在静态游标中所作的修改,无法反映到资料库中,此种游标是消耗资源度第三的游标。

Defines a cursor that makes a temporary copy of the data to be used by the cursor. All requests to the cursor are answered from this temporary table in tempdb; therefore, modifications made to base tables are not reflected in the data returned by fetches made to this cursor, and this cursor does not allow modifications.

动态游标

动态游标 (Dynamic Cursor) 是可以反映资料库中修改的一种游标,不过它并不会让结果集的位置固定 (随机变动),因此无法确实的以游标位置来判断资料,并且它因为要随时反映资料库的变化,因此伺服器需要消耗较多的资源,此种游标是消耗资源第二高的游标。

Defines a cursor that reflects all data changes made to the rows in its result set as you scroll around the cursor. The data values, order, and membership of the rows can change on each fetch. The ABSOLUTE fetch option is not supported with dynamic cursors.

索引键集型游标

索引键集游标 (Keyset Cursor) 是动态游标的强化版本,借由维护一个资料集位置对应表 (以 SQL Server 为例,会建立在 tempdb 的 keyset 资料表中),以维护在结果集中的顺序不受更新而变化,但这相对的也付出了伺服器效能和资源消耗的代价,因此索引键集游标是最消耗伺服器资源的一种游标,在实务上应避免使用。

仅前移型游标

仅前移型游标 (Forward-Only Cursor) 是一旦将游标往前移时,其走过的游标之前的结果集就会被舍弃,因此应用程式不能再往后移动游标,但也因此让伺服器只需要记住游标在结果集中目前的位置即可,这让它消耗的资源只有游标而已,是最省资源的一种游标,在实务中被广泛使用,像 ADO.NET 的 DataReader 就只限定只能使用 Forward-Only Cursor。

Specifies that the cursor can only be scrolled from the first to the last row. FETCH NEXT is the only supported fetch option. If FORWARD_ONLY is specified without the STATIC, KEYSET, or DYNAMIC keywords, the cursor operates as a DYNAMIC cursor. When neither FORWARD_ONLY nor SCROLL is specified, FORWARD_ONLY is the default, unless the keywords STATIC, KEYSET, or DYNAMIC are specified. STATIC, KEYSET, and DYNAMIC cursors default to SCROLL. Unlike database APIs such as ODBC and ADO, FORWARD_ONLY is supported with STATIC, KEYSET, and DYNAMIC Transact-SQL cursors.

posted @   qanholas  阅读(1493)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
点击右上角即可分享
微信分享提示