An optimized correlation function estimator for galaxy surveys