简体   繁体   中英

Insert into MYSQl too slow

User keys in search parameters, then we make a request to a data provider and redirect user to a loading page. The response from the data provider hits a callback url, in which case we parse the results and store about 200 rows into the db. Meanwhile the loading page uses ajax to query the db every second and when the results are all there we display the results to the user.

The issue is that insert into the mysql db is too slow. We know the response back from the data provider comes back within seconds, but the processing of the script and inserting of rows into the db is very slow. We do use multirow insert.

Any suggestions to improve? FYI, the code is hugely long... that's why not displaying right now.

There are multitude of factors affecting your insertions:

1) slow hardware and bad server speeds. Sol : Contact your server administrator

2) Use something other than InnoDB

3) Use a surrogate key , other than your primary key that is numeric and sequential along with your natural primary key.

OR

4) Try this https://stackoverflow.com/a/2223062/3391466 .

Suggestion: Instead of running the code on one page and having the user wait the whole process, why not have the php page store the instructions in a php queue? The instructions would then be executed by a separate php script (for instance a Cron Job) and the user wouldn't have to wait for the whole process to take place.

However, in this situation it would be ideal to let the user know that the changes made can take a bit of time to update.

Cron jobs are very easy to implement. In CPanel there is an option for Cron Jobs where you specify which script you want to run and in which intervals. You can let your script know to run once every 1 minute (or more or less depending on how much demand there is). From there your script would check the queue and could keep on running until the queue is empty again.

Let me know if that helped!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM