Hi Vidya,
In my opinion, for such complex requirement, you should write a transformation and store the data in the required format in another DSO. If performance is the key concern for you, then memory consumption should not be that much issue.
Hence I'd suggest to persist the data in the required format and ensure that the performance is better / as expected.
Regards.
Ravi