I need to take a large amount of data from multiple database tables, format the data into nested JSON and spit it out to the browser and do it really fast. I've been profiling different techniques to optimise the process. The two techniques I've tried so far are:
1) Run query and return the result to rails. Construct json in rails and send to the browser.
2) Run query and using string concatenation, build the json in the database, return a single string to rails. Rails sets the content type to application/json and sends the string to the browser.
The latter technique is much faster than the former. I can speculate this is because there is an overhead to turning database objects into ruby objects and the sheer number of objects is overwhelming.
Is there a better way of doing this?