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

2

6

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?

Depside answered 13/9, 2010 at 11:23 Comment(0)
T
9

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
Teammate answered 14/9, 2010 at 19:17 Comment(0)
K
1

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.

Keos answered 17/12, 2016 at 2:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.