微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何更快地从 SQL 中获取大尺寸数据

如何解决如何更快地从 SQL 中获取大尺寸数据

我正在努力加快从 sql 获取大型数据的速度。

这是我的系统信息。

APP

示例代码

OS: Linux
Language: python3
sql library: pymssql

样本数据有 3 万行。

msg= "SELECT TAG_CODE,UI_X,UI_Y,INSERT_TIME FROM dbo.R_TAG_HIST WHERE "
        msg+= "CREATE_TIME > '" + self.start_time + \
            "' AND CREATE_TIME < '" + self.end_time + "' "
        msg += "AND TAG_CODE IN " + \
            str(self.tag_ids).replace("[","(").replace("]",")")
        msg+= " ORDER BY TAG_CODE"

def receive_all_tables(self,msg):
    try:
        # check connection
        try:
            hasattr(self.conn,'_conn')
        except:
            self.connect_db()

        with self.conn.cursor() as cursor:
            cursor.execute(msg)             
            tables = cursor.fetchall()      
            self.conn.commit()                  
        return tables

    except Exception as e:
        exc_type,exc_obj,exc_tb = sys.exc_info()
        print("fail to receive query.",exc_type,exc_tb.tb_lineno,e)

def result_iterator(self,cursor,arraysize=1000):
        # 'iterator using fetchmany and consumes less memory'
        while True:
            results = cursor.fetchmany(arraysize)
            if not results:
                break
            for result in results:
                yield result

def receive_all_tables_by_iterator(self,'_conn')
        except:
            self.connect_db()

        tables=[]
        with self.conn.cursor() as cursor:
            cursor.execute(msg)             
            for result in self.result_iterator(cursor) :
                tables.append(result)       
            # self.conn.commit()                   
        return tables

我想减少获取数据的时间。
我想知道另一种从数据库接收大数据的好方法

请帮帮我:)

解决方法

您可以尝试 multiprocessing 或 spark 以更快地查询大数据

  1. Fastest way to read huge MySQL table in python
  2. https://spark.apache.org/docs/1.5.2/sql-programming-guide.html
,

为了加快查询速度,您可以做的一件事是在 CREATE_TIMETAG_CODE 列上添加索引(如果它们没有)。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?