SSCursor in MySQL

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
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.
相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
存储 关系型数据库 MySQL
MySQL
能说下myisam 和 innodb的区别吗? • myisam引擎是5.1版本之前的默认引擎,支持全文检索、压缩、空间函数等,但是不支持事务和行级锁,所以一般用于有大量查询少量插入的场景来使用,而且myisam不支持外键,并且索引和数据是分开存储的。 • innodb是基于B+Tree索引建立的,和myisam相反它支持事务、外键,并且通过MVCC来支持高并发,索引和数据存储在一起。
59 0
|
关系型数据库 MySQL 数据库
mysql实用篇
mysql实用篇
|
SQL 安全 关系型数据库
MySQL写马详解
MySQL写马详解
394 0
|
SQL 搜索推荐 关系型数据库
MySQL的初步使用
引入 在之前的一篇文章中,我们一起过了一遍标准SQL语言的核心操作。标准sql语言适配市面上所有主流的RDBMS(即关系数据库管理系统 Relational DataBase Management System),你与任何一个RDBMS连接后,基本可以直接用所学命令进行操作了。
MySQL的初步使用
|
SQL 存储 安全
MySQL详细讲解
MySQL入门教程整理,涉及MySQL初级篇所有知识点。
MySQL详细讲解
|
存储 SQL 安全
MySQL(十)
MySQL(十),一起来学习吧。
|
关系型数据库 MySQL 数据库
MySQL(九)
MySQL(九),一起来学习吧。
|
存储 Oracle 关系型数据库
MySQL(一)
MySQL(一),一起来学习吧。
MySQL(一)
|
SQL 存储 数据可视化
|
关系型数据库 MySQL
MySQL练习13——where in和not in
MySQL练习13——where in和not in!