Select 行来自数据库使用不同的多个 WHERE 条件
Select rows from database using varying multiple WHERE conditions
我有一张地图,上面画了很多,当有人 select 一个或多个地段时,我想从数据库中获取这些地段的信息并 return 它。这些批次由 "lots_list" 中的 ID 标识。
目前我正在使用 for 循环遍历列表并获取数据,将 ID 传递给带有占位符的 where 子句,但这种方式的执行速度相当慢。
def getLotInfo(lots_list):
lots = []
for lot in lots_list:
try:
connection = psycopg2.connect(user=user,
password=password,
host=host,
port=port,
database=database)
cursor = connection.cursor()
Psql_Query = '''SELECT setor, quadra, lote, area_ocupada FROM iptu_sql_completo WHERE sql
LIKE %s'''
cursor.execute(Psql_Query, (lot,))
lots.append(cursor.fetchone())
print(lots)
except (Exception, psycopg2.Error) as error:
print("Error fetching data from PostgreSQL table", error)
finally:
# closing database connection.
if (connection):
cursor.close()
connection.close()
print("PostgreSQL connection is closed")
return lots
lots_list = ["0830480002", "0830480003", "0830480004"]
Lots = getLotInfo(lots_list)
我尝试使用 psycopg2 execute_batch 命令
Psql_Query = '''SELECT setor, quadra, lote, area_ocupada FROM
iptu_sql_completo WHERE sql LIKE %s'''
ppgextra.execute_batch(cursor, Psql_Query, SQLs)
lots.append(cursor.fetchall())
print(lots)
但我收到以下错误 "not all arguments converted during string formatting" 我想那是因为我应该在查询中为列表中的每个项目使用占位符,但是如果列表的大小不断变化,是否会有解决这个问题的方法? ID 并不总是连续的。
我的问题是:有没有比使用 for 循环获得更好性能的方法?
您当前的代码几乎是我在这里想到的最坏情况:
- What are the pros and cons of performing calculations in sql vs. in your application
- What is faster, one big query or many small queries?
Maurice 已经提到了重复连接开销。但即使只有一个连接,这也远非理想。相反,运行 一个 单个查询 并将整个列表 lots_list
作为 Postgres array:
SELECT setor, quadra, lote, area_ocupada
FROM iptu_sql_completo
WHERE sql = ANY (%s);
相关:
- Pass array literal to PostgreSQL function
- IN vs ANY operator in PostgreSQL
我有一张地图,上面画了很多,当有人 select 一个或多个地段时,我想从数据库中获取这些地段的信息并 return 它。这些批次由 "lots_list" 中的 ID 标识。 目前我正在使用 for 循环遍历列表并获取数据,将 ID 传递给带有占位符的 where 子句,但这种方式的执行速度相当慢。
def getLotInfo(lots_list):
lots = []
for lot in lots_list:
try:
connection = psycopg2.connect(user=user,
password=password,
host=host,
port=port,
database=database)
cursor = connection.cursor()
Psql_Query = '''SELECT setor, quadra, lote, area_ocupada FROM iptu_sql_completo WHERE sql
LIKE %s'''
cursor.execute(Psql_Query, (lot,))
lots.append(cursor.fetchone())
print(lots)
except (Exception, psycopg2.Error) as error:
print("Error fetching data from PostgreSQL table", error)
finally:
# closing database connection.
if (connection):
cursor.close()
connection.close()
print("PostgreSQL connection is closed")
return lots
lots_list = ["0830480002", "0830480003", "0830480004"]
Lots = getLotInfo(lots_list)
我尝试使用 psycopg2 execute_batch 命令
Psql_Query = '''SELECT setor, quadra, lote, area_ocupada FROM
iptu_sql_completo WHERE sql LIKE %s'''
ppgextra.execute_batch(cursor, Psql_Query, SQLs)
lots.append(cursor.fetchall())
print(lots)
但我收到以下错误 "not all arguments converted during string formatting" 我想那是因为我应该在查询中为列表中的每个项目使用占位符,但是如果列表的大小不断变化,是否会有解决这个问题的方法? ID 并不总是连续的。
我的问题是:有没有比使用 for 循环获得更好性能的方法?
您当前的代码几乎是我在这里想到的最坏情况:
- What are the pros and cons of performing calculations in sql vs. in your application
- What is faster, one big query or many small queries?
Maurice 已经提到了重复连接开销。但即使只有一个连接,这也远非理想。相反,运行 一个 单个查询 并将整个列表 lots_list
作为 Postgres array:
SELECT setor, quadra, lote, area_ocupada
FROM iptu_sql_completo
WHERE sql = ANY (%s);
相关:
- Pass array literal to PostgreSQL function
- IN vs ANY operator in PostgreSQL