了解MySQL的内存使用情况会导致PHP(PDO) [英] Understanding memory usage of MySQL result in PHP (PDO)
问题描述
我试图了解为什么单个PDO结果的内存使用量如此之高.以下是有关查询/结果的几件事:
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:
- 我要从单个表中提取单个
VARCHAR(6)
列 - 结果少于3万行
- 获取此结果将使用PHP中约12MB的内存(来源:
memory_get_usage
) - 如果我将结果
json_encode
并转储到文件中,则实际数据(以文本形式)仅为〜1MB - 使用在Ubuntu 14.04上部署的PHP7,MySQL 5.7.
- I am pulling a single
VARCHAR(6)
column from a single table - The result is less than 30K rows
- This fetching this result uses ~12MB of memory in PHP (source:
memory_get_usage
) - If I
json_encode
the result and dump it to a file, the actual data (in text form) is only ~1MB - Using PHP7, MySQL 5.7, deployed on Ubuntu 14.04.
我的问题是,11MB的膨胀到底从何而来?如果文本形式的实际数据只有大约1MB,那么11MB似乎只是为了解析PHP中的数据而产生的大量开销.是否有一个原因?我想念什么吗?
My question is, where exactly does the 11MB of bloat come in? If the actual data in text form is only about 1MB, then 11MB seems like a lot of overhead just to parse the data in PHP. Is there a reason for this? Am I missing something?
为澄清起见,我正在寻找有关膨胀原因的技术解释,而不是该问题的解决方法.
Just to clarify, I am looking for a technical explanation as to why the bloat exists, not a workaround for the issue.
推荐答案
好,所以感谢Ollie Jones的回答,我意识到我一直在找错地方来获取我的信息.这不是PDO内存使用问题,而是PHP存储其数组的方式问题. (也许问题不是正确的词,而是它的意思)
Ok, so thanks to Ollie Jones's answer, I realized I've been looking in the wrong place for my information. This isn't a PDO memory usage issue but an issue with the way PHP stores its arrays. (Maybe issue isnt the right word, it is what it is)
经过一番挖掘,我发现了这篇非常有用的文章,对PHP如何为数组元素分配内存进行了详尽的介绍:
After a bit of digging I found this incredibly helpful article which gives a great breakdown of how PHP allocates memory for array elements:
https: //nikic.github.io/2011/12/12/How-big-are-PHP-arrays-really-Hint-BIG.html
剧透警报,它为每个元素使用一吨的内存.显然,它在PHP7中变得更好了.文章指出,对于一个简单的整数数组(在PHP5中),它将使用的内存比整数本身的大小大18倍.由于我看到关联字符串数据增加了12倍,因此id表示,与PHP5相比确实是一个巨大的改进.
Spoiler alert, it uses a TON of memory for each element. Apparently it has gotten a lot better in PHP7. The article states that for a simple integer array (in PHP5) element it will use about 18 times the more memory than the size of the integer itself. Since i'm seeing about a 12* increase on associative string data, id say that is indeed a vast improvement over PHP5.
根据该文章,正在为以下内容分配内存:
According to the article, memory is being allocated for the following:
-
zvalue_value
联合,与PHP允许的弱类型转换有关 - 类型存储和垃圾收集数据
- Zend内存管理器分配
- 哈希表存储桶
zvalue_value
union, which relates to the weak typecasting that PHP allows- Type storage and garbage collection data
- The Zend Memory Manager allocation
- Hash table buckets
如果您对此也有兴趣,我强烈建议您阅读该文章,它是一种快速阅读的书,并且有很多有用的信息.
If you had some interest in this as well, I highly recommend reading that article, its a quick read and has a lot of great information.
再次感谢Ollie Jones向我指出正确的方向.
Thanks again to Ollie Jones for pointing me in the right direction.
这篇关于了解MySQL的内存使用情况会导致PHP(PDO)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!