Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert data into database without waiting for it

Tags:

php

mysql

insert

In my php script,I'm using PDO to insert csv data into database.

$sqlsmttempl = 'INSERT INTO %s (%s) VALUES (%s)';

But it took quite a long time for just few thousands records.How can I prevent clients from waiting for inserting?

I tried INSERT DELAYED , it dont' have any error but it seems no responses at all.

Any hints for it?Thank you.

like image 237
Irene Ling Avatar asked Sep 20 '26 20:09

Irene Ling


1 Answers

The only way to not have the client wait for the server to finish processing is to have the processing occur in another process after you have sent your response.

INSERT DELAYED is a quick-and-dirty way to do this. The database rows to insert will be queued up by MySQL and inserted later. This means there's no way to detect or handle a value error, because after the execute() the row hasn't actually been inserted. Looping through the csv and issuing queries must still be done while the client waits.

If this is still too long you will need to construct or use some kind of spool or task queue. In this design, the request is accepted and then passed to a long-running process or placed in a directory and the web response is sent immediately. Meanwhile, another process running on the server scoops up the requests, processes them, and then records the status somewhere (e.g. a database table). There should be another page the user can go to to monitor the status of their task.

like image 200
Francis Avila Avatar answered Sep 22 '26 09:09

Francis Avila



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!