首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python数据库插入

python数据库插入
EN

Stack Overflow用户
提问于 2009-08-13 11:49:33
回答 1查看 301关注 0票数 0

我在代码中遇到了一个性能问题。我创建了一个数据库连接,创建了一个select查询,然后在table.Around中插入500行在一个select查询中it填充了.Before插入我先运行select查询8-9次,然后插入,然后全部使用cursor.executemany.But插入需要2分钟才能插入,这不是一个好的.Any想法

代码语言:javascript
复制
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)

其中,rdi是记录生成的aray,indata是insert语句

代码语言:javascript
复制
#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)
EN

回答 1

Stack Overflow用户

发布于 2009-08-13 12:59:48

我真的不明白你为什么要这么做。

您似乎想要将"assd“中的行子集插入到一个表中,而将另一个子集插入到另一个表中?

为什么不使用两个SQL语句,结构如下:

代码语言:javascript
复制
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';

这将极大地减少到数据库的往返次数和客户端-服务器流量。它的速度也会快一个数量级。

当然,我也强烈建议您在代码的insert和select部分指定列名。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1271502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档