You wrote:
> I suppose the key question is, is there a big > performance boost in grabbing the serialized object, > rather than constructing a new instance out of the > stored data?
Don't know off the top of my head; you could definitely test it using Benchmark. You could test something like Class::DBI against a hand-rolled solution using Storable and DBI.

Hate to say it, but it really depends on your application. If you have an application where you need to search on the fields in your object/database, you shouldn't be serializing your data.

On the other hand, one thing you gain by storing a serialized object is that you don't have to worry about the columns in your table. If you add a new attribute to your object, you don't need to worry about database, etc.

You also said:

> If not, then it seems like you give up the ability to search > the stored fields if you just serialize.
Yes, you do. And in some cases that's a real drawback. You don't want to have to load every object in the database then search through them w/ Perl, just to get back the objects you want. That's ugly and a memory hog and is just the sort of thing you want to avoid.

In my experience I very often find situations where I do want searchability, but YMMV.

The situations where serializing works well is where the only thing you'll ever want to search on is some sort of primary key ID. Session data, for example: if you store sessions in a DB and give out IDs for each session, your session layer really doesn't care what's in a session; and your app layer usually doesn't need to search on anything other than the session ID. So it makes a lot of sense to bundle up your data into a serialized stream and store it in the database, then thaw it in your session layer when you pull it back out. Object goes in, object comes out.

An example of needing searchability is when you have lots of different columns that you may need to search on. For example, you might have a "document record" stored in a DB, and you may need to search by author, title, body, last modified, etc. Here you *really* don't want to serialize your data, because you're just going to end up with a mess of Perl code sorting through the objects, where you really should let your database do that work.

Pointers to modules. If you want to store unserialized data in a DB--column maps to attribute, row maps to object, etc.--take a look at Class::DBI. I've not used it because I have a DB-object layer of my own that I use, but it's reputable, I believe. :) Update: Also check out Tangram.

For serializing, you could take a look at Apache::Session, which is a good way of managing session data through various interfaces (file, DB, etc.).


In reply to Re: storing serialized object in a database by btrott
in thread storing serialized object in a database by dshahin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.