Ah, this is covered under the idea of hierarchy of values. Certainly, at certain times the good of knowledge (as opposed to ignorance) is superceded by the good of mental stability. Certainly, at certain times, the good of order is superceded by the good of freedom. To make it even clearer, the Christian would say that the good
of sexual relations (because pleasure is
a good, although many Christian denominations would refute that opinon) is superceded by the good of having one sexual partnership throughout one's life. The Christian would say that the good of a woman choosing what to do with her own body is superceded by the good of protecting the life of a fetus, in the case of abortion. And yet, although one good is superceded by another, in all of these cases, each one of those things were a good and never ceased to be a good. Because one good is not the best
, it does not follow that that said good ceases to be an objective good; it is merely not best
in that situation.
So yes, pain is better than non-pain, and if we lived in an ideal world, pain would not be necessary to gain knowledge, because all possible goods would be actual in every situation. But since we live in a flawed world, not all possible goods are attainable, and one good must be sacrificed for another. It's like moral economics.