简体   繁体   English

在python中对Postgresql执行多个查询的更快方法

[英]Faster way to execute multiple queries to postgresql in python

I'm writing a script in python where I will need to access a postgresql database multiple times and execute multiple select queries and insert queries. 我正在用python编写脚本,需要多次访问PostgreSQL数据库并执行多个选择查询和插入查询。 I am trying to reduce the time it takes for this script to run. 我正在尝试减少运行此脚本所需的时间。

Currently I have written a secondary function which I pass a qry string, a boolean indicating if I am inserting or recieving data, and a list of parameters and then execute the query: 当前,我编写了一个辅助函数,该函数传递一个qry字符串,一个布尔值(指示是否要插入或接收数据)以及一个参数列表,然后执行查询:

    def sql_call(qry, insert, inputlist):
        params = config_np()
        with psycopg2.connect(**params) as conn:
            cur = conn.cursor()
            try:
                cur.execute(qry, inputlist)
                if insert:
                    conn.commit()
                    sqlrtn = True
                else:
                    sqlrtn = cur.fetchall()
            except (Exception, psycopg2.DatabaseError) as error:
                print(error)
                quit()
    conn.close()
    return sqlrtn

I'm working with a few hundred thousand entries and this takes forever to run. 我正在处理数十万个条目,这需要永远的运行。 Is there a faster way to do it? 有更快的方法吗?

A few things you can do. 您可以做的几件事。 First, don't reestablish the connection with each query. 首先,不要与每个查询重新建立连接。 This can be used over multiple queries so you will not need to recreate it with each query. 可以在多个查询中使用它,因此您无需在每个查询中重新创建它。 If you still want to have the flexibility of having a function to execute the query, create a class where the __init__ method opens the connection and keeps it in self.conn, and a __del__ method that closes the connection. 如果仍然希望具有执行查询功能的灵活性,请创建一个类,其中__init__方法打开连接并将其保留在self.conn中,并创建一个__del__方法关闭连接。

For INSERT operations, you can insert multiple rows if you're using VALUES with, 对于INSERT操作,如果您将VALUES

INSERT INTO table (fld1, fld2, fld3)
    (VALUES ('some', 'data', 'here'),
            ('more', 'data', 'here'));

psycopg2 will take quite a lot rows in a query like this. psycopg2在这样的查询中将占用很多行。 For the SELECT queries, it would depend on what your program is doing. 对于SELECT查询,这取决于您的程序在做什么。 Depending on requirements, you could cache quite of lot a data in memory given the amount available on computers these days. 根据需求,鉴于这些天计算机上可用的数量,您可以在内存中缓存大量数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM