如何用python保持postgresql数据库连接打开

xqnpmsa8  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(369)

我正在运行一个循环,在每次迭代中,我都去postgres数据库查找某些匹配的记录。我的问题是,每次通过循环打开一个新的数据库连接所花费的时间太长。

import psycopg2
from psycopg2 import sql
from sqlalchemy import create_engine

engine = create_engine(mycredentials)
db_connect = engine.connect()

query = ("""SELECT ("CustomerId") AS "CustomerId", 
                ("LastName") AS "LastName", 
                ("FirstName") AS "FirstName",
                ("City") AS "City", 
                FROM 
                    public."postgres_table" 
                WHERE (LEFT("LastName", 4) = %s 
                    AND LEFT("City", 4) = %s) 
                    OR (LEFT("FirstName", 3) = %s
                    AND LEFT("City", 4) = %s)
""")

for index, row in df_loop.iterrows():
    blocking_lastname = row['LastName'][:4]  
    blocking_firstname = row['FirstName'][:3]  
    blocking_city = row['City'][:4]  

    df = pd.read_sql(query, db_connect, params= {'blocking_firstname': blocking_firstname,
                                        'blocking_lastname': blocking_lastname, 
                                        'blocking_city': blocking_city})

这段代码可以工作,但似乎没有保持数据库连接打开,因为查询有太多的延迟(注意:表列有索引。)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题