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..
我想知道如何在后台运行sql查询...我有一个大查询,花了这么多时间我想把它作为背景运行,这样我的页面加载时间会更少..
5 个解决方案
#1
3
Run the query in a PHP script using cron and cache the result.
使用cron在PHP脚本中运行查询并缓存结果。
#2
3
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.
您可以使用AJAX在单独的脚本中调用查询。当请求页面时,正常处理PHP,呈现并将页面发送给访问者,并立即启动一个AJAX脚本,请求另一个执行慢查询的PHP脚本。查询运行时将加载页面,当您从查询中获得结果时,请使用一些JavaScript来合并结果。这里的好处是,当页面加载时,查询正在运行,所以你要同时执行这两个操作。
Also look at optimizing your query and ensure that you have set an index on your tables to speed up the query.
另请参阅优化查询并确保在表上设置索引以加快查询速度。
#3
2
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.
这里有一些解决方案http://dev.mysql.com/doc/refman/5.0/en/table-locking.html和http://dev.mysql.com/doc/refman/5.5/en/insert.html。
The official mySQL site says:
官方的mySQL网站说:
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");
#4
2
I would suggest the CRON and Cache approach, or implement the use of AJAX.
我建议使用CRON和Cache方法,或者实现AJAX的使用。
With CRON and Cache, run your query, then serialize it and save it to a file for sanity. Call and unserialize when needed.
使用CRON和Cache,运行查询,然后对其进行序列化并将其保存到文件中以保证理智。需要时调用和反序列化。
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.
使用AJAX,当您的页面加载时,调用一个执行并返回查询的文件。这样,您可以在服务器运行查询时显示等待指示符,并且它们彼此分开。
#5
0
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
将查询写入PHP文件,并在要运行它时按时间间隔安排。对于Linux计划CRON或Windows使用任务计划程序其他您可以通过Ajax请求触发它
#1
3
Run the query in a PHP script using cron and cache the result.
使用cron在PHP脚本中运行查询并缓存结果。
#2
3
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.
您可以使用AJAX在单独的脚本中调用查询。当请求页面时,正常处理PHP,呈现并将页面发送给访问者,并立即启动一个AJAX脚本,请求另一个执行慢查询的PHP脚本。查询运行时将加载页面,当您从查询中获得结果时,请使用一些JavaScript来合并结果。这里的好处是,当页面加载时,查询正在运行,所以你要同时执行这两个操作。
Also look at optimizing your query and ensure that you have set an index on your tables to speed up the query.
另请参阅优化查询并确保在表上设置索引以加快查询速度。
#3
2
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.
这里有一些解决方案http://dev.mysql.com/doc/refman/5.0/en/table-locking.html和http://dev.mysql.com/doc/refman/5.5/en/insert.html。
The official mySQL site says:
官方的mySQL网站说:
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");
#4
2
I would suggest the CRON and Cache approach, or implement the use of AJAX.
我建议使用CRON和Cache方法,或者实现AJAX的使用。
With CRON and Cache, run your query, then serialize it and save it to a file for sanity. Call and unserialize when needed.
使用CRON和Cache,运行查询,然后对其进行序列化并将其保存到文件中以保证理智。需要时调用和反序列化。
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.
使用AJAX,当您的页面加载时,调用一个执行并返回查询的文件。这样,您可以在服务器运行查询时显示等待指示符,并且它们彼此分开。
#5
0
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
将查询写入PHP文件,并在要运行它时按时间间隔安排。对于Linux计划CRON或Windows使用任务计划程序其他您可以通过Ajax请求触发它