views:

247

answers:

2

Is it possible to get the size of the result-set when doing a query?

I need to set a proper MySql cache_limit (MB) and therefore I am trying out some queries, but I need to know the sizes of the result-sets to fine-tune my cache configurations.

What exactly does query_cache_limit do when measuring the size of a query (or result)...

Any help appreciated!

Thanks

A: 

As a rouph estimate you coud try

<?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

?>

or

echo strlen(serialize(mysql_fetch_assoc($result)));

Petah
strlen won't always be indicative of the byte length, as there may be things like multibyte strings in the response (which take up one character but several bytes)
adam
Very true, like I said this would only be an estimate.Considering the problem it is likly Camran would know if there would be an issue with multibyte strings. Also consider that serialized string might not represent an accurate size compared to a array
Petah
A: 

I believe Petah's method of using memory_get_usage before and after is probably the easiest way. To get a more accurate result, you could use mb_strlen but you'd need to loop through each row in the result and each field of each row, summing up the total as you go. You'd also need to use the correct encoding.

http://us3.php.net/manual/en/function.mb-strlen.php

adam