Reduce memory consumption by using cursors