getById db query optimization