← All topics/Data persistence

Question 5

Performance with large datasets

Your database grows to thousands of records and performance degrades. How do you diagnose and optimize reads/writes?

Answer outline

Profile before you change anything. Measure slow fetches, write latency, and memory spikes on a realistic dataset, then find the exact hotspot: a predicate, a sort, a relationship traversal, or how often you save.

Once you know the hotspot, four things speed up reads:

  1. 1.Indexes: add them on the attributes you filter and sort by most. Each index costs write time, so index the hot paths only.
  2. 2.Pagination and batching: fetch in pages with a batch size or a limit and offset, so a list of thousands of rows never loads at once.
  3. 3.Narrower fetches: pull only the properties a screen needs, and prefetch the relationships it walks so each row doesn't fire a Core Data fault and trigger a separate fetch.
  4. 4.Precomputed fields: store a derived value such as a count or a sort key when a measured read needs it, and keep its update rule explicit.

Writes are a separate problem. Batch inserts and updates into one save instead of saving inside a hot loop. Run heavy transforms such as decoding or image processing off the main thread, then persist the result through a background context.

Principles

  • Profile on a realistic dataset, and compare before and after with the same workload.
  • Fix the top hotspot first, and don't micro-optimize cold paths nobody waits on.
  • Prefer a simple schema with the right indexes before any denormalization.
  • Keep heavy fetches and saves off the main thread, because a stalled scroll is the symptom users notice.