loop through query in batches