Sureshkumar's avatar

Get Inserted Id from DB::insert()

I need to insert more than 100000 records from one of the SOAP response.

I used Laravel eloquent and it took much time, after watching Laracasts https://laracasts.com/series/how-to-build-command-line-apps-in-php, I started to migrate the codes to CLI, and I got very good improvements. I used PDO as he explained in video. It took 30 seconds to insert 30000 records. Then I come back to Laravel and used its 5.1 Command, to rewrite same code, and I used DB facade here.

Issue is, how do I get last inserted id from DB::insert() method?

If you suggests,

$id = DB::table('table')->insertGetId($params);

Then the issue is it took 90 seconds to insert 30000 records.

What are you suggesting me, since I am aware of performance and speed.

0 likes
5 replies
toniperic's avatar

@Sureshkumar not sure what the question is?

Can you post some code as well, as in how you're inserting these 30k records? It shouldn't take that long, no. Are you sure you're not doing 30k queries in a loop?

1 like
Sureshkumar's avatar

Yes I loop, later the SOAP response, we need process the XML and insert each records to database using loop, is there any other way to do this? We can't use bulk insert since there some relationship insertion also need to be executed.

For this relationship only I needed inserted ID.

BayronVazquez's avatar

I think in this case it's necessary to use queues; in processes that take a long time there aren't many alternatives.

Tray2's avatar

You do know that this thread is 11 years old right?

toniperic's avatar

What relationship insertion have to be executed?

You should be able to insert 30000 rows within a matter of seconds, not 130 seconds. Batch inserts would be the way to go.

1 like

Please or to participate in this conversation.