TL;DR - use in-memory cache instead of expensive database lookups on every iteration