|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
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 |
|
#2
|
||||
|
||||
|
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
__________________
|
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
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.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Software Design > more memory vs. a lot of queries |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|