Jerry B
Jerry B

Reputation: 453

Does Core Data/SQLite compress redundant information?

I want to use Core Data (probably with SQLite backing) to store a large database. Much of the string data will be the same between numerous rows. Does Core Data/SQLite see such redundancy, and automatically save space in the db files?

Do I need to make sure that the same text in different rows is the same string object before adding it to the db? If so, how do I detect that a new piece of text matches something anywhere in the existing db?

Upvotes: 0

Views: 321

Answers (2)

Nyon
Nyon

Reputation: 11

Application-layer logic can help reduce space at the cost of application complexity.

Say your name field can contain either an integer or a string. (SQLite's weak typing makes this easy to do).

If string -- that's the name right there.

If integer -- go look it up on a name table, using the int as key

Of course you have to create that name table, either on the fly as data is inserted, or a once-in-a-while trawl through the data for new names that are worth surrogating in this way.

Upvotes: 0

Tom Harrington
Tom Harrington

Reputation: 70976

No, Core Data does not attempt to analyze your data to avoid duplication. If you want to save 10 million objects with the same attributes, you'll get 10 million copies.

If you want to avoid creating duplicate instances, you need to do a fetch for matching instances before creating a new one. The general approach is

  1. Fetch objects matching new data-- according to whatever standard indicates a duplicate for your app. Use a predicate with the fetch that contains the attribute(s) that you don't want to duplicate.
  2. If you find anything, either (a) update the instances you find with any new values you have, or (b) if there are no new values, do nothing.
  3. If you don't find anything, create a new instance.

Upvotes: 1

Related Questions