optimized database insertion code