Rails:使用 ajax 对抗较长的 http 响应时间。这是个好主意吗?请帮助了解实施细节
我用谷歌搜索了一些教程,浏览了一些答案,但无法找到解决我的问题的方法。
我正在编写一个网站,该网站应该显示几乎实时的股票图表。 数据存储在不断更新的 MySQL 数据库中,我编写了一个 find_by_sql 查询代码,它获取绘制图表所需的所有数据。一切都很好,除了性能 - 不同的查询需要一秒到一分钟才能从数据库中获取所有数据,这段时间包括必要的(My)SQL 服务器端计算。这简直令人无法接受。
我得到了以下想法:如果一次从MySQL服务器查询数据一个点而不是整个数据集,那么只需要大约1-100ms就可以获得单个点。
我想数据获取过程可能是浏览器驱动的。用户按下按钮以绘制图表后,控制器向数据库发出一个请求并渲染进度条(例如 1% 就绪)。当浏览器得到响应后,立即发出(ajax)请求,服务器取出下一条数据并渲染“2%”。
依此类推,直到所有数据准备就绪并且服务器显示所请求的图表。 这个可以用rails+js实现吗,网上有解决类似问题的教程吗?我想如果这件事完全可行的话,那么之前就应该有人这样做了。
我读过几篇关于 ajax 的文章,我相信我确实了解一般原理,但自己从未进行过重要的 ajax 编程。
感谢您抽出时间!
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 myself.
Thanks for your time!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用显示 gif 动画(表示“正在工作”)的 div 加载页面。然后,使用 body onload 启动 ajax 请求并替换该 div 的内容。
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.