How can I export a large volume of data from a DB2 database to a CSV file without consuming much client memory?
Data volume : The result-set contains 100 Million Records, 14GB in size when written to CSV file. As of today.
I am running the db2 export command like so :
db2 "EXPORT TO /mounted/nfs/location/file.csv OF DEL MODIFIED BY NOCHARDEL SELECT * FROM LARGE_TABLE WHERE ID = 123"
This is run via a shell script (.sh file), in a VM of a private cloud.
The problem is that 14GB of VM memory is being consumed when this command is running. And if the data in the table grows any more, the VM will not have enough resources to run this process. I don't really understand why it needs to bring the result-set into the client memory.
Can't DB2 write to the target without consuming so much memory on the VM? Preferably consuming less than 1GB of memory.
I am using an IBM db2 db of version 11.1.3.1
I went through the documentation and it doesn't mention anywhere that the client memory is consumed for the export process https://www.ibm.com/docs/en/db2/11.1?topic=commands-export
Are there no command line arguments to perform this in batches or without consuming client memory?
Yes, as a last resort, I can manually do a batch write with the FETCH & OFFSET options in SQL, but I wanted to check if there are any existing options to do this in a simpler way?
I am avoiding the FETCH & OFFSET because the real SQL query has GROUP-BY operations, and I'm guessing it's going to perform the aggregations each time I run it. Which would put unnecessary load on the DB.
It has a similar structure to this sample query:
SELECT LT.K1, LT.K2, SUM(LT.VAL)
FROM LARGE_TABLE LT
WHERE LT.K3 IN (123, 456)
GROUP BY LT.K1, LT.K2
Note that there is no primary key on this table, so there is no ORDER BY
clause which would have helped with fetching in batches.
SELECT * FROM XYZ WHERE XYZ.col1 = 'foo'
, whereXYZ
has no keys to order by. By the questions in the comments, do I assume that the only way db2 can export is by fetching all the rows of the result-set of the query into the client memory?