0

I try to find size in MB of my query, but I don't understand how to do it. I would like to extract the size of my query in this way:

SELECT size_query FROM my_query

Where 'my_query' it could be: SELECT * FROM MyTable WHERE idFactory = 1

A. Ror
  • 17
  • 1
  • 7

1 Answers1

1

If I understand your question correctly, ajreal has already provided a solution on this StackOverflow question. Quoted:

select sum(row_size) 
from (
  select 
    char_length(column1)+
    char_length(column2)+
    char_length(column3)+
    char_length(column4) ... <-- repeat for all columns
  as row_size 
  from your_table
) as tbl1;

This will give you the size of your query in bytes, divide by 1024 for kilobytes and again for megabytes.

Edit: If you're pulling the full result set back to PHP anyway and want to know the size of it, you could calculate it in PHP using something like this:

<?php

$data = [
    [
        'item' => 'Apple',
        'type' => 'fruit',
        'in_stock' => true
    ],
    [
        'item' => 'Biscuits',
        'type' => 'confectionery',
        'in_stock' => false
    ],
    [
        'item' => 'Milk',
        'type' => 'dairy',
        'in_stock' => true
    ],
];


function get_array_size(&$array)
{
    $size = 0;

    foreach ($array as $key => $value) {
        if (is_array($value)) {
            $size += get_array_size($value);
        } else if (is_string($value)) {
            $size += strlen($value);
        } else if (is_bool($value)) {
            $size += 1;
        } /* else if ( some other type ) {

        } */
    }

    return $size;
}

echo get_array_size($data); // Outputs 43

This may or may not be acceptable to you depending on your use case. If you're looking to measure the physical bytes on the wire, this probably won't be accurate enough.

OdinX
  • 4,135
  • 1
  • 24
  • 33
  • Okay, but if I have 70 columns? I must write every column? And.. If I have some JOIN? You understand, @vimist, that is more complicated to do it.. – A. Ror May 28 '18 at 08:56
  • Yes you have to. – Mike Doe May 28 '18 at 09:06
  • If you're already returning the actual results to your PHP code, it might be worth iterating over the result set and counting the bytes there instead. – OdinX May 28 '18 at 09:21
  • What do you mean @vimist? – A. Ror May 28 '18 at 09:35
  • Once you get your results back from the query in a PHP variable, you could recursively loop over the array and get the size of each value to determine the size of the returned data. – OdinX May 28 '18 at 09:53
  • Can you make an example @vimist? – A. Ror May 28 '18 at 10:17
  • @A.Ror Check my edit – OdinX May 28 '18 at 14:35
  • I believe this doesn't give information in bytes or something, it just gives the number of characters in each field. For example: for id field, whose type is bigint, if we have a value like 15384, char_length() just returns 5, but clearly 15384 does not need 5 bytes to for getting stored right? Please correct me if I'm wrong – Akki Jul 06 '22 at 14:39