使用 RODBC 的 MS-SQL 批量插入
是否可以使用 RODBC 包向 MS-SQL Server(2000、2005、2008)执行批量插入?
我知道我可以使用 freebcp 来做到这一点,但我很好奇 RODBC 包是否实现了 Microsoft SQL API 的这一部分,如果没有,实现它会有多困难。
Is it possible to perform a bulk insert into an MS-SQL Server (2000, 2005, 2008) using the RODBC package?
I know that I can do this using freebcp, but I'm curious if the RODBC package implements this portion of the Microsoft SQL API and if not, how difficult it would be to implement it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
查看新的
odbc
和DBI
软件包。DBI::dbWriteTable
每秒写入大约 20,000 条记录...比RODBC::sqlSave()
中的行插入快得多check out the new
odbc
andDBI
packages.DBI::dbWriteTable
writes around 20,000 records per second... Much much faster than the Row Inserts fromRODBC::sqlSave()
您可能正在寻找
?sqlSave
< /a> 当您设置Fast=True
时,它会使用参数化INSERT INTO
查询(在一个操作中发生)。You're probably looking for
?sqlSave
which uses a parametrizedINSERT INTO
query (taking place in one operation) when you setFast=True
.现在您可以使用新的 rsqlserver 包中的
dbBulkCopy
:典型场景
假设您的表已在数据库中创建:
Now You can use
dbBulkCopy
from the new rsqlserver package:A typical scenario:
dbBulkCopy
to read fil and insert it using internallybcp
tool of MS Sql server.This assume that your table is already created in the data base:
使用 RODBC,我们能够创建的最快插入(2.6 亿行插入)如下所示(以 R 伪代码表示):
如果您在服务器之间运行此操作,则需要 R 服务器可以使用的网络驱动器写入(例如,一台有权写入数据库的服务器使用 Rscript 来生产代码),并且 SQL Server 可以从中读取。
Using RODBC, the fastest insert we've been able to create (260 million row insert) looks like the following (in R pseudo code):
If you're running this from between servers, you need a network drive that the R server can write to (e.g. one server with permissions for writing to the DB uses Rscript to productionalize the code), and the SQL Server can read from.
从我能找到的所有信息来看,没有批量插入 MySQL 的解决方案,也没有任何可以与 SSIS 配合使用的解决方案,这就是 Microsoft 在购买 Revolution R Analytics 后在 SQL Server 2016 中包含数据库内分析的原因。
我试图对之前的答案发表评论,但没有这样做的声誉。
rsqlserver
包需要与rClr
一起运行,但这两个包都表现不佳,特别是因为rsqlserver
的 INSERT 函数的数据类型很差处理。因此,如果您使用它,您将不知道您在 SQL 表中查看的内容,因为 data.frame 中的大部分信息都已被转换。考虑到 RODBC 包已经存在了 15 年,我对没有人创建批量插入功能感到非常失望......
From everything I can find, there is NO solution for bulk insert to MySQL and nothing that works with SSIS which is why Microsoft is including in-database analytics with SQL Server 2016 after buying Revolution R Analytics.
I tried to comment on the previous answer but don't have the reputation to do it.
The
rsqlserver
package needs to run withrClr
and neither of those packages are well-behaved, especially becausersqlserver
's INSERT functions have poor data type handling. So if you use it, you'll have no idea what you're looking at in the SQL table as much of the information in your data.frame will have been transformed.Considering the
RODBC
package has been around for 15 years, I'm pretty disappointed that no one has created a bulk insert function...我们的
n2khelper
包可以使用bcp
(批量复制)(如果可用)。当不可用时,它会回退到多个 INSERT 语句。您可以在 https://github.com/INBO-Natura2000/n2khelper 上找到该软件包并
安装它使用
devtools::install_git("INBO-Natura2000/n2khelper")
并查找odbc_insert()
函数。Our
n2khelper
package can usebcp
(bulkcopy) when it is available. When not available it falls back to multiple INSERT statements.You can find the package on https://github.com/INBO-Natura2000/n2khelper
Install it with
devtools::install_git("INBO-Natura2000/n2khelper")
and look for theodbc_insert()
function.