Optimize DB code