一次向一个表中插入多条数据,SQL应该怎么样写效率最好?

zhiweixuexi8 2008-10-16 02:47:14
一次向一个表中插入多条数据,SQL应该怎么样写效率最好?

我现在是这样的 效率好吗?还有什么更好的吗?

INSERT Biao SELECT xx,xx,xx,xx,xx
UNION ALL SELECT xx,xx,xx,xx,xx
UNION ALL SELECT xx,xx,xx,xx,xx
UNION ALL SELECT xx,xx,xx,xx,xx
...全文
3006 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
rzhgd 2011-08-04
  • 打赏
  • 举报
回复
楼主,python—mysql

conn=MySQLdb.connect(host="",user="",passwd="",db="", harset="utf8")
cursor=conn.cursor()
param=[(1,2),(3,4)]
sql="insert into table values(%s,%s)"
self.cursor.executemany(sql,param)

executemany(sql,param) // 这条是插入多条,楼主可以去查查


mjjzg 2008-10-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 cpp2017 的回复:]
分开写快

insert into 表 select ..........
;insert into 表 select ........


但可放在一起执行.
[/Quote]
ZKM_ZHANG 2008-10-17
  • 打赏
  • 举报
回复
insert into talbe1
select * from table2
编程有钱人了 2008-10-17
  • 打赏
  • 举报
回复

declare @x int
set @x=0;

while (@x<100000)
begin
insert table ---这里是SQL语句
set @x=@x+1
end

jiang_jiajia10 2008-10-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 cpp2017 的回复:]
分开写快

insert into 表 select ..........
;insert into 表 select ........


但可放在一起执行.
[/Quote]
UP
qaz123456qaz 2008-10-17
  • 打赏
  • 举报
回复
学习一下
Jarvis-Li 2008-10-17
  • 打赏
  • 举报
回复
用存储过程啊
tonyze 2008-10-17
  • 打赏
  • 举报
回复
用dataadapter比较好,但是如果考虑到并发的问题就不好了
可以写一个insert循环执行
LoadingThink 2008-10-17
  • 打赏
  • 举报
回复
1、建立临时表,写组合数据进临时表, 取出插往目标物理表,清空临时表!
就这样做!

2、不清楚你的意思:
如果只是测试
请这样
declare @x int
set @x=0;

while (@x<100000)
begin
insert table ---这里是SQL语句
set @x=@x+1
end
oec2003 2008-10-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 cpp2017 的回复:]
分开写快

insert into 表 select ..........
;insert into 表 select ........


但可放在一起执行.
[/Quote]

up
mjjzg 2008-10-16
  • 打赏
  • 举报
回复
不是太影响速度,随意吧
CloneCenter 2008-10-16
  • 打赏
  • 举报
回复
批量的搞法,可以这样,一次insert一条,用 insert into table(...) values(...) 的方法,可以积攒到 1000 条左右的时候再去提交,这样比插入一条提交一条的速度要快一些,但不是很明显。
zhnzzy 2008-10-16
  • 打赏
  • 举报
回复
都差不多
baobaoxiong_840123 2008-10-16
  • 打赏
  • 举报
回复
存储过程效率高
yagebu1983 2008-10-16
  • 打赏
  • 举报
回复
都差不对!!
jack20080808 2008-10-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 cpp2017 的回复:]
分开写快

insert into 表 select ..........
;insert into 表 select ........


但可放在一起执行.
[/Quote]
就按这种方式写,另外上面的select并不是读取的意思,你别理解错了
zhiweixuexi8 2008-10-16
  • 打赏
  • 举报
回复
select 和 values 有什么区别吗
zhiweixuexi8 2008-10-16
  • 打赏
  • 举报
回复
我在程序里直接写SQL,一次插入多条`数据不是读取的`是输入的```主要就是SQL语句怎么样写效率高````
周公 2008-10-16
  • 打赏
  • 举报
回复
Sqlconnection connection=new SqlConnection();
string sql="INSERT Biao SELECT xx,xx,xx,xx,xx
UNION ALL SELECT xx,xx,xx,xx,xx
UNION ALL SELECT xx,xx,xx,xx,xx
UNION ALL SELECT xx,xx,xx,xx,xx";
SqlComand command=new SqlCommand(sql,connection);
connection.Open();
int count=command.ExecuteNonQuery();
connection.Close();
cpp2017 2008-10-16
  • 打赏
  • 举报
回复
分开写快

insert into 表 select ..........
;insert into 表 select ........


但可放在一起执行.
加载更多回复(3)

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧