[CDBI] Is Class::DBI slow?
Matt S Trout
dbix-class at trout.me.uk
Sun Apr 30 13:59:49 BST 2006
Ankur Gupta wrote:
> Hi All,
> My first try with Class::DBI and DBI modules and I find that making a
> query using Class::DBI takes much much more time than just DBI.
> Is it me who is doing something wrong or Class::DBI is always slow(I
> have kind of feeling that its me who is doing something wrong).
No, Class::DBI is much slower than pure DBI - because it's inflating a full
object for each record. A simple query like this, DBI is always going to be
much faster - DBIx::Class is faster than Class::DBI, Rose::DB::Object faster
still, but raw DBI will blow all of them away on raw straight line speed.
The thing is, that's not why you use any of these packages - you use them
because they produce objects, in classes on which you can define methods, so
you can encapsulate your logic in the code there. It's a question of
abstraction level - writing C code that called your DB's library of choice
would probably be faster than DBI :)
> Also, I have another question.
> Why do I need to specify the column names, primary keys in Class::DBI
> while not in DBI? I mean, how is DBI module able to determine the
> primary keys and other fields while Class::DBI not.
DBI isn't. But it's not intelligent enough to need that information.
Class::DBI, OTOH, can do rather smarter things - e.g.
my $obj = Class->retrieve(1); # SELECT * FROM table WHERE pk_col = 1;
$obj->update; # UPDATE table SET colname = 'value' WHERE pk_col = 1;
In raw DBI, you'd have to pass everything by hand, every time. Since CDBI got
the information up-front, it already knows the column names (so it can
generate the accessors for you), and it already knows the pk cols (so it can
generate the WHERE clause).
Also bear in mind that Class::DBI, DBIx::Class and Rose::DB::Object all have
available code (in the form of separate ::Loader packages for the first two)
that can interrogate your database and automatically set up your classes -
personally I prefer declaring the stuff myself, but if you don't want to the
alternative exists :)
Matt S Trout Offering custom development, consultancy and support
Technical Director contracts for Catalyst, DBIx::Class and BAST. Contact
Shadowcat Systems Ltd. mst (at) shadowcatsystems.co.uk for more information
+ Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +
More information about the ClassDBI