duansha7025 2014-05-16 23:32
浏览 35
已采纳

在PHP中执行的MySQL查询导致极大的httpd线程

I'm working on an application which, based on a MySQL database, dynamically creates a UI that can be used to create and execute queries against that database. The database I am currently using contains about 400,000 rows with 77 columns.

When I execute a query that will return about half or more of the database an httpd thread is created that continues to grow in size, quickly taking up all the memory on my machine. The raw CSV I imported to create the database is only around 150MB.

I am developing with the Yii framework for PHP. Here is what my query code looks like:

$queryResults = 
    Yii::app()->db->createCommand()
          ->select('*')
          ->from($table_name)
          ->where($where)
          ->queryAll();

In this case $where is a value being passed via AJAX. After performing some checks on the query I then send it back to the client as such:

$response['success'] = true;
$response['results'] = $queryResults;
echo json_encode($response);

Here is a screenshot of what the httpd request created by a large query looks like:

httpd process screen shot

When the process is initially created it jumps up to around 90% CPU usage. This query stayed at around 1.8GB real memory usage and still brought my machine to a halt until I manually stop the process. The machine I am working on at work only has 4GB of RAM.

I'm not sure what I am doing wrong. I have been looking into seeing if apache is configured incorrectly or if I should be using an unbuffered query in PHP but I'm still not sure what I must to do fix this memory issue. Any advice would be greatly appreciated. Thank you!

  • 写回答

3条回答 默认 最新

  • dougang5088 2014-05-18 08:53
    关注

    If you look at the code you are effectively creating three copies of the results.

    1. $queryResults array, which is the results of your query.
    2. $response['results'], which is a copy of $queryResults
    3. The implicit copy created by the json_encode.

    You could try reducing this by creating the JSON on the fly.

    However, to drastically reduce memory issues, use the iterator.

    $dataProvider = new CActiveDataProvider('User');   // <-- This is the model name
    $resultIterator = new CDataProviderIterator($dataProvider);
    foreach($resultIterator as $userRecord) {
       print_r($category);
    }
    

    Reference : http://www.yiiframework.com/doc/api/1.1/CDataProviderIterator

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题