Understanding memory usage of MySQL result in PHP (PDO)

后端 未结 2 538
执笔经年
执笔经年 2020-12-20 16:56

I am trying to understand why the memory usage for a single PDO result is so high. Here are a few things to know about the query/result:

  • I am pulling a single
2条回答
  •  时光说笑
    2020-12-20 17:17

    It's hard to give a specific answer without seeing your specific code. That being said, PHP data structures like arrays are associative. The PHP designers intentionally made a tradeoff to use extra RAM to save time on array access.

    You can save memory in a couple of ways. For one thing, you can fetch each row of your result set as a numeric, rather than an associative array. Read this. http://php.net/manual/en/mysqli-result.fetch-array.php

    For another thing, PHP slurps all the rows in your result set at once unless you tell it not to. This slurp operation consumes a lot of RAM. You don't need that if you're planning to process your large result set one row at a time. You need an unbuffered query to do that. Read this: http://php.net/manual/en/mysqlinfo.concepts.buffering.php

提交回复
热议问题