Laravel大量数据库查询导致php进程内存耗尽
2016-09-28 10:53
337 查看
最近做了一个Laravel命令行工具来处理大量用户数据。奇怪的是,每次处理到十多万条数据的时候就报如下内存耗尽错误:
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 72 bytes) in [....]
出现该错误的原因是php.ini中默认设置php进程最大可以使用128M内存,一旦超过就会导致新分配内存失败。
可以使用以下语句将限制临时提高到1G内存:
ini_set('memory_limit', '1024M');
但这种方法毕竟是权宜之计,如果未来数据量指数级增加还是会出问题,所以必须找到内存暴涨的根源。
经过百般查找,把问题定位到了一个不起眼的DB查找语句:
...
$item = DB::table('表名')->where('user_id', $user_id)->where......
...
在Laravel官方文档找到以下说明:
the
To get an array of the executed queries, you may use the
在普通的http中数据库请求语句并不多,所有不会导致问题,但是需要大量数据库查询的命令行工具就显然不能这么干,解决方法是禁用query日志:
DB::connection()->disableQueryLog(); //禁用query log
禁用query日志之后php进程的内存消耗一直维持在二十几兆,问题解决!
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 72 bytes) in [....]
出现该错误的原因是php.ini中默认设置php进程最大可以使用128M内存,一旦超过就会导致新分配内存失败。
可以使用以下语句将限制临时提高到1G内存:
ini_set('memory_limit', '1024M');
但这种方法毕竟是权宜之计,如果未来数据量指数级增加还是会出问题,所以必须找到内存暴涨的根源。
经过百般查找,把问题定位到了一个不起眼的DB查找语句:
...
$item = DB::table('表名')->where('user_id', $user_id)->where......
...
在Laravel官方文档找到以下说明:
Query Logging
By default, Laravel keeps a log in memory of all queries that have been run for the current request. However, in some cases, such as when inserting a large number of rows, this can cause the application to use excess memory. To disable the log, you may usethe
disableQueryLogmethod:
<code class=" language-php" style="font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 11px; direction: ltr; word-spacing: normal; word-break: normal; line-height: 2; vertical-align: middle;"><span class="token scope" style="color: rgb(218, 86, 74);">DB<span class="token punctuation" style="color: rgb(153, 153, 153);">::</span></span><span class="token function" style="color: rgb(85, 85, 85);">connection<span class="token punctuation" style="color: rgb(153, 153, 153);">(</span></span><span class="token punctuation" style="color: rgb(153, 153, 153);">)</span><span class="token operator" style="color: rgb(85, 85, 85);">-</span><span class="token operator" style="color: rgb(85, 85, 85);">></span><span class="token function" style="color: rgb(85, 85, 85);">disableQueryLog<span class="token punctuation" style="color: rgb(153, 153, 153);">(</span></span><span class="token punctuation" style="color: rgb(153, 153, 153);">)</span><span class="token punctuation" style="color: rgb(153, 153, 153);">;</span></code>
To get an array of the executed queries, you may use the
getQueryLogmethod:
<code class=" language-php" style="font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 11px; direction: ltr; word-spacing: normal; word-break: normal; line-height: 2; vertical-align: middle;"> <span class="token variable" style="color: rgb(78, 161, 223);">$queries</span> <span class="token operator" style="color: rgb(85, 85, 85);">=</span> <span class="token scope" style="color: rgb(218, 86, 74);">DB<span class="token punctuation" style="color: rgb(153, 153, 153);">::</span></span><span class="token function" style="color: rgb(85, 85, 85);">getQueryLog<span class="token punctuation" style="color: rgb(153, 153, 153);">(</span></span><span class="token punctuation" style="color: rgb(153, 153, 153);">)</span><span class="token punctuation" style="color: rgb(153, 153, 153);">;</span></code>意思是Laravel框架默认存储每次请求(每次命令行执行也相当于一次请求)的所有数据库查询语句!!!
在普通的http中数据库请求语句并不多,所有不会导致问题,但是需要大量数据库查询的命令行工具就显然不能这么干,解决方法是禁用query日志:
DB::connection()->disableQueryLog(); //禁用query log
禁用query日志之后php进程的内存消耗一直维持在二十几兆,问题解决!
相关文章推荐
- Laravel大量数据库查询导致php进程内存耗尽
- PHP查询大量数据内存耗尽问题的解决方法
- 如何解决PHP查询大量数据内存耗尽的问题
- php查询mysql大量数据占用大量内存导致内存不足
- 如何解决PHP查询大量数据内存耗尽的问题
- 解决Linux下php-fpm进程过多导致内存耗尽问题
- 解决mysqldb查询大量数据导致内存使用过高的问题
- php查询mysql大量数据造成内存不足的解决方法
- 生产上数据库大量的latch free 导致的CPU资源耗尽的问题的解决
- PHP查询MySQL大量数据的内存
- RAC环境产生大量ons进程,导致用户进程资源耗尽,用户切换提示Resource temporarily unavailable
- PHP查询MySQL大量数据的内存占用分析
- PHP查询MySQL大量数据的内存占用分析
- 解决mysqldb查询大量数据导致内存使用过高的问题
- php-fpm进程太多,内存耗尽问题排查
- 如何解决PHP里大量数据循环时内存耗尽的问题
- mysql查询大量数据,php内存占用溢出问题
- 初学php遇见的查询数据库语句出错问题占内存
- PHP查询MySQL大量数据的内存
- 解决:php进程过多,耗用系统大量内存