[sf-perl] ORM vs. DBI

Quinn Weaver quinn at pgexperts.com
Sun Feb 27 12:10:31 PST 2011


I agree with the points made so far about the complementary (and optional)
nature of ORMs.

I have a talk about using Catalyst, specifically, without an ORM, which you
may find useful. It's quite easy to make raw SQL queries in a Catalyst app and
never worry about an ORM. Even if that's not your intention, you may glean
some new information from its criticism of ORMs. The slides are at
http://bit.ly/NoORM They start out with motivation, then move into
implementation.

Do take with a grain of salt the section that criticizes the shortcomings of
ORMs; most of those criticisms don't apply to DBIC, which is an extremely
well-written ORM (it even supports aggregates—though you could argue that
that's a "reinventing SQL" feature).

In general, my strongest criticism of ORMs is that they make you think about
DBs the wrong way. They encourage a one-at-a-time mentality for DB operations:
e.g. in a Perl foreach/for loop, open a transaction, do an update or insert
(save), and close the transaction, *for each of hundreds or thousands of
items*. My colleague at pgexperts.com Christophe Pettus calls this
"pathological iteration." It really is a performance killer.

You don't have to do things this way, but the API of ORMs, combined with the
way they isolate you from SQL, combined with (sad to say) the naive SQL skills
of many web programmers, make mistakes natural. If you are more familiar with
relational model, you won't fall into this trap, but you may still have
difficulty expressing what you want to say.

The good news, though, is that you don't have to use an ORM at all. Even if
you use one, you can still fall back to raw SQL when you need it. My talk
doesn't cover that, but it sounds as if you have a good handle on it.

Anyway, those are my (immediate) thoughts on the subject. I hope they're
helpful. Good luck with your project!

--
Quinn Weaver
PostgreSQL Experts, Inc.
http://pgexperts.com/
1-888-743-9778 (my extension: 510)






More information about the SanFrancisco-pm mailing list