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.
时间: 2024-09-20 15:47:04

SSCursor in MySQL的相关文章

php列出mysql表所有行和列的方法

 这篇文章主要介绍了php列出mysql表所有行和列的方法,涉及php操作mysql数据库的技巧,具有一定参考借鉴价值,需要的朋友可以参考下     本文实例讲述了php列出mysql表所有行和列的方法.分享给大家供大家参考.具体实现方法如下: 代码如下: <html> <head> <title>Selecting Data</title> </head> <body> <?php $user = "root&quo

mysql 复制表数据,表结构的3种方法

 什么时候我们会用到复制表?例如:我现在对一张表进行操作,但是怕误删数据,所以在同一个数据库中建一个表结构一样,表数据也一样的表,以作备份.如果用mysqldump比较麻烦,备份.MYD,.MYI这样的文件呢,操作起来也还是麻烦. 一,复制表结构 方法1: mysql> create table a like users; //复制表结构 Query OK, 0 rows affected (0.50 sec)   mysql> show tables; +------+ | Tables_i

Sequelize 和 MySQL 对照

如果你觉得Sequelize的文档有点多.杂,不方便看,可以看看这篇. 在使用NodeJS来关系型操作数据库时,为了方便,通常都会选择一个合适的ORM(Object Relationship Model)框架.毕竟直接操作SQL比较繁琐,通过ORM框架,我们可以使用面向对象的方式来操作表.NodeJS社区有很多的ORM框架,我比较喜欢Sequelize,它功能丰富,可以非常方便的进行连表查询. 这篇文章我们就来看看,Sequelize是如何在SQL之上进行抽象.封装,从而提高开发效率的. 安装

MySQL和MongoDB设计实例对比

MySQL是关系型数据库中的明星,MongoDB是文档型数据库中的翘楚.下面通过一个设计实例对比一下二者:假设我们正在维护一个手机产品库,里面除了包含手机的名称,品牌等基本信息,还包含了待机时间,外观设计等参数信息,应该如何存取数据呢? 如果使用MySQL的话,应该如何存取数据呢? 如果使用MySQL话,手机的基本信息单独是一个表,另外由于不同手机的参数信息差异很大,所以还需要一个参数表来单独保存. CREATE TABLE IF NOT EXISTS `mobiles` (     `id` 

mysql-求一个Mysql语句 查询出当前周的数据按照天分组

问题描述 求一个Mysql语句 查询出当前周的数据按照天分组 SELECT DATE_FORMAT(uploadTime_beg%Y-%m-%d"") as time SUM(field01) as sumStatus1 SUM(field02) as sumStatus2 SUM(field03) as sumStatus3 SUM(field04) as sumStatus4 SUM(field05) as sumStatus5 FROM health_realdata WHERE

在MYSQL中插入当前时间,就象SQLSERVER的GETDATE()一样。(见内)

mysql|server|sqlserver|插入 NOW()函数以`'YYYY-MM-DD HH:MM:SS'返回当前的日期时间,可以直接存到DATETIME字段中.CURDATE()以'YYYY-MM-DD'的格式返回今天的日期,可以直接存到DATE字段中.CURTIME()以'HH:MM:SS'的格式返回当前的时间,可以直接存到TIME字段中.例:insert into tablename (fieldname) values (now()) 

Mysql字段长度,供出血者参考,呵呵!

mysql|参考 列类型  需要的存储量  TINYINT  1 字节 SMALLINT  2 个字节 MEDIUMINT  3 个字节 INT  4 个字节 INTEGER  4 个字节 BIGINT  8 个字节 FLOAT(X)  4 如果 X < = 24 或 8 如果 25 < = X < = 53 FLOAT  4 个字节 DOUBLE  8 个字节 DOUBLE PRECISION  8 个字节 REAL  8 个字节 DECIMAL(M,D)  M字节(D+2 , 如果M

在oracle中限制返回结果集的大小,类似于mysql的limit(转译)

mysql|oracle Oracle不支持类似于 MySQL 中的 limit. 但你还是可以rownum来限制返回的结果集的行数. 如果你只希望返回前十行纪录,你可以这样写: SELECT * FROM table WHERE ROWNUM<10; 但是下面的语句是不对的: SELECT * FROM table WHERE ROWNUM>90 AND ROWNUM<100; 这是因为 Oracle 认为这个条件不成立,所以没有返回.你应该这样写: SELECT * FROM tab

使用MySQL时的一些常见错误

mysql|错误 MySQL server has gone away错误本小节也涉及有关Lost connection to server during query的错误. 对MySQL server has gone away错误最常见的原因是服务器超时了并且关闭了连接.缺省地,如果没有事情发生,服务器在 8个小时后关闭连接.你可在启动mysqld时通过设置wait_timeout变量改变时间限制. 你可以通过执行mysqladmin version并且检验正常运行的时间来检查MySQL还没