Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I measure the amount of space taken by blobs on a Firebird 2.1 database?

I have a production database, using Firebird 2.1, where I need to find out how much space is used by each table, including the blobs. The blob-part is the tricky one, because it is not covered using the standard statistical report.

I do not have easy access to the server's desktop, so installing UDFs etc. is not a good solution.

How can I do this easily?

like image 370
Lars D Avatar asked Sep 13 '10 11:09

Lars D


2 Answers

You can count total size of all BLOB fields in a database with following statement:

EXECUTE BLOCK RETURNS (BLOB_SIZE BIGINT)
AS
  DECLARE VARIABLE RN CHAR(31) CHARACTER SET UNICODE_FSS;
  DECLARE VARIABLE FN CHAR(31) CHARACTER SET UNICODE_FSS;
  DECLARE VARIABLE S BIGINT;
BEGIN
  BLOB_SIZE = 0;
  FOR
    SELECT r.rdb$relation_name, r.rdb$field_name 
      FROM rdb$relation_fields r JOIN rdb$fields f 
        ON r.rdb$field_source = f.rdb$field_name
    WHERE f.rdb$field_type = 261
    INTO :RN, :FN
  DO BEGIN
    EXECUTE STATEMENT
      'SELECT SUM(OCTET_LENGTH(' || :FN || ')) FROM ' || :RN ||
      ' WHERE NOT ' || :FN || ' IS NULL'
    INTO :S;
    BLOB_SIZE = :BLOB_SIZE + COALESCE(:S, 0);
  END
  SUSPEND;
END
like image 147
Andrej Kirejeŭ Avatar answered Nov 14 '22 10:11

Andrej Kirejeŭ


I modified the code example of Andrej to show the size of each blob field, not only the sum of all blobs.

And used SET TERM so you can copy&paste this snippet directly to tools like FlameRobin.

SET TERM #;
EXECUTE BLOCK
RETURNS (BLOB_SIZE BIGINT, TABLENAME CHAR(31), FIELDNAME CHAR(31) )
AS
  DECLARE VARIABLE RN CHAR(31) CHARACTER SET UNICODE_FSS;
  DECLARE VARIABLE FN CHAR(31) CHARACTER SET UNICODE_FSS;
  DECLARE VARIABLE S BIGINT;
BEGIN
  BLOB_SIZE = 0;
  FOR
    SELECT r.rdb$relation_name, r.rdb$field_name 
      FROM rdb$relation_fields r JOIN rdb$fields f 
        ON r.rdb$field_source = f.rdb$field_name
    WHERE f.rdb$field_type = 261
    INTO :RN, :FN
  DO BEGIN
    EXECUTE STATEMENT
      'SELECT SUM(OCTET_LENGTH(' || :FN || ')) AS BLOB_SIZE, ''' || :RN || ''', ''' || :FN || '''
      FROM ' || :RN ||
      ' WHERE NOT ' || :FN || ' IS NULL'
    INTO :BLOB_SIZE, :TABLENAME, :FIELDNAME;
    SUSPEND;
  END
END
#
SET TERM ;#

This example doesn't work with ORDER BY, maybe a more elegant solution without EXECUTE BLOCK exists.

like image 28
peterdd Avatar answered Nov 14 '22 11:11

peterdd