user3074909
user3074909

Reputation: 51

Cache SQLite data in objects?

I need for my project a SQLite database to store relevant information, which will be read next time, when I start the program.

Is it better to read the complete database, creating objects for each row and work on these objects stored in a list now or should I always ask the database for the row?

The database will have around 10,000 rows and get around one query each second, where I want to select a row. And if I should work with objects in a list, how can I bring them back? I think dropping the table and creating a new one is not the best solution.

Upvotes: 2

Views: 672

Answers (2)

Matthijs Bierman
Matthijs Bierman

Reputation: 1757

Premature optimization is the root of all evil. -- Donald Knuth

You really shouldn't be optimizing something that isn't a performance problem right now.

Upvotes: 0

Noctis
Noctis

Reputation: 11783

There's up and downs to each option:

Get all from db into a list of objects:
Plus:

  • you get to have the objects as a list and it's nice and easy to work with

Minus:

  • Bigger memory footprint.

Get each when you need it:
Plus:

  • Let the database worry about what you need

Minus:

  • need to call the db over and over obviously.

In each case, I doubt performance wise you'll notice a huge difference assuming your rows aren't huge.

If you do decide to query once and work with the objects, you'll have to either save all or save all the one that changed before you quit (and if your program dies unexpectedly, you might lose changes).

If you get one item at a time, and then save it, more calls to db (disk, since it's sqlite), but if something goes bad, you might lose less info.

Upvotes: 2

Related Questions