Measuring the two-point correlation function of the galaxies in the Universe gives access to the underlying dark matter distribution, which is related to cosmological parameters and to the physics of the primordial Universe. The estimation of the correlation function for current galaxy surveys makes use of the Landy-Szalay estimator, which is supposed to reach minimal variance. This is only true, however, for a vanishing correlation function. We study the Landy-Szalay estimator when these conditions are not fulfilled and propose a new estimator that provides the smallest variance for a given survey geometry. Our estimator is a linear combination of ratios between paircounts of data and/or random catalogues (DD, RR and DR). The optimal combination for a given geometry is determined by using lognormal mock catalogues. The resulting estimator is biased in a model-dependent way, but we propose a simple iterative procedure for obtaining an unbiased model- independent estimator.Our method can be easily applied to any dataset and requires few extra mock catalogues compared to the standard Landy-Szalay analysis. Using various sets of simulated data (lognormal, second-order LPT and N-Body), we obtain a 20-25% gain on the error bars on the two-point correlation function for the SDSS geometry and $Lambda$CDM correlation function. When applied to SDSS data (DR7 and DR9), we achieve a similar gain on the correlation functions, which translates into a 10-15% improvement over the estimation of the densities of matter $Omega_m$ and dark energy $Omega_Lambda$ in an open $Lambda$CDM model. The constraints derived from DR7 data with our estimator are similar to those obtained with the DR9 data and the Landy-Szalay estimator, which covers a volume twice as large and has a density that is three times higher.