optimized query handling through history