Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从查询中获取MySQL结果集大小_Php_Mysql_Caching - Fatal编程技术网

Php 从查询中获取MySQL结果集大小

Php 从查询中获取MySQL结果集大小,php,mysql,caching,Php,Mysql,Caching,在执行查询时是否可以获取结果集的大小 我需要设置一个适当的MySQL缓存限制(MB),因此我正在尝试一些查询,但我需要知道结果集的大小,以微调缓存配置 当测量查询(或结果)的大小时,query\u cache\u limit究竟做了什么?作为一个组估计,您可以尝试一下 <?php // This is only an example, the numbers below will // differ depending on your system echo memory_get_usa

在执行查询时是否可以获取结果集的大小

我需要设置一个适当的MySQL
缓存限制(MB),因此我正在尝试一些查询,但我需要知道结果集的大小,以微调缓存配置


当测量查询(或结果)的大小时,
query\u cache\u limit
究竟做了什么?

作为一个组估计,您可以尝试一下

<?php
// This is only an example, the numbers below will
// differ depending on your system

echo memory_get_usage() . "\n"; // 36640

$a = mysql_fetch_assoc($result);

echo memory_get_usage() . "\n"; // 57960

unset($a);

echo memory_get_usage() . "\n"; // 36744

?>


echo strlen(序列化(mysql_fetch_assoc($result))

我相信善待动物协会在使用前后使用内存的方法可能是最简单的方法。为了得到更准确的结果,您可以使用
mb_strlen
,但您需要在结果中的每一行和每一行的每一个字段中循环,并在运行时将总数相加。您还需要使用正确的编码


有几种方法可以测量从mysql服务器发送到php进程的数据大小:

纯SQL 执行查询并直接在查询后执行
显示会话状态
。您将获得多个统计信息,包括发送和接收的字节:

Bytes_received  191
Bytes_sent      120
仅从
显示会话状态
查询中减去字节,就得到了准确的值

mysqlnd PHP5.3提供了“MySQL本机驱动程序”,它为您调试连接提供了一些不错的选项

执行查询,然后调用
mysqli\u get\u connection\u stats
。它还返回网络统计信息:

Array
(
    [bytes_sent] => 43
    [bytes_received] => 80
    ...

您需要在这里使用mysqli和mysqlnd,但与纯SQL解决方案一样,您可以获得更准确的数字。

那么您想以字节为单位调整mysql查询结果的大小吗?您使用的是什么mysql_fetch函数?strlen并不总是表示字节长度,因为响应中可能有多字节字符串(占用一个字符但几个字节)非常正确,就像我说的,这只是一个估计值。考虑到这个问题,Camran很可能知道多字节字符串是否存在问题。还考虑串行化字符串可能不代表与数组相比的精确大小。