SSCursor in MySQL

简介:

There are four kinds of cursors available in MySQL client side programming. In this article, I am going to introduce the difference between Cursor the normal one and SSCursor the server side one and how to use SSCursor.
First, let's see the definitions of those four cursors in MySQLdb source code.

class Cursor(CursorStoreResultMixIn, CursorTupleRowsMixIn,
             BaseCursor):

    """This is the standard Cursor class that returns rows as tuples
    and stores the result set in the client."""


class DictCursor(CursorStoreResultMixIn, CursorDictRowsMixIn,
                 BaseCursor):

     """This is a Cursor class that returns rows as dictionaries and
    stores the result set in the client."""


class SSCursor(CursorUseResultMixIn, CursorTupleRowsMixIn,
               BaseCursor):

    """This is a Cursor class that returns rows as tuples and stores
    the result set in the server."""


class SSDictCursor(CursorUseResultMixIn, CursorDictRowsMixIn,
                   BaseCursor):

    """This is a Cursor class that returns rows as dictionaries and
    stores the result set in the server."""

In fact, there are two orthogonal features of cursors in MySQLdb. CursorDictRowsMixIn/CursorTupleRowsMixIn controls result type and CursorStoreResultMixIn/CursorUseResultMixIn which we will focus on in this article controls the result will be store in server or client.
With the definitions, we know that SSCursor stores result in the server rather than in the client.

server side VS client side

Two simple flows show how they works.

  • client side
  # mysql_store_result
  while(True):
    result = produce_part_result() # find part of results that can be filled into a network package
    fill_network_package(result)
    send_network_package
  • server side
  # mysql_use_result
  while(True):
    result = produce_part_result() # find part of results that can be filled into a network package

    wait_until_the_client_invoke_`mysql_fetch_row()`

    fill_network_package(result)
    send_network_package

In fact, the MySQL server will not store results in server even if a SSCursor is used, it will produce the part of results before fill them into the network packages. So don't worry the memory use of the server when use SSCursor.

how to use SSCursor

  cur = conn.cursor(MySQLdb.cursors.SSCursor)

Here is a trap that commit should be invoked after all of the results have been sent to client. Otherwise, 2014, "Commands out of sync; you can't run this command now" error will be raised.

If you get Commands out of sync; you can't run this command now in your client code, you are calling client functions in the wrong order. This can happen, for example, if you are using mysql_use_result() and try to execute a new query before you have called mysql_free_result(). It can also happen if you try to execute two queries that return data without calling mysql_use_result() or mysql_store_result() in between.

commit is also a query, or we can say request, so the program should read all of the results then invoke commit.

advantages and disadvantages of using SSCursor

  • Less memory use in the client.
  • Get the first row more quickly.
  • The whole results sending will be slower.
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
目录
相关文章
|
10月前
|
机器学习/深度学习 人工智能 自然语言处理
微调与推理:解锁大模型定制化能力的核心密钥
本文深入浅出讲解大模型微调与推理的核心原理与实践:微调(如LoRA)让通用模型适配垂直场景,推理则实现价值落地。以LLaMA为例,手把手演示数据准备、平台微调、在线测试与效果评估,零代码门槛,助初学者快速打造专属AI模型。(239字)
|
5月前
|
数据采集 人工智能 自然语言处理
自动化比价系统:从采集到数据清洗,全链路打通教程
本文详解淘宝/京东/拼多多三平台自动化比价系统全链路:用OpenClaw自然语言采集、站大爷隧道代理防封(24小时成功率98.2%+)、AI智能清洗价格(统一格式、核销优惠、去重校验),自动生成可决策的比价报告与飞书预警,真正实现“采得稳、洗得准、用得上”。
490 1
|
5月前
|
数据采集 人工智能 自然语言处理
AI时代企业如何构建面向未来的智能业务体系
本文探讨企业从“用上AI”迈向“AI原生”的系统性转型路径,强调这不仅是技术升级,更是战略、流程、数据基建与组织文化的全面重塑。聚焦四大核心:精准定位AI在行业中的变革类型(效率颠覆/决策增强/体验重塑);围绕“人+AI”重构业务流程;夯实非结构化数据治理与能力中心架构;培育AI业务架构师等新角色,推动文化转向“容错迭代”与“数据+经验双驱动”。
|
人工智能 监控 机器人
从会说话到能落地:「AI外呼」为什么需要端到端一站式解决方案?
智能外呼不止是“让AI说话”,而是融合通信资源、大模型对话、实时运营与合规风控的端到端解决方案。阿里云智能联络中心2.0提供通信智能引擎(PaaS)与通信智能体(MaaS)双模式,支持号码、线路、AI、多触点协同及多智能体动态优化,真正实现“越打越聪明”的一站式解决方案。
652 0
|
6月前
|
安全 API 数据库
用办公Agent自动处理员工入职/离职:账号开通、权限回收、文档交接
本文揭秘HRBP小雅如何告别重复劳动:曾耗时40分钟/人入职、60分钟/人离职,年均百小时“开关账号”。引入办公Agent后,仅需飞书发送一句指令(如“李明入职,研发部后端”),即可全自动完成跨7大系统权限配置与回收,并支持断点续传、失败重试、冷静期、撤销离职等安全机制。让HR回归人才战略本源。(239字)
724 0
|
JSON 监控 API
京东商品详情API秘籍!轻松获取商品详情数据
京东商品详情API提供商品SPU/SKU的完整信息,涵盖基础属性、价格、库存及促销等120+字段,支持HTTPS协议与JSON格式,适用于电商多场景。
|
12月前
|
机器学习/深度学习 人工智能 自然语言处理
大模型专业名词解释手册
本文系统介绍了大语言模型(LLM)的核心概念、训练方法、优化技术、应用模式及伦理问题,涵盖Transformer架构、注意力机制、预训练与微调、提示工程、模型压缩、安全对齐等关键技术术语,全面解析大模型的工作原理与发展挑战,助力深入理解生成式AI的技术脉络与未来方向。
1045 0
|
12月前
|
SQL 安全 JavaScript
黑名单和白名单
应用内防御SQL注入主要有黑名单和白名单两种输入验证方法。黑名单过滤已知恶意字符,易实现但防护有限;白名单仅允许预定义的合法字符,更安全有效。应根据输入类型定制规则,并结合警报机制应对异常输入,确保及时发现潜在攻击或误判。客户端验证可提升体验,但不可依赖,服务端验证必不可少。
|
人工智能 运维 专有云
持续领先!阿里云入选2025年Gartner®分布式混合基础设施魔力象限
近日,Gartner发布2025年《分布式混合基础设施魔力象限》报告,在混合云场景下,阿里云凭借飞天企业版(Apsara Stack)、边缘云ENS和云盒CloudBox产品组合能力,在“执行能力”和“愿景完整性”两大维度分别处于亚太厂商中最高最远的位置。
956 6
|
消息中间件 JavaScript 安全
HarmonyOSNext性能核弹:用Node-API引爆ArkTS/C++跨语言
本文介绍HarmonyOS Next中通过Node-API实现ArkTS与C++的跨语言交互,适合教育科普学习。内容涵盖三步核心流程:C++模块注册(开店准备)、接口映射(设计菜单)及ArkTS调用(点外卖)。重点包括SO库命名规则、线程安全规范及实战示例(如两数相加)。附带血泪经验总结,助你掌握丝滑跨语言调用技巧。