Write a function to optimize data retrieval from a database using indexing, caching, and partitioning.