标签云

微信群

扫码加入我们

WeChat QR Code

I'm currently developing a SPA(shift planner) using rails 5 in api mode and angular JS. The problem is it takes the browser 1 second to display the plain JSON.The structure of the JSON looks like this:[{"id": 2,"name": "person0","skills": [{"skill_id": 3,"person_id": 2,"name": "skill1"},{"skill_id": 6,"person_id": 2,"name": "skill4"}],"roles": [{"name": "role4","id": 5,"person_id": 2},{"name": "role8","id": 9,"person_id": 2}],"languages": [{"name": "language1","id": 2,"person_id": 2}],"shifts": [{"date_of_shift": "2016-02-29T00:00:00+00:00","shift_id": 1011,"shift_type_id": 1,"name": "shift_type0"},{"date_of_shift": "2016-03-01T00:00:00+00:00","shift_id": 1012,"shift_type_id": 2,"name": "shift_type1"},{"date_of_shift": "2016-03-02T00:00:00+00:00","shift_id": 1013,"shift_type_id": 4,"name": "shift_type3"},{"date_of_shift": "2016-03-03T00:00:00+00:00","shift_id": 1014,"shift_type_id": 8,"name": "shift_type7"},{"date_of_shift": "2016-03-04T00:00:00+00:00","shift_id": 1015,"shift_type_id": 1,"name": "shift_type0"}]},So each person has about 40 elements and I have 50 people (for the department I want to display) in my test data, which leads to 2000 json elements.So I did some research how fast it is to display a json and it looks like even 4 years ago it was way faster than what I experience.This would be the output of the railsserver for the request.Completed 200 OK in 913ms (Views: 18.8ms | ActiveRecord: 73.3ms)When I'm getting all people in the db (2000) via the API as jsonit only takes 300ms to finish displaying the json.Server output:Completed 200 OK in 335ms (Views: 329.8ms | ActiveRecord: 4.6ms)Now it takes way more time to render the view on the server side, but the time spend in active record and the view is the same as the total time, whereas in the other api call active record and the view only take 100ms and 800ms are spend doing something different.The people json would look something like this:[{"id": 1,"name": "jonny","department_id": 1,"created_at": "2016-02-04T13:33:34.357Z","updated_at": "2016-02-04T13:33:34.357Z"},only 2000 times.I get the data using a psql query which gives me exactly the data I need. I don't do anything else with it in rails.This behavior is very confusing to me. Can someone explain the differences and why they happen and hopefully how to improve the performance in the first case.edit: I tried removing the person_id from skills, roles and languages array but it had almost no impact on the performance.


this is my psql query. I spend the last week optimizing it from 1sec to only take 70ms (according to the server log). The query gives me exactly what I need. I don't do anything else in rails with data, despite formating it as a json

2019年04月19日32分32秒

well if the time is not spent In the view and in the model, mb it takes this long to format the response of the query into a json before sending it to the browser.

2019年04月18日32分32秒

That would be my next guess. Which json parser do you use? I'm not sure if oj is rails 5 compatible, but you could try it, as it's known to be relatively fast: rubygems.org/gems/oj

2019年04月19日32分32秒

Currently working an getting the json as I want it directly from postgres and pass it directly to the browser. looks like it will work, dunno about performance yet.

2019年04月19日32分32秒