I'm trying to write to a table in DB2 using overwrite = TRUE
with dbWriteTable()
from the DBI package in R. I would like to specify the table space I am writing to rather than writing to the default space (USERSPACE1). I also would rather not create the table before hand and use append = TRUE
because it tends to take MUCH longer than overwriting (I have roughly 4 million rows with varying number of columns each time I'm writing it; minimum of 100 columns). Overwriting switches the table space back to default.
I first tried creating the table and defining the table space, but that requires appending rather than overwriting. I also tried creating the table in the table space I want it in and then overwriting, but the table switches back to the default table space. The arguments for dbWriteTable do not support altering the table space.
###################################
### Method One - Takes Too Long ###
###################################
# `variable_definitions` defines my variable types
query <- paste(
'
CREATE TABLE MYSCHEMA.TABLE_NAME
(',variable_definitions,')
IN "DESIRED_SPACE" DISTRIBUTE BY HASH ("FIELD_ONE")
COMPRESS YES;
'
, sep = ''
)
# This works to create the table in the desired table space
dbExecute(edw_advanl, query)
## 0
# This takes FOREVER
dbWriteTable(conn = myConn,
name = "TABLE_NAME",
value = myData,
append = TRUE)
##########################################################
### Method Two - Changes the Table Space to USERSPACE1 ###
##########################################################
# `variable_definitions` defines my variable types
query <- paste(
'
CREATE TABLE MYSCHEMA.TABLE_NAME
(',variable_definitions,')
IN "DESIRED_SPACE" DISTRIBUTE BY HASH ("FIELD_ONE")
COMPRESS YES;
'
, sep = ''
)
# Again, this works to create the table in the desired table space
dbExecute(edw_advanl, query)
## 0
# This now puts the table in the default table space
dbWriteTable(conn = myConn,
name = "TABLE_NAME",
value = myData,
overwrite = TRUE)
My desired result is to have myData
(R dataframe) written in the DESIRED_SPACE table space as quick as possible. I'm currently using dbWriteTable
but am open to other methods.