Fix query which will takes too long for large installations.