• <i id='k7x5l'><tr id='k7x5l'><dt id='k7x5l'><q id='k7x5l'><span id='k7x5l'><b id='k7x5l'><form id='k7x5l'><ins id='k7x5l'></ins><ul id='k7x5l'></ul><sub id='k7x5l'></sub></form><legend id='k7x5l'></legend><bdo id='k7x5l'><pre id='k7x5l'><center id='k7x5l'></center></pre></bdo></b><th id='k7x5l'></th></span></q></dt></tr></i><div id='k7x5l'><tfoot id='k7x5l'></tfoot><dl id='k7x5l'><fieldset id='k7x5l'></fieldset></dl></div>

    <small id='k7x5l'></small><noframes id='k7x5l'>

  • <tfoot id='k7x5l'></tfoot>
  • <legend id='k7x5l'><style id='k7x5l'><dir id='k7x5l'><q id='k7x5l'></q></dir></style></legend>

        • <bdo id='k7x5l'></bdo><ul id='k7x5l'></ul>
      1. 大结果集的 PDO/MySQL 内存消耗

        时间:2023-10-05
        <legend id='M5Hgr'><style id='M5Hgr'><dir id='M5Hgr'><q id='M5Hgr'></q></dir></style></legend>
          <tbody id='M5Hgr'></tbody>
        <i id='M5Hgr'><tr id='M5Hgr'><dt id='M5Hgr'><q id='M5Hgr'><span id='M5Hgr'><b id='M5Hgr'><form id='M5Hgr'><ins id='M5Hgr'></ins><ul id='M5Hgr'></ul><sub id='M5Hgr'></sub></form><legend id='M5Hgr'></legend><bdo id='M5Hgr'><pre id='M5Hgr'><center id='M5Hgr'></center></pre></bdo></b><th id='M5Hgr'></th></span></q></dt></tr></i><div id='M5Hgr'><tfoot id='M5Hgr'></tfoot><dl id='M5Hgr'><fieldset id='M5Hgr'></fieldset></dl></div>
            <bdo id='M5Hgr'></bdo><ul id='M5Hgr'></ul>

          • <tfoot id='M5Hgr'></tfoot>

                  <small id='M5Hgr'></small><noframes id='M5Hgr'>

                  本文介绍了大结果集的 PDO/MySQL 内存消耗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我在处理从大约 30,000 行的表中进行选择时遇到了奇怪的时间.

                  I'm having a strange time dealing with selecting from a table with about 30,000 rows.

                  似乎我的脚本使用了大量的内存来实现一个简单的、仅向前遍历查询结果的内容.

                  It seems my script is using an outrageous amount of memory for what is a simple, forward only walk over a query result.

                  请注意,这个例子是一个有点人为的、绝对最低限度的例子,它与真实代码几乎没有相似之处,不能用简单的数据库聚合来代替.旨在说明不需要在每次迭代中保留每一行的观点.

                  Please note that this example is a somewhat contrived, absolute bare minimum example which bears very little resemblance to the real code and it cannot be replaced with a simple database aggregation. It is intended to illustrate the point that each row does not need to be retained on each iteration.

                  <?php
                  $pdo = new PDO('mysql:host=127.0.0.1', 'foo', 'bar', array(
                      PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,
                  ));
                  $stmt = $pdo->prepare('SELECT * FROM round');
                  $stmt->execute();
                  
                  function do_stuff($row) {}
                  
                  $c = 0;
                  while ($row = $stmt->fetch()) {
                      // do something with the object that doesn't involve keeping 
                      // it around and can't be done in SQL
                      do_stuff($row);
                      $row = null;
                      ++$c;
                  }
                  
                  var_dump($c);
                  var_dump(memory_get_usage());
                  var_dump(memory_get_peak_usage());
                  

                  输出:

                  int(39508)
                  int(43005064)
                  int(43018120)
                  

                  我不明白为什么在任何时候几乎不需要保存任何数据的情况下使用 40 兆内存.我已经计算出通过将SELECT *"替换为SELECT home, away",我可以将内存减少大约 6 倍,但是我认为即使这种用法也非常高,而且表只会变得更大.

                  I don't understand why 40 meg of memory is used when hardly any data needs to be held at any one time. I have already worked out I can reduce the memory by a factor of about 6 by replacing "SELECT *" with "SELECT home, away", however I consider even this usage to be insanely high and the table is only going to get bigger.

                  是否有我遗漏的设置,或者 PDO 中是​​否存在一些我应该注意的限制?如果 mysqli 不支持,我很高兴摆脱 PDO 以支持 mysqli,所以如果这是我唯一的选择,我将如何使用 mysqli 执行此操作?

                  Is there a setting I'm missing, or is there some limitation in PDO that I should be aware of? I'm happy to get rid of PDO in favour of mysqli if it can not support this, so if that's my only option, how would I perform this using mysqli instead?

                  推荐答案

                  创建连接后,需要设置PDO::MYSQL_ATTR_USE_BUFFERED_QUERY 为假:

                  After creating the connection, you need to set PDO::MYSQL_ATTR_USE_BUFFERED_QUERY to false:

                  <?php
                  $pdo = new PDO('mysql:host=127.0.0.1', 'foo', 'bar', array(
                      PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,
                  ));
                  $pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
                  
                  // snip
                  
                  var_dump(memory_get_usage());
                  var_dump(memory_get_peak_usage());
                  

                  输出:

                  int(39508)
                  int(653920)
                  int(668136)
                  

                  无论结果大小如何,内存使用量几乎保持不变.

                  Regardless of the result size, the memory usage remains pretty much static.

                  这篇关于大结果集的 PDO/MySQL 内存消耗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PDO::PARAM 日期? 下一篇:PDO 循环通过并打印 fetchAll

                  相关文章

                  最新文章

                  <small id='v68f6'></small><noframes id='v68f6'>

                  1. <tfoot id='v68f6'></tfoot>
                  2. <i id='v68f6'><tr id='v68f6'><dt id='v68f6'><q id='v68f6'><span id='v68f6'><b id='v68f6'><form id='v68f6'><ins id='v68f6'></ins><ul id='v68f6'></ul><sub id='v68f6'></sub></form><legend id='v68f6'></legend><bdo id='v68f6'><pre id='v68f6'><center id='v68f6'></center></pre></bdo></b><th id='v68f6'></th></span></q></dt></tr></i><div id='v68f6'><tfoot id='v68f6'></tfoot><dl id='v68f6'><fieldset id='v68f6'></fieldset></dl></div>
                        <bdo id='v68f6'></bdo><ul id='v68f6'></ul>
                      <legend id='v68f6'><style id='v68f6'><dir id='v68f6'><q id='v68f6'></q></dir></style></legend>