Reduce memory usage on long queries #1056