我正在我的代码中遇到性能问题。我正在进行数据库连接,然后插入一个表。在一个选择查询ID中填充了500行。插入之前我正在运行选择查询8 -9次首先然后插入所有使用cursor.executemany.But它需要2个miuntes插入哪个不qood。任何想法
def insert1(id,state,cursor):
cursor.execute("select * from qwert where asd_id =%s",[id])
if sometcondition:
adding.append(rd[i])
cursor.executemany(indata, adding)
其中rd [i]是记录制作的aray,indata是插入语句
#prog start here
cursor.execute("select * from assd")
for rows in cursor.fetchall()
if rows[1]=='aq':
insert1(row[1],row[2],cursor)
if rows[1]=='qw':
insert2(row[1],row[2],cursor)
答案 0 :(得分:3)
我真的不明白你为什么要这样做。
您希望将“assd”中的行子集插入到一个表中,将另一个子集插入到另一个表中?
为什么不用两个SQL语句来做,结构如下:
insert into tab1 select * from assd where asd_id = 42 and cond1 = 'set';
insert into tab2 select * from assd where asd_id = 42 and cond2 = 'set';
这会大大减少您到数据库的往返次数和客户端 - 服务器流量。它的速度也快了一个数量级。
当然,我还强烈建议您在代码的插入和选择部分中指定列名。