SQL插入查询性能 [英] Sql insert query performance

查看:72
本文介绍了SQL插入查询性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单个表中插入n条记录。可能有许多并发用户,他们可能会从此表中插入/更新/选择数据。在这样的表中插入1000条记录是更好的方法:

I want to insert n records into a single table. There may be many concurrent users and they may insert/update/select data from this table. What is better way to insert in a such table say 1000 records:


  1. 将单个sql查询发送到具有多个插入的数据库。这样可以将服务器保存到数据库调用中,但是(我不确定)会锁定表,直到插入完成,并且对该表的所有其他查询都将等待。

  2. 将1000条记录分成若干块并发送他们在多个SQL查询。这样一来,其他查询就可以在表上执行了,但是却花了一些时间在服务器上进行数据库调用。

这取决于某件事,还是只有一种始终是最优方法?
这是否取决于在插入数据时是否使用事务?
还有其他更好的方法来执行这样的插入吗?

Is this depends on something, or there is a single way that is always the optimal one? Is this depends whether transactions are used or not, while inserting data? Is there other better ways to perform a such insert?

我使用的数据库是MS SQL,但有趣的是它如何在Oracle等其他数据库中工作。

The database I use is MS SQL, but it's interesting how it works in other DB like Oracle.

推荐答案

这完全取决于您使用的 RDBMS

This totally depends on what RDBMS you are using.

Oracle 中,写操作永远不会阻止读取,这就是为什么您可以安全地一次放置所有数据的原因。但是请注意,这将降低性能,因为并发查询将需要从 UNDO 表空间中获取数据,这将需要额外的读取。

In Oracle, writes never block reads, that's why you can safely put your data all at once. Note, though, that this will degrade performance, since the concurrent queries will need to fetch the data out of UNDO tablespace that will require extra reads.

SQL Server 中,写操作会阻止对受影响的行/页/表进行读取(取决于锁升级问题),除非您设置将交易隔离级别转换为快照

In SQL Server writes do block reads on affected rows / pages / tables (depending on lock escalation issues), unless you set TRANSACTION ISOLATION LEVEL to SNAPSHOT.

在所有允许并发的事务引擎中在进行读写操作时,引擎需要将旧数据和新数据都存储在某个位置,以便可以同时使用。

In all transactional engines that allow concurrent writing and reading, the engine needs to store both old and new data somewhere for it to be available at the same time.

Oracle code>,旧数据将复制到 UNDO 表空间。

In Oracle, old data is copied into the UNDO tablespace.

SQL Server ,它将复制到 tempdb 中(仅当启用 SNAPSHOT 隔离时,否则,否则)

In SQL Server, it gets copied into tempdb (only when SNAPSHOT isolation is enabled, otherwise it's just locked).

这总是需要一些资源(内存或磁盘),并且如果您执行 UPDATE ,您可能会用光这些资源 code>查询影响很多行。

This always requires some resources (memory or disk), and you can run out of these resources if your UPDATE query affects lots of rows.

这篇关于SQL插入查询性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆