Optimized use of database