2

有没有办法使用 MonetDB.R 进行批量插入(而不是通过 for 循环和 dbSendUpdate)?

dbWriteTable 是否允许更新(append=TRUE)?

关于“INSERT INTO”,MonetDB 文档指出:“好处很明显:这非常简单。但是,在 MonetDB 中,这是一种非常低效的处理方式。”

谢谢。

4

4 回答 4

2

I'll consider it. monetdb.read.csv does use COPY INTO, so you might get away with creating a temp. CSV file.

于 2014-07-14T08:24:11.563 回答
2

Hannes 可能有一个更聪明的解决方案,但就目前而言,这可能会有所帮助:)

# start with an example data set
nrow( mtcars )

# and a MonetDB.R connection
db

# here's how many records you'd have if you stack your example data three times
nrow( mtcars ) * 3

# write to three separate tables
dbWriteTable( db , 'mtcars1' , mtcars )
dbWriteTable( db , 'mtcars2' , mtcars )
dbWriteTable( db , 'mtcars3' , mtcars )

# stack them all
dbSendUpdate( db , "CREATE TABLE mtcars AS SELECT * FROM mtcars1 UNION ALL SELECT * FROM mtcars2 UNION ALL SELECT * FROM mtcars3 WITH DATA" )

# correct number of records
nrow( dbReadTable( db , 'mtcars' ) )
于 2014-07-11T14:16:09.103 回答
1

我明白你的意思,尽管这并没有改变 dbWriteTable 使用 for 循环和“INSERT INTO”可能相当慢的事实。我在最初的帖子中可能不是很清楚。

作为一种解决方法,我猜想使用 dbSendUpdate 的“START TRANSACTION”和“COMMIT”可能会起作用。

理想情况下,这样的事情会很棒:

“从 data.frame 复制到表中”

于 2014-07-12T10:14:25.130 回答
1

我们刚刚在 CRAN 上发布了 MonetDB.R 的 0.9.4 版。此版本的主要变化是对 dbWriteTable 方法的重大改进。默认情况下,INSERT 现在被分块为每条语句 1000 行。此外,如果数据库与 R 在同一台机器上运行,您可以使用 csvdump=T 参数。这会将 data.frame 写入本地临时 CSV 文件,并使用自动生成的 COPY INTO 语句进行导入。这两种方法显然都是为了提高 dbWriteTable 导入数据的速度。此外,他追加/覆盖参数处理已得到修复。

于 2014-07-23T07:27:40.637 回答