0

The heart of my app is a multi-conditional comparison using an input array and parameters stored in a few database tables.

I'm trying to make the process most efficient ... and I think this could lead to some good conversation about using memory vs. accessing a database.

Here's one example:

  1. I have a Merchant, MerchantUserRelation, and User table.
  2. Most of the data I need to store in a temporary array is from MerchantUserRelation, BUT at one point, I need to check if it's the user's birthday (user.birthdate.today?).

To me, it seems there are two options:

  1. Create a temporary array with only data from UserMerchantRelation and then access the database separately for the user.birthdate.today? method (2 hits to the database), --or--
  2. Create a slightly-larger temporary array with both the data from UserMerchantRelation AND User (and thus hit the database only once)

For this example I recognize the differences are EXTREMELY small (read: negligible), but what if the array sizes and # of database accesses required were much larger?

Thank you for any references and/or insights!

1 Answer 1

1

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil - Donald Knuth

As you said, the difference is negligible. And there is no absolute solution: memory or db hits. So why care them now? If your app grows and really met bottleneck, you can always profile and refactor to overcome it, by whatever workable methods.

2
  • Thank you, this confirms what I'd hoped -- that there isn't some obvious difference in efficiency of which I (being novice) was unaware.
    – Dan
    Commented May 25, 2013 at 18:31
  • @DanShev, yes, just go ahead following the common conventions, and solve problems only when they exist.
    – Billy Chan
    Commented May 25, 2013 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.