NAME
DBIx::QuickORM::Manual::Optimizations - What is fast, what is slow, and the faster alternatives, with numbers.
DESCRIPTION
When something feels slow, find it here. Each section says what the default costs, what is faster, and what the faster form gives up, with timings next to DBIx::Class and raw DBI for scale.
Start with "IF SOMETHING FEELS SLOW".
THE NUMBERS
Collected on 2026-11-28 from commit 1ba80cb5, except the GROUP BY figures, re-measured on 2026-11-29 from commit 8f4dfb70, and the insert_many figures, re-measured on 2026-11-29 from commit ef46fcc1. One machine (16 cores, 32 threads), throwaway servers with their data on tmpfs: two MariaDB releases, two MySQL releases, PostgreSQL, and SQLite (the benchmark records which). DBIx::Class ran with InflateColumn::DateTime and a JSON inflator.
Times are in microseconds (µs, millionths of a second) unless marked ms (milliseconds, thousandths of a second) or s (seconds). 1 ms is 1,000 µs, and 1 s is 1,000 ms. To read a time as a rate, divide one second by it: 15 µs a call is about 67,000 calls a second, and 2 µs a row is 500,000 rows a second.
An operation's time is the median of five passes, each pass the median of nine runs. A startup time is the median of five fresh processes after a warmup. A range spans the six databases unless it says otherwise. Reads of many rows are per row. The benchmark lives in the repository's agent_scripts/bench (not in the CPAN distribution).
These numbers will grow stale as QuickORM changes. This page may be updated from time to time. Treat them as orders of magnitude, and measure your own code before and after a change.
SCHEMA
The examples use PostgreSQL and this schema:
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE,
prefs JSON,
referred_by INTEGER REFERENCES users (id),
created TIMESTAMP NOT NULL DEFAULT now()
);
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users (id),
title TEXT NOT NULL,
views INTEGER NOT NULL DEFAULT 0
);
IF SOMETHING FEELS SLOW
Reading many rows you do not change
Use
barefor plain hashrefs, about 2.1x to 2.5x faster than row objects. See "READING MANY ROWS".Changing rows you read as
barehashrefsUse the ORM's
cas_update, which writes them safely without a canonical copy. See "READING MANY ROWS".Reading many rows as row objects you will not write
Use
no_cacheto skip the row cache, about 1.2x faster.Reading a JSON or date field you only display or pass on
Use
raw_fieldto skip inflation. See "READING FIELDS".Large fields you rarely read
Leave them out with
auto_omitoromit, and ask for them withfieldswhen you do. See "LEAVING FIELDS OUT".A loop of
findcallsFetch the rows with one handle and
-in. See "FINDING ROWS".A loop of
insertcallsUse
insert_many, in void context when you need nothing back. See "WRITING".A loop of
updatecalls making the same changeUse a handle's
update: one UPDATE changes every matching row.A loop of small transactions
When the whole loop should commit or roll back together, wrap it in one transaction.
Writes got slower after turning on
debugTurn it off outside debugging. See "DEBUG".
A slow start with many tables
Scan instead of loading table classes, and turn off
autotypewhen you do not need types. See "STARTUP".
READING MANY ROWS
my @rows = $orm->handle('posts')->all; # row objects, canonical, in the row cache
my @fresh = $orm->handle('posts')->no_cache->all; # row objects, not in the row cache
my @data = $orm->handle('posts')->data_only->all; # hashrefs, still in the row cache
my @bare = $orm->handle('posts')->bare->all; # hashrefs the row cache never sees
my $views = $bare[0]->{views};
Fetching 1000 posts and reading one field, per row:
Default row objects: 1.8 to 2.0 µs
no_cache: 1.5 to 1.6 µsdata_only: 1.0 to 1.3 µsbare: 0.73 to 0.96 µsDBIx::Class objects: 1.7 to 1.9 µs
DBIx::Class HashRefInflator: 0.98 to 1.19 µs
Raw DBI hashrefs: 0.55 to 0.83 µs
A handle fetches its rows together. Walking those 1000 one at a time with next costs 2.7 to 2.9 µs a row, against 4.3 to 4.5 µs for DBIx::Class.
What each gives up, on a table with a primary key (a table without one never goes through the row cache, and its rows cannot be refreshed or updated one by one):
Default rows
Each row goes into the row cache, so objects for that row share the current canonical copy until a write or a changed or narrower read replaces it (DBIx::QuickORM::Manual::Caching). Skipping the cache saved 16% to 21% of the default read time.
no_cacheRows are full row objects, but not canonical. Writing one needs
cas_updateorenforce_canon => 0, or arefreshfirst.data_onlyHashrefs, but still in the row cache. It saves building row objects. Each hashref is the cache's own copy, so never change it.
bareHashrefs straight from the fetch: no inflation, no row methods, no cache. The ORM's CRUD operations take a hashref wherever a row is expected ("HASHREFS" in DBIx::QuickORM::Manual::RowClasses). A
barehashref is not canonical, so write it with the ORM'scas_update, which compares the fields it changes with what the hashref holds and writes only while the database still holds them. It returns the new row data, or undef when a compared field changed since it was read or the row is gone:my ($post) = $orm->handle('posts')->where({id => $post_id})->bare->all; my $saved = $orm->cas_update(posts => $post, {views => $post->{views} + 1});
READING FIELDS
my $user = $orm->find(users => $id);
my $prefs = $user->field('prefs'); # a Perl hash, decoded on every read
my $json = $user->raw_field('prefs'); # the stored text
my $year = substr($user->raw_field('created'), 0, 4); # no DateTime built
A DateTime or UUID field inflates on first read, and the row keeps the inflated value. JSON is decoded again on every read, which is faster than copying decoded data, so changing it cannot change the row. A DateTime is built only when used.
Fetching 1000 users and reading one typed field, per row:
JSON with
field: 3.4 to 3.9 µsJSON with
raw_field: 2.6 to 3.1 µsDBIx::Class JSON: 3.7 to 4.1 µs
DateTime year with
field: 23 to 24 µsDate text with
raw_field: 2.3 to 2.9 µsDBIx::Class DateTime year: 23 to 24 µs
QuickORM's JSON read took 4% to 8% less time than DBIx::Class's on every database, each reading the field once per row. For a 6 KB document read whole, QuickORM took 33 to 58 µs a row against 62 to 87 µs.
Building the DateTime is nearly all of the date cost, for both ORMs. When the stored text is enough, raw_field skips it.
LEAVING FIELDS OUT
my $lean = DBIx::QuickORM->new(
connect => $connect,
auto_omit => {types => ['JSON']}, # leave JSON fields out of default selects
);
my @names = map { $_->field('name') } $lean->handle('users')->all; # prefs never fetched
# asked for up front: no extra query per row
my @prefs = map { $_->field('prefs') } $lean->handle('users')->fields([qw/name prefs/])->all;
auto_omit leaves fields out by ORM type or database type, on every table with a primary key whose default fields no omit, default_fields, or given db_map sets. It keeps primary, unique, and foreign key fields. omit => {users => ['prefs']} leaves out named fields of one table, and default_fields lists what to keep instead. omit refuses primary key fields, and default_fields adds them ("Constructor" in DBIx::QuickORM).
MariaDB reports JSON columns as text, so name their type with types (the benchmark did) or match them with db_types.
Fetching 1000 rows and reading a plain field, per row:
Documents with about 6 KB of JSON, default: 3.2 to 5.6 µs, and 25 to 26 µs on MySQL
The same with
auto_omit: 1.7 to 2.2 µsDBIx::Class, every column: 2.9 to 6.1 µs, and 26 to 27 µs on MySQL
DBIx::Class, listing columns: 1.6 to 1.9 µs
Users with small JSON, default: 2.1 to 2.6 µs
The same with
auto_omit: 2.0 to 2.2 µs
The gain grows with the size of the field left out. MySQL is slow to send large JSON (raw DBI took 25 to 26 µs a row there too), so leaving it out gains most there, about 13x. For small fields it saves 6% to 20% of the time.
The cost comes when code reads a field that was left out. On a row object, the first read of it sends a query for that row. A bare or data_only hashref just lacks the key. Either way, name it in fields when you need it. Without that, each row fetches it with its own query:
Small JSON read with
auto_omitand nofields: 33 to 46 µs, against 3.4 to 3.9 µs by defaultLarge JSON, the same: 65 to 111 µs, against 33 to 58 µs by default
Large JSON with
fieldsasking for it: 35 to 59 µs
So leave out what is rarely read, and name it in fields on the handles that read it.
Leaving fields out also changes what stays canonical. A read that lacks a field the canonical copy holds replaces that copy. So with auto_omit or omit, listing a table replaces the canonical copy of every row object that holds a field the listing left out. For example, insert a row with its JSON, list the table, then update the row: the update croaks, saying the row data is no longer canonical. Fix it with refresh before the write, with cas_update, or by listing every field the row holds in the listing's fields, since fields replaces the default fields ("FIELDS LEFT OUT OF A SELECT" in DBIx::QuickORM::Manual::Caching). A row's canon_status says why, and with debug which call replaced it ("WHY A ROW IS NOT CANONICAL" in DBIx::QuickORM::Manual::Caching).
FINDING ROWS
my @users = $orm->handle('users')->where({id => {-in => \@ids}})->all; # one query
Finding a user by primary key, per find:
find, row not held: 13 to 22 µsfind, the canonical copy already holding every field asked for: 4.3 to 4.6 µs, with no queryDBIx::Class
find: 149 to 167 µsRaw DBI: 3.6 to 12 µs
Each find that misses the cache sends a query, so a loop of them costs a query per key. One handle with -in fetches every matching row in one query. The rows come back in the database's order, once each, so rebuild the input's order or duplicates yourself if you need them.
FOLLOWING KEYS AND JOINS
Following a post's key to its author: 54 to 91 µs, DBIx::Class 188 to 207 µs
Following a user's key back to their 10 posts: 73 to 114 µs, DBIx::Class 201 to 224 µs
Joining posts to authors, per row: 14 to 15 µs, DBIx::Class 44 to 46 µs
A GROUP BY over 10000 posts into 1000 groups: 0.92 to 6.4 ms, DBIx::Class 1.35 to 7.1 ms
Reading each follow_fk handle sends a query. In a loop over many rows, a join fetches the matching pairs in one query, and left_join keeps rows with no match (DBIx::QuickORM::Manual::Joins).
A grouped read is 1.1x to 1.6x faster than DBIx::Class, closest on MySQL. A handle blesses its aggregate rows directly, and field reads their columns without the inflation methods unless a column has a type. bare on a grouped handle returns hashrefs instead (not measured here):
my @per_user = $orm->handle('posts')->group_by('user_id')->fields({user_id => 'user_id', n => [count => '*']})->bare->all;
WRITING
# one statement, nothing read back
$orm->insert_many(posts => [map { {user_id => $id, title => "Post $_"} } 1 .. 100]);
# one UPDATE changes every matching row
my $changed = $orm->handle('posts')->where({user_id => $id})->update({views => 0});
# one transaction around the loop
$orm->txn(sub { $orm->insert(posts => {user_id => $id, title => "Draft $_"}) for 1 .. 10 });
Per row:
insert, getting each row back: 31 to 44 µs, DBIx::Classcreate99 to 125 µsinsert_manyof 1000, void context: 3.1 to 7.9 µs, DBIx::Classpopulate3.8 to 15 µsinsert_manyof 1000, rows back: 5.6 to 9.8 µs, DBIx::Classpopulate68 to 89 µsupdateof a fetched row: 39 to 51 µs, DBIx::Class 66 to 99 µsA transaction holding one insert: 59 to 84 µs, DBIx::Class 121 to 146 µs
A nested transaction (savepoint) holding one insert: 42 to 63 µs, DBIx::Class 120 to 130 µs
In void context, insert_many is 1.7x to 2.6x faster than populate on the servers, and a little faster on SQLite (3.1 against 3.8 µs). On the servers it also beats raw DBI executing one prepared INSERT per row in a transaction (9.5 to 13 µs). On SQLite that loop takes 1.0 µs.
A handle's update of 1000 rows took 0.18 to 2.5 ms in one statement. At 39 to 51 µs a row, updating them one at a time would take about 39 to 51 ms. It returns a count, and drops every canonical copy of the table's rows, since it cannot tell which changed.
Where the statement supports RETURNING, an insert that returns a row (without as_given) or a row update reads back the default fields and the fields written, and a row update also the fields the row held. MySQL has no RETURNING, and QuickORM does not use MariaDB's on UPDATE ("RETURNING" in DBIx::QuickORM::Manual::Dialects), so there a written row holds only its key and other fields load when read. On MySQL, insert_many returns rows only when every row carries its key.
A count is one query: 15 to 16 µs on SQLite and MariaDB, 367 to 432 µs on PostgreSQL and MySQL (where raw DBI takes 359 to 424), against 136 to 585 µs for DBIx::Class.
DEBUG
my $traced = DBIx::QuickORM->new(connect => $connect, debug => 1); # records which call made a row stale
debug => 1 makes canon_status, and the croak for writing a row that is no longer canonical, name the call that replaced the row. It is off by default because every public call that can write row data then records its caller. Per operation:
find, row not held: 0.2 to 0.8 µs moreupdateof a fetched row: 0.1 to 4.8 µs moreinsert, getting the row back: up to 9.8 µs more, within the noise on some databases
A fetch of many rows costs nothing more per row, unless it replaces canonical copies, each of which gets a note. Turn it on while chasing a stale row, or in development and tests ("WHY A ROW IS NOT CANONICAL" in DBIx::QuickORM::Manual::Caching).
STARTUP
my $quick = DBIx::QuickORM->new(connect => $connect, autotype => undef); # no types loaded, nothing inflated
# a later new() in the same process reuses the first scan
my $again = DBIx::QuickORM->new(connect => $connect);
# after changing tables in a running process
my $fresh = DBIx::QuickORM->new(connect => $connect, rescan => 1);
Time for a fresh process to load, build, find one row, and read a date and a JSON value, in ms, at 10, 100, and 1000 tables:
Scan,
autotype => undef: 40 to 52, 45 to 59, 104 to 144Scan with types: 104 to 121, 112 to 125, 181 to 221
Table classes: 107 to 124, 123 to 142, 296 to 333
DBIx::Class classes, no inflation: 60 to 69, 78 to 85, 381 to 396
DBIx::Class classes, inflating dates and JSON: 113 to 125, 142 to 151, 520 to 535
DBIx::Class::Schema::Loader: 164 to 264, 508 to 4921, and 6.7 to 74 s (on MySQL one run at 1000 tables did not finish within 8 minutes)
At 10 tables, in "scan with types", the first query and its two field reads took 58 to 60 ms. That first use also loads DateTime and its formatters. A program that touches no date at startup, not even a date primary key, does not pay it. DBIx::Class's first inflated read took 54 to 58 ms.
A scan builds faster than table classes, which are compiled and merged onto the scan. With autotype off, and no types given, nothing is inflated, so nothing loads DateTime. That is most of the gap above, since the measurement reads a date. It also saved 2.3 to 4.3 ms of build at 10 tables, and 8.8 to 14 ms at 1000. The cost is that fields come back as stored (DBIx::QuickORM::Manual::Types).
The first scan of a database is kept for the rest of the process, so building another ORM for the same database and session (after a lost connection, say) does not scan again. In-memory SQLite and DuckDB databases, and PostgreSQL sessions with temporary tables, always scan. A rebuild and one find took 0.25 ms on SQLite, 0.41 to 0.45 ms on MariaDB and MySQL, and 2.0 ms on PostgreSQL. DBIx::Class, cloning its compiled classes, took 0.43, 0.54 to 0.56, and 2.2 ms. With rescan the scan runs again: 0.47 ms, 0.82 to 2.2 ms, and 5.6 ms. Pass rescan only when tables changed.
SEE ALSO
DBIx::QuickORM::Manual::Caching for what the row cache does, DBIx::QuickORM::Manual::RowClasses for hashrefs and row classes, DBIx::QuickORM::Manual::BulkInsert for insert_many, and DBIx::QuickORM::Manual::DBIC for the same tasks side by side with DBIx::Class.
SOURCE
The source code repository for DBIx-QuickORM can be found at https://github.com/exodist/DBIx-QuickORM.
MAINTAINERS
AUTHORS
COPYRIGHT
Copyright Chad Granum <exodist7@gmail.com>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.