0

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?

1 Answer 1

1

Is it in option to break it up into paging or similar? Often the easiest way to improve performance is to reduce the amount of work that needs to be done.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm visualizing the data so it's little more complicated than displaying data in a table. It's all measured data so GIS, temperature etc. I have already split the data request into a series of smaller requests and have algorithms for simplifying and compressing the data set.
Is it possible to keep summary data in your database. Use after_save callbacks or something to update it as needed. Perhaps create a table the stores the summary information, it will be an irrelevant overhead when saving each record but a huge saviour when reporting on it. Otherwise perhaps you can use caching?
Yes, I agree. I have a layer of caching implemented which saves the json data so the database is not hit for the same query. My question is more around micro optimizing getting the data from the database to the browser.
I know I am skirting around the question, but I don't have a solution to that, it sounds like you are already optimising well. It just sounds like looking elsewhere at the bigger picture is probably where you will get the biggest gains. Good luck with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.