Andrew
Andrew

Reputation: 121

Should I load mysql data into arrays or just query the database using java

I am making a java desktop application for billing customers that will be using a mysql database (so I can make a php frontend using the same database later). I was wondering if I should make a class that puts all the mysql info into arrays on startup so I can work with the arrays or if I should just query the database when I need to access data.

I was wondering what is the most efficient, fastest etc... Has anyone got an good pointers?

Upvotes: 2

Views: 226

Answers (3)

Michael Berkowski
Michael Berkowski

Reputation: 270637

Query as needed rather than pre-loading all the information. This will use potentially a lot less memory. Some of your data may need to be cached while working, but odds are most of it doesn't. The RDBMS is already designed and optimized to store and retrieve data as needed, so it is best allowed to do its job.

Upvotes: 0

duffymo
duffymo

Reputation: 308763

Putting data into arrays might make sense if it's static - I'd call that caching.

But billing data seems more dynamic to me, depending on how you define it. In that case, I'd query the database each time.

Upvotes: 0

Ned Batchelder
Ned Batchelder

Reputation: 375594

You should query the database when you need the data. That's what databases are for. If you bring all the data into Java arrays, then you will end up building querying methods on those arrays, or limiting yourself to simplistic ways of accessing the data.

If your data is small enough to fit easily into RAM, then MySQL will cache it all anyway, and it will go just as fast as if you had pulled it into arrays first.

Upvotes: 3

Related Questions