From optimal score matching to optimal sampling