Aadil Shaik wrote:Hello:
I need to insert millions of records into mysql database. I want to use the batch execute option. But my SQL statements are for different tables and they should execute in the same sequence.
Table 1 insert 1 record
Table 2 insert 1 record using the Table 1's id (key)
Table 3 insert 20 records using the Table 2's id
Table 1 insert 1 record
Table 2 insert 1 record using the Table 1's second record's id
Table 3 insert 20 records using the Table 2's second record's id
right now i am executing one statement at a time. How to change this into batch execution to save time.
Thanking you Aadil
Aadil Shaik wrote:Table 1 insert 1 record
Table 2 insert 1 record using the Table 1's id (key)
Table 3 insert 20 records using the Table 2's id
Aadil Shaik wrote:right now i am executing one statement at a time. How to change this into batch execution to save time.
Roel De Nijs wrote:
Aadil Shaik wrote:right now i am executing one statement at a time. How to change this into batch execution to save time.
Is there any possibility to insert the records table per table? So first insert all records into table 1, then insert all records into table 2 and finally insert all records into table3?
Aadil Shaik wrote:
Roel De Nijs wrote:
Aadil Shaik wrote:right now i am executing one statement at a time. How to change this into batch execution to save time.
Is there any possibility to insert the records table per table? So first insert all records into table 1, then insert all records into table 2 and finally insert all records into table3?
I need to execute the insert statements in a sequence.
I am thinking about doing this way.
May be not a good option but i will continue to think for the best solution.
Ron McLeod wrote:Using Statement.getGeneratedKeys() will save a round-trip to the MySQL database - for example:
Aadil Shaik wrote:I need to execute the insert statements in a sequence.
Roel De Nijs wrote:
Aadil Shaik wrote:I need to execute the insert statements in a sequence.
And is there any specific reason why these statements have to be executed in that sequence. Because the table-per-table option is probably the easiest solution to insert all records in batch (although using LAST_INSERT_ID() as suggested by Brian will do the job as well).
Brian Tkatch wrote:
Ron McLeod wrote:Using Statement.getGeneratedKeys() will save a round-trip to the MySQL database - for example:
Better to do the batch and make no round trips (*or as few as possible).
Dave Tolls wrote:Though, thinking about it, I'm not sure how you'll track the ids down the chain.
Brian Tkatch wrote:
Dave Tolls wrote:Though, thinking about it, I'm not sure how you'll track the ids down the chain.
Variables.
Dave Tolls wrote:
Brian Tkatch wrote:
Dave Tolls wrote:Though, thinking about it, I'm not sure how you'll track the ids down the chain.
Variables.
Well, yes, but I thought we were talking batching here, as in the addBatch method.
Can't use a variable there as the batch hasn't run.
Dave Tolls wrote:OK, so what are your batches doing?
I'd be interested to see a sample.
Take the OPs problem:
As I said, I can see how LAST_INSERT_ID would work for the first child, and the first grandchild, but not the second.
Dave Tolls wrote:The only thing that will do that is the sequence of INSERT statements with retrieving generated keys.
Well, you could do a separate SELECT, but that would involve more round trips (INSERT, SELECT, INSERT etc).
Dave Tolls wrote:I think Oracles RETURNING mechanism is what its JDBC driver uses for the generated keys functionality, under the hood. Though don't quote me on that!
Dave Tolls wrote:Still not convinced re: the use of LAST_INSERT_ID in JDBC batches for this particular problem...but I haven't got anything to try it out on, so it's a fair bit of "gut feeling" on my part, and that's not always reliable.
Aadil Shaik wrote:This is how i implemented, 80% batch execution and 20% individual execution.
Where ever i need to retrieve the id from the database its an individual execution and for the statements which can run independently and don't need their keys are batched.
Roel De Nijs wrote:
Aadil Shaik wrote:This is how i implemented, 80% batch execution and 20% individual execution.
Where ever i need to retrieve the id from the database its an individual execution and for the statements which can run independently and don't need their keys are batched.
So I assume all parent and child records are inserted individually and all grand child records are inserted in batch...
Aadil Shaik wrote:Yes. all parent and some of the child records are inserted individually so that i can get the keys in the java program and the child records for which i don't need keys and all grand child records are executed in batch mode.
There's a hole in the bucket, dear Liza, dear Liza, a hole in the bucket, dear liza, a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|