Reputation: 2662
I have a index.html page where I make an ajax call and I get the JSON data on success of ajax and send the JSON to other functions.
$.ajax({
type : "GET",
url : 'get_data',
dataType : 'json',
success: function(data) {
update_page(data.info);
}
})
In the controller I have
def get_data
parents = Parent.all
students = Student.all
teachers = Teacher.all
schools = School.all
@collection = {"parents" => parents, "students" => students, "teachers" => teachers, "schools" => schools}
respond_to do |format|
format.json { render json: Oj.dump({"info" => @collection.to_json(:except=> [:created_at, :updated_at] ) }) }
end
end
There are around 1500
records in each Model. It takes lot of time to respond and also memory consumption is also high. I have used new relic
to trace the response time and it took around 12000ms
and I have used oink
and other tools like rack mini profiler
and it shows the memory consumption and response time higher but I am not sure on how to fix this issue.
Please help me solve this. I have been trying for the past two days and I could not find a solution.
Thanks in advance!
Upvotes: 1
Views: 2344
Reputation: 3207
The basic idea is to only query the amount of data that you need. Do you really need all the records (1500 records * 4 models = 6000 records) all the time? Just look at StackOverflow, there are thousands of questions but on one page a user only need to see around 50 questions. Maybe you want to implement pagination for your page.
UPDATE
If loading all the records at start up is a must. You might want to look at browser offline storage. You will load all your data once, store it in browsers and use the offline data instead of querying all the data per request. This is for Firefox. You can easily find something similar for other browsers.
https://developer.mozilla.org/en-US/Apps/Build/Offline
Upvotes: 1
Reputation: 1862
Consider disk or memory caching. Whatever is bottle neck, SQL query or JSON serialization, it will be blaze fast while serving from disk. First request will be cached, obviously it will take long time, but only once. However you must take care about cache invalidation by your self. I'm pretty sure it will worth it.
Upvotes: 0