Search code examples
firebirdfirebird2.1

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?


Solution

  • 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