Apex is not the right tool for this, as it is not designed to handle such a large amount of CSV data (hence why you are getting an error).
I would suggest using Salesforce Data Loader if this job can be done manually. Otherwise something like a function in AWS Lambda could handle this use case.
Alternatively, you could use Salesforce Connect to view the records in one org from another org, if that works with your use-cases.