MDL-33788 completion: SQL optimisation