My requirement is to fetch multiple tables data(more than 200K rows) and convert to Json and return.
I can acheive above by two ways(Please suggest me if any other)
a. convert DB data to Json in query itself(array_to_json(array_agg() or json_agg()) b. Fetch DB data and convert to list of Java objects using rowmapper etc and then convert list of java objects to json.
My observations related to both ways,
a. convert DB data to Json in query itself(array_to_json(array_agg() or json_agg()):
1. array_to_json(array_agg()) is taking long time and which is leading to crash my application 2. replaced array_to_json(array_agg()) with json_agg() and query execution is taking long time(infact more than array_to_json(array_agg())) here too.
b.Fetch DB data and convert to list of Java objects using rowmapper etc and then conevrt list of java objects to json:
1. Application crahsed with outofmemory issue while parsing list of java objects(Count would be more than 200K) to Json
Database: Postgres
Query:
SELECT array_to_json(array_agg(dev_info)) FROM (select * from device_instance_global join device_active_global
on device_instance_global.device_id=device_active_global.device_id join dev_management on
device_active_global.system_id=dev_management.node_id
join device_model on device_active_global.device_code=device_model.device_code
and device_instance_global.device_error_class=device_model.device_severity
where dev_management.node_type<>'F' order by device_instance_global.device_raise_time DESC )
dev_info;
SELECT json_agg(alarm_info) FROM (select * from device_instance_global join device_active_global
on device_instance_global.device_id=device_active_global.device_id join dev_management on
device_active_global.system_id=dev_management.node_id
join device_model on device_active_global.device_code=device_model.device_code
and device_instance_global.device_error_class=device_model.device_severity
where dev_management.node_type<>'F' order by device_instance_global.device_raise_time DESC )
dev_info;
Please suggest me is there any best way to acheive my requirement.