Reputation: 101
I want to know how can we run sql query in background ... I have a big query which is taking so much time I want to run this as background so that my page loading time will be less..
Upvotes: 5
Views: 5393
Reputation: 5380
Write the query in your PHP file and schedule it at time or time interval when you want to run it. For linux schedule CRON or Windows use Task Scheduler Else You can trigger it by Ajax request
Upvotes: 0
Reputation: 26380
You could call your queries in separate scripts with AJAX. When the page is requested, process PHP as normal, render and send the page to the visitor, and immediately kick off an AJAX script requesting another PHP script that executes the slow query. The page will be loading while the query runs, and when you get the results back from your query, use a little JavaScript to incorporate the results. The gain here is that while the page is loading the query is running, so you're doing both at once.
Also look at optimizing your query and ensure that you have set an index on your tables to speed up the query.
Upvotes: 4
Reputation: 14237
I would suggest the CRON and Cache approach, or implement the use of AJAX.
With CRON and Cache, run your query, then serialize it and save it to a file for sanity. Call and unserialize when needed.
With AJAX, when your page loads, make a call to a file which executes and returns the query. This way, you can display a waiting indicator while your server runs the query and they are separate from eachother.
Upvotes: 2
Reputation: 16494
Some solutions here http://dev.mysql.com/doc/refman/5.0/en/table-locking.html and here http://dev.mysql.com/doc/refman/5.5/en/insert.html.
The official mySQL site says:
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [INTO] tbl_name [(col_name,...)]
{VALUES | VALUE} ({expr | DEFAULT},...),(...),...
so a real use case would look like
INSERT LOW_PRIORITY INTO tablename (column1,column2) VALUES ("hello","you");
Upvotes: 2