Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

Patterns for saving objects to the DB.

Question posted by: lister (Guest) on March 19th, 2008 01:45 PM
Hi,

Currently each of the members within my objects have a field in a
table. The objects have a Save() method which writes out all the
members at once in a single SQL statement.

Although this works fine I have been pondering that it's not
particularly efficient to update every field if just one boolean has
changed for example. I have been considering flagging dirty fields
within my object and only writing those out when a Save() is called. I
am, however, wondering whether it's worth the effort to implement
this, and have a few questions:

1) Is MySQL smart enough to realise that certain fields haven't
changed anyway and so to not bother updating any indexes on that
field?

2) Is it really worth worrying about the redundancy for 10-15 field
rows?

3) In researching this a bit, I have found that some people just
serialise the object to a BLOB field. Is this a common method? It
seems a bit nasty to me since it prevents much ability to query the
table, and ties the data to a particular language.

4) How do you guys normally do it?

Many thanks
ZeldorBlat's Avatar
ZeldorBlat
Guest
n/a Posts
March 19th, 2008
01:55 PM
#2

Re: Patterns for saving objects to the DB.
On Mar 19, 9:36 am, lister <listerofsme...@hotmail.comwrote:
Quote:
Hi,
>
Currently each of the members within my objects have a field in a
table. The objects have a Save() method which writes out all the
members at once in a single SQL statement.
>
Although this works fine I have been pondering that it's not
particularly efficient to update every field if just one boolean has
changed for example. I have been considering flagging dirty fields
within my object and only writing those out when a Save() is called. I
am, however, wondering whether it's worth the effort to implement
this, and have a few questions:
>
1) Is MySQL smart enough to realise that certain fields haven't
changed anyway and so to not bother updating any indexes on that
field?
>
2) Is it really worth worrying about the redundancy for 10-15 field
rows?


You're prematurely optimizing. Do it the straightforward, easy,
maintainable way first, then optimize if you run into specific
performance problems. At this point you should have a much better
idea of what is causing the problem -- and you may be surprised to
find that it isn't what you thought it was.
Quote:
>
3) In researching this a bit, I have found that some people just
serialise the object to a BLOB field. Is this a common method? It
seems a bit nasty to me since it prevents much ability to query the
table, and ties the data to a particular language.


You seem to already know why this is a bad idea.
Quote:
>
4) How do you guys normally do it?
>


Use the ActiveRecord pattern.



Captain Paralytic's Avatar
Captain Paralytic
Guest
n/a Posts
March 19th, 2008
02:15 PM
#3

Re: Patterns for saving objects to the DB.
On 19 Mar, 13:36, lister <listerofsme...@hotmail.comwrote:
Quote:
>
1) Is MySQL smart enough to realise that certain fields haven't
changed anyway and so to not bother updating any indexes on that
field?


Try running an update statement where no data has changed and MySQL
will tell you that no rows were affected.

 
Not the answer you were looking for? Post your question . . .
189,798 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors