Batch Loading of Data¶
Let us understand how we should take care of loading data in batches. We will perform load using multiple approaches to understand which one is better.
Approach 1: Insert and commit each record. Whenever there is a commit in database, there is considerable amount of overhead.
Approach 2: Insert one record at a time, but commit at the end.
Approach 3: Insert all records at once and commit at the end.
Approach 4: Insert records in chunks or batches and commit per chunk or batch.
We should follow the fourth approach while dealing with huge amounts of data. It will facilitate us to restartability or recoverability.
order_id | order_date | order_customer_id | order_status | |
---|---|---|---|---|
0 | 1 | 2013-07-25 00:00:00.0 | 11599 | CLOSED |
1 | 2 | 2013-07-25 00:00:00.0 | 256 | PENDING_PAYMENT |
2 | 3 | 2013-07-25 00:00:00.0 | 12111 | COMPLETE |
order_item_id | order_item_order_id | order_item_product_id | order_item_quantity | order_item_subtotal | order_item_product_price | |
---|---|---|---|---|---|---|
0 | 1 | 1 | 957 | 1 | 299.98 | 299.98 |
1 | 2 | 2 | 1073 | 1 | 199.99 | 199.99 |
2 | 3 | 2 | 502 | 5 | 250.00 | 50.00 |
Note
Inserting and committing one row in each iteration. Commit is quite expensive as it result in database checkpoint.
Note
Inserting one row at a time but committing at the end. Even though it is much faster than previous approach, it is transferring one record at a time between Python Engine and Database Engine.
We can further tune by leveraging batch insert.
Note
All the records will be inserted as part of one batch insert operation. If there is lot of data to be inserted, then this might start running into issues such as out of memory.
Also, if the job fails in the middle then all the data that is transferred thus far will be lost. Hence it is better to batch with manageable size and then insert as well as commit.
Note
You might not see significant difference in performance as our database is running in the same server from where the code is running to insert the data.