Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 27th, 2005, 02:41 PM
R++'s Avatar
R++ R++ is offline
mentally screwed up
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2004
Location: México
Posts: 857 R++ User rank is Sergeant (500 - 2000 Reputation Level)R++ User rank is Sergeant (500 - 2000 Reputation Level)R++ User rank is Sergeant (500 - 2000 Reputation Level)R++ User rank is Sergeant (500 - 2000 Reputation Level)R++ User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 19 h 31 m 41 sec
Reputation Power: 19
more memory vs. a lot of queries

hello
lets say i have an object of class 'content', it has two methods 'title' and 'text', is it better to load all the data from the db when de object is created (more memory), or to query the database each time the data is needed (more queries)???

Example 1, more memory
Code:
class content{
private title
private text

public __construct(){
query the db to get title and text
store title and text in properties
}

public title(){
return this->title
}

public text(){
return this->text
}

}


example 2, more queries
Code:
class content{

public title(){
query to get title 
return title
}

public text(){
query to get text
return text
}

}


thanks for your ideas!
__________________
<RamÔ_ôn >
...and i will strike down upon thee with great vengeance and furious anger those who attempt to poison and destroy my brothers, and you will now my name is the LORD when i lay my vengeance upon you
Pulp Fiction

Reply With Quote
  #2  
Old November 27th, 2005, 02:46 PM
SimonGreenhill's Avatar
SimonGreenhill SimonGreenhill is offline
(retired)
Dev Shed God 11th Plane (10000 - 10499 posts)
 
Join Date: Dec 2003
Location: The Laboratory
Posts: 10,101 SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)  Folding Points: 4925 Folding Title: Novice Folder
Time spent in forums: 3 Months 3 Weeks 5 h 49 m 4 sec
Reputation Power: 1331
Facebook
Moved to Software Design.

FWIW - premature optimisation is the root of all evil, and you should benchmark, but off the top of my head, I'd say protect your database first.

--Simon

Reply With Quote
  #3  
Old December 3rd, 2005, 06:04 AM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 6
The cost of performing a select (plus more joins, etc) is measured in microseconds. The cost of going into the database (connecting) is measured in milliseconds (thousands of times more).

So its better to have fewer 'chunky' calls and get as much as you can rather that frequent 'chatty' calls get bits at a time. This is the case for anything to do with persistence or data travelling between processes (and/or between machines) such as web services, etc..

I'd suggest not going to the database for each property as this would be a huge hit on performance. And in fact, I would only get a single row if that is all you need at that time. If you are getting a list of rows, that would be all loaded from a single database call.

Reply With Quote
  #4  
Old December 3rd, 2005, 12:27 PM
jafet jafet is offline
Redpill
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2005
Posts: 1,658 jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 9 h 52 m 31 sec
Reputation Power: 106
Send a message via MSN to jafet
Are you gonna use the members many times? Or just a couple of times?

It all boils down to the program you're working on now.

Reply With Quote
  #5  
Old December 26th, 2005, 01:59 PM
techMonster techMonster is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 96 techMonster User rank is Lance Corporal (50 - 100 Reputation Level)techMonster User rank is Lance Corporal (50 - 100 Reputation Level)techMonster User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 16 h 6 m 21 sec
Reputation Power: 4
I would suggest that if possible, analyse your system and find out which part of database is used most and load only that part or information in memory.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > more memory vs. a lot of queries


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT