catalog SQL optimisations.

Authored by @toporchillo.
Signed-off-by: Vadim A. Misbakh-Soloviov <mva@mva.name>