NAME

DBIO::Storage::Statistics - SQL Statistics

VERSION

version 0.900000

SYNOPSIS

my $stats = $schema->storage->debugobj;

# Query timing is always available, even without DBIO_TRACE
say $stats->last_query_elapsed;   # seconds (float) of last query
say $stats->total_elapsed;        # cumulative seconds
say $stats->query_count;          # number of queries executed
$stats->reset_stats;              # reset counters

# Enable trace output to see SQL and timing:
#   DBIO_TRACE=1 ./my_app.pl
# Output:
#   SELECT me.id, me.name FROM artist me: '1', '2'
#     Elapsed: 0.003421s

DESCRIPTION

This class is called by DBIO::Storage::DBI as a means of collecting statistics on its actions. It prints SQL statements and elapsed time when tracing is enabled, and always tracks query timing internally for programmatic access.

To customize statistics collection, subclass this class and override the query_start/query_end methods as discussed in DBIO::Manual::Cookbook.

ATTRIBUTES

_defaulted_to_stderr

Internal flag indicating that debug output currently defaults to STDERR.

silence

Boolean flag to suppress trace output when true.

callback

Optional callback invoked by query_start instead of printing.

last_query_elapsed

The elapsed time (in seconds, as a float) of the most recent query. Always available, even when debug output is disabled.

total_elapsed

The cumulative elapsed time (in seconds) of all queries since the statistics object was created or "reset_stats" was called.

query_count

The number of queries executed since the statistics object was created or "reset_stats" was called.

METHODS

new

Returns a new DBIO::Storage::Statistics object.

debugfh

Sets or retrieves the filehandle used for trace/debug output. This should be an IO::Handle compatible object (only the print method is used). By default it is initially set to STDERR - although see discussion of the DBIO_TRACE environment variable.

Invoked as a getter it will lazily open a filehandle and set it to autoflush (if one is not already set).

print

Prints the specified string to our debugging filehandle. Provided to save our methods the worry of how to display the message.

txn_begin

Called when a transaction begins.

txn_rollback

Called when a transaction is rolled back.

txn_commit

Called when a transaction is committed.

svp_begin

Called when a savepoint is created.

svp_release

Called when a savepoint is released.

svp_rollback

Called when rolling back to a savepoint.

query_start

Called before a query is executed. The first argument is the SQL string being executed and subsequent arguments are the parameters used for the query.

query_end

Called when a query finishes executing. Has the same arguments as query_start. Records the elapsed time and updates "query_count" and "total_elapsed".

reset_stats

Resets "query_count", "total_elapsed", and "last_query_elapsed".

AUTHOR

DBIO & DBIx::Class Authors

COPYRIGHT AND LICENSE

Copyright (C) 2026 DBIO Authors Portions Copyright (C) 2005-2025 DBIx::Class Authors Based on DBIx::Class, heavily modified.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.