SSCursor in MySQL

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS PostgreSQL,高可用系列 2核4GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介:

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 
目录
相关文章
|
SQL 数据库 Python
某站点存在SQL注入,直接可进行脱库(实战可参考此方法)
某站点存在SQL注入,直接可进行脱库(实战可参考此方法)
|
网络协议
aws-vpc-对等连接(不同vpc之间的内网互通)
aws-vpc-对等连接(不同vpc之间的内网互通)
2093 0
aws-vpc-对等连接(不同vpc之间的内网互通)
|
6月前
|
网络虚拟化
配置OptionC方式跨域VPN示例
本文介绍了跨域BGP/MPLS IP VPN的配置方法。公司总部(CE1)与分部(CE2)分别通过不同运营商AS10和AS20接入,同属vpn1。配置思路包括:1) 配置IGP协议实现骨干网互通;2) 配置MPLS基本能力和LDP建立LSP;3) 配置VPN实例并绑定接口;4) 建立EBGP对等体交换路由;5) 在ASBR-PE上发布带标签的路由;6) 配置MP-EBGP对等体关系。操作步骤涵盖IP地址配置、MPLS骨干网互通、VPN实例接入及路由验证,确保跨域通信正常。
|
10月前
|
前端开发 Java 测试技术
语音app系统软件源码开发搭建新手启蒙篇
在移动互联网时代,语音App已成为生活和工作的重要工具。本文为新手开发者提供语音App系统软件源码开发的启蒙指南,涵盖需求分析、技术选型、界面设计、编码实现、测试部署等关键环节。通过明确需求、选择合适的技术框架、优化用户体验、严格测试及持续维护更新,帮助开发者掌握开发流程,快速搭建功能完善的语音App。
|
存储 机器学习/深度学习 编解码
基于STM32的车牌识别系统
基于STM32的车牌识别系统
708 0
|
算法 搜索推荐 C语言
C语言中的经典算法实现
C语言中的经典算法实现
295 1
|
人工智能
LangChain:1. Prompt基本使用
LangChain:1. Prompt基本使用
584 1
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的购物商城小程序的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的购物商城小程序的详细设计和实现(源码+lw+部署文档+讲解等)
174 1
|
XML 安全 Java
Spring高手之路20——深入理解@EnableAspectJAutoProxy的力量
本文详细探讨了Spring框架中的面向切面编程(AOP),特别是通过@EnableAspectJAutoProxy注解来启用和配置AOP的详细过程。从基本的AOP概念介绍到高级配置技巧,文章全面解析了如何在Spring应用中有效地使用AOP来增强代码的模块化和重用性。通过深入的代码示例和解释,本文不仅帮助初学者理解Spring AOP的基础,也使得经验丰富的开发者能够精通其高级特性和性能优化方法。
312 0
Spring高手之路20——深入理解@EnableAspectJAutoProxy的力量
|
Perl
awk的组合模式多条件模式
awk的组合模式多条件模式
722 3

热门文章

最新文章