optimized message querying