减少对数据库的调用次数
Reducing number of calls to Database
我正在使用以下方法进行数据库调用,
for record in records:
num = "'"+str(record['Number'])+"'"
id = "'"+str(record['Id'])+"'"
query = """select col2_text,col3_text from table where id= {} and num = {} and is_active = 'Y';""".format(id,num)
因为它是迭代,其中数据库调用总数等于记录数。我想优化我的调用并进行最少数量的数据库调用,最好是在一次调用中。
您可以将数据库调用的数量减少到一个。您可能想看看 SQL-IN 运算符。
您可以执行以下操作:
values = ""
for record in records:
num = "'"+str(record['Number'])+"'"
id = "'"+str(record['Id'])+"'"
values += "({},{}),".format(num, id)
values = values[:-1]
query = """select col2_text,col3_text from table where (id, num) in ({}) and is_active = 'Y';""".format(values)
我正在使用以下方法进行数据库调用,
for record in records:
num = "'"+str(record['Number'])+"'"
id = "'"+str(record['Id'])+"'"
query = """select col2_text,col3_text from table where id= {} and num = {} and is_active = 'Y';""".format(id,num)
因为它是迭代,其中数据库调用总数等于记录数。我想优化我的调用并进行最少数量的数据库调用,最好是在一次调用中。
您可以将数据库调用的数量减少到一个。您可能想看看 SQL-IN 运算符。
您可以执行以下操作:
values = ""
for record in records:
num = "'"+str(record['Number'])+"'"
id = "'"+str(record['Id'])+"'"
values += "({},{}),".format(num, id)
values = values[:-1]
query = """select col2_text,col3_text from table where (id, num) in ({}) and is_active = 'Y';""".format(values)