开发者

Maintenance script makes loading slow

I run a script on a website each time the user will visit the homepage. The script will go through each rows in the database and check of its date has expired and archive it if true. However, since it has many rows, it does take some time (like 6 seconds) while loading the homepage.

Firstly, i suggested to use a cron开发者_如何学运维job for the task, but my boss suggested this idea, but i digress...

My question is, is it possible to run the script in parallel, so that, the homepage can continue on to load while the script is also running. Also note that the homepage displays data from the database, so it is quite important that the execution of the script ends before the homepage finishes loading


if you need to run a maintenance script everytime you get a visitor, you site is very broken, and a better idea would be to fix the system so it doesnt need maintaining every second


is it possible to run the script in parallel, so that, the homepage can continue on to load while the script is also running.

Yes. You can call maintenance scripts asynchronously. By setting ignore_user_abort in the maintenance scripts then from the homepage you open socket to that script and then close it right away.

However if you still want to wait until maintenance script has finished it's job before homepage is loaded, there really is no point of doing that. You will still have to wait this 6 seconds anyway.

So the best way to go is to check why do you need to run this script every time, and try to optimize it.

For example if all you need is to set all expired items you can just use a single query instead of checking all rows individually. eg. (UPDATE table SET expired=1 WHERE expiry_date

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜