开发者

Rails : fighting long http response times with ajax. Is it a good idea? Please, help with implementation details

I've googled some tutorials, browsed some SO answers, and was unable to find a recipe for my problem.

I'm writing a web site which is supposed to display almost realtime stock chart. Data is stored in constantly updating MySQL database, I wrote a find_by_sql query code which fetches all the data I need to get my chart drawn. Everything is ok, except performance - it takes from one second to one minute for different queries to fetch all the data from the database, this time includes necessary (My)SQL-server side calculations. This is simply unacceptable.

I got the following idea: if the data is queried from the MySQL server one point a time instead of entire dataset, it takes only about 1-100ms to get an individual point.

I imagine the data fetch process might be browser-driven. After the user presses the button in order to get a chart drawn, controller makes one request to the database and renders, say, a progress bar, say 1% ready. When the browser gets the response, it immediately makes an (ajax) request, and the server fetches the next piece of data and renders "2%".

And so on, until all the data is ready and the server displays the requested chart. Could this be implemented in rails+js, is there a tutorial for solving a similar problem on the Web? I suppose if the thing is feasible at all, somebody should have already done this before.

I have read several articles about ajax, I believe I do understand general principles, but never did nontrivial ajax programming m开发者_高级运维yself.

Thanks for your time!


You could load the page with a div that displays an animated gif (indicating "working"). Then, use body onload to launch your ajax request and replace the contents of that div.

<body onload="some function call to Ajax.Updater('chart')">
<div id="chart">
  <img src="working.gif">
</div>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜