Question

I have a few queries that seem to be returning bulky amounts of data, but I would like to know how much.

Short of serializing the data and dumping it into a file, is there a way to programmatically select the size in bytes of a query response?

Was it helpful?

Solution

To do this 100% in the DB, you could create a table from the query results:

 CREATE TABLE db_name.test_table SELECT a v1, b v2 FROM db_name.tbl2;

The query you want the result size of should replace SELECT a v1, b v2 FROM db_name.tbl2

Then get the size of the table

  SELECT round((data_length / 1024 / 1024), 2) "Size in MB" 
      FROM information_schema.TABLES 
    WHERE table_schema = "db_name" AND table_name = "test_table";

Then drop the test table:

 drop table db_name.test_table;

This will just give you the raw data size, but not any additional overhead like data packet headers being transfered back (if your result set is broken into many packets).

There may be even a clever way to do this with temp tables or in a stored procedure where you pass the query into it.

OTHER TIPS

Or you can do it in a query, with no temporary database, like this:

select sum((row_size) / 1024 / 1024) AS `Size (MB)`
from (
  select 
    char_length(column1),
    char_length(column2),
    char_length(column3),
    char_length(column4)
  as row_size 
  from YourTable) AS tbl1

If you have a null value of a column using IsNull(column)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top