I
I
Ilya Loopashko2021-06-09 07:38:08
Laravel
Ilya Loopashko, 2021-06-09 07:38:08

How to add records to the database in batches (for example, 100 pieces)?

Good for everyone. I have two databases, I need to transfer data from one database to another, there are 17 thousand records. I need to transfer data in batches, there is a chunk() method that will divide the data, for some reason it doesn’t work for me, I don’t understand where the error is, maybe I didn’t format it correctly.

How to transfer the data completely, I figured it out.

Here is the code when we pass everything at once

$groups = DB::connection("oracle")->table('STAT.ANSWERS')->get();

        foreach ($groups as $group) {
            DB::connection()->insert('insert into stat (title) values (?)', [$group->competence]);
        }


And here is the code for splitting records and it doesn't work
$groups = DB::connection("oracle")->table('STAT.ANSWERS')->get();

$groups->chunk(100, function ($groups) {
            foreach ($groups as $group) {
                DB::connection()->insert('insert into stat (title) values (?)', [$group->competence]);
            }
            
        });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rPman, 2021-06-09
@deadloop

To quickly add data to the database, you need the following:
* do all operations in one transaction (hundreds of inserts within a transaction) i.e. you start transaction, in a cycle you create 100 records, you close transactions, to repeat with the new data.
* disable indexes and constraints for the time of insertion, especially it makes sense when really a lot of data is transferred, of course after it is enabled (if there are conflicts in the data, then indexes or constraints will not be added with an error)
ps larvel and oracle? I feel some discrepancy, at least in terms of price
Once upon a time, I saw attempts to pull an oracle database into the web, it didn’t work out very quickly and there was a mysql layer as an ingenious solution;)
It's very funny, but then there was a 'political' question, it was necessary to use oracle and nothing else.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question